首页 > 其他分享 >2023年9月14日 天气:阴

2023年9月14日 天气:阴

时间:2023-09-24 23:36:36浏览次数:58  
标签:14 verifySize int 天气 param Color 2023 new g2

  今天看了关于java web的介绍,然后安装了vs code和他的一些使用技巧。

 随机生成一张验证码图片

public class CheckCodeUtil {

        public static final String VERIFY_CODES = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private static Random random = new Random();

        public static void main(String[] args) throws IOException {
            FileOutputStream fos=new FileOutputStream("d:/abc.jpg");
            outputVerifyImage(100,50,fos,4);
        }

        /**
         * 输出随机验证码图片流,并返回验证码值(一般传入输出流,响应response页面端,Web项目用的较多)
         *
         * @param width
         * @param height
         * @param os
         * @param verifySize
         * @return
         * @throws IOException
         */
        public static String outputVerifyImage(int width, int height, OutputStream os, int verifySize) throws IOException {
            String verifyCode = generateVerifyCode(verifySize);
            outputImage(width, height, os, verifyCode);
            return verifyCode;
        }

        /**
         * 使用系统默认字符源生成验证码
         *
         * @param verifySize 验证码长度
         * @return
         */
        public static String generateVerifyCode(int verifySize) {
            return generateVerifyCode(verifySize, VERIFY_CODES);
        }

        /**
         * 使用指定源生成验证码
         *
         * @param verifySize 验证码长度
         * @param sources    验证码字符源
         * @return
         */
        public static String generateVerifyCode(int verifySize, String sources) {
            // 未设定展示源的字码,赋默认值大写字母+数字
            if (sources == null || sources.length() == 0) {
                sources = VERIFY_CODES;
            }
            int codesLen = sources.length();
            Random rand = new Random(System.currentTimeMillis());
            StringBuilder verifyCode = new StringBuilder(verifySize);
            for (int i = 0; i < verifySize; i++) {
                verifyCode.append(sources.charAt(rand.nextInt(codesLen - 1)));
            }
            return verifyCode.toString();
        }

        /**
         * 生成随机验证码文件,并返回验证码值 (生成图片形式,用的较少)
         *
         * @param w
         * @param h
         * @param outputFile
         * @param verifySize
         * @return
         * @throws IOException
         */
        public static String outputVerifyImage(int w, int h, File outputFile, int verifySize) throws IOException {
            String verifyCode = generateVerifyCode(verifySize);
            outputImage(w, h, outputFile, verifyCode);
            return verifyCode;
        }



        /**
         * 生成指定验证码图像文件
         *
         * @param w
         * @param h
         * @param outputFile
         * @param code
         * @throws IOException
         */
        public static void outputImage(int w, int h, File outputFile, String code) throws IOException {
            if (outputFile == null) {
                return;
            }
            File dir = outputFile.getParentFile();
            //文件不存在
            if (!dir.exists()) {
                //创建
                dir.mkdirs();
            }
            try {
                outputFile.createNewFile();
                FileOutputStream fos = new FileOutputStream(outputFile);
                outputImage(w, h, fos, code);
                fos.close();
            } catch (IOException e) {
                throw e;
            }
        }

        /**
         * 输出指定验证码图片流
         *
         * @param w
         * @param h
         * @param os
         * @param code
         * @throws IOException
         */
        public static void outputImage(int w, int h, OutputStream os, String code) throws IOException {
            int verifySize = code.length();
            BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            Random rand = new Random();
            Graphics2D g2 = image.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            // 创建颜色集合,使用java.awt包下的类
            Color[] colors = new Color[5];
            Color[] colorSpaces = new Color[]{Color.WHITE, Color.CYAN,
                    Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
                    Color.PINK, Color.YELLOW};
            float[] fractions = new float[colors.length];
            for (int i = 0; i < colors.length; i++) {
                colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)];
                fractions[i] = rand.nextFloat();
            }
            Arrays.sort(fractions);
            // 设置边框色
            g2.setColor(Color.GRAY);
            g2.fillRect(0, 0, w, h);

            Color c = getRandColor(200, 250);
            // 设置背景色
            g2.setColor(c);
            g2.fillRect(0, 2, w, h - 4);

            // 绘制干扰线
            Random random = new Random();
            // 设置线条的颜色
            g2.setColor(getRandColor(160, 200));
            for (int i = 0; i < 20; i++) {
                int x = random.nextInt(w - 1);
                int y = random.nextInt(h - 1);
                int xl = random.nextInt(6) + 1;
                int yl = random.nextInt(12) + 1;
                g2.drawLine(x, y, x + xl + 40, y + yl + 20);
            }

            // 添加噪点
            // 噪声率
            float yawpRate = 0.05f;
            int area = (int) (yawpRate * w * h);
            for (int i = 0; i < area; i++) {
                int x = random.nextInt(w);
                int y = random.nextInt(h);
                // 获取随机颜色
                int rgb = getRandomIntColor();
                image.setRGB(x, y, rgb);
            }
            // 添加图片扭曲
            shear(g2, w, h, c);

            g2.setColor(getRandColor(100, 160));
            int fontSize = h - 4;
            Font font = new Font("Algerian", Font.ITALIC, fontSize);
            g2.setFont(font);
            char[] chars = code.toCharArray();
            for (int i = 0; i < verifySize; i++) {
                AffineTransform affine = new AffineTransform();
                affine.setToRotation(Math.PI / 4 * rand.nextDouble() * (rand.nextBoolean() ? 1 : -1), (w / verifySize) * i + fontSize / 2, h / 2);
                g2.setTransform(affine);
                g2.drawChars(chars, i, 1, ((w - 10) / verifySize) * i + 5, h / 2 + fontSize / 2 - 10);
            }

            g2.dispose();
            ImageIO.write(image, "jpg", os);
        }

        /**
         * 随机颜色
         *
         * @param fc
         * @param bc
         * @return
         */
        private static Color getRandColor(int fc, int bc) {
            if (fc > 255) {
                fc = 255;
            }
            if (bc > 255) {
                bc = 255;
            }
            int r = fc + random.nextInt(bc - fc);
            int g = fc + random.nextInt(bc - fc);
            int b = fc + random.nextInt(bc - fc);
            return new Color(r, g, b);
        }

        private static int getRandomIntColor() {
            int[] rgb = getRandomRgb();
            int color = 0;
            for (int c : rgb) {
                color = color << 8;
                color = color | c;
            }
            return color;
        }

        private static int[] getRandomRgb() {
            int[] rgb = new int[3];
            for (int i = 0; i < 3; i++) {
                rgb[i] = random.nextInt(255);
            }
            return rgb;
        }

        private static void shear(Graphics g, int w1, int h1, Color color) {
            shearX(g, w1, h1, color);
            shearY(g, w1, h1, color);
        }

        private static void shearX(Graphics g, int w1, int h1, Color color) {

            int period = random.nextInt(2);

            boolean borderGap = true;
            int frames = 1;
            int phase = random.nextInt(2);

            for (int i = 0; i < h1; i++) {
                double d = (double) (period >> 1)
                        * Math.sin((double) i / (double) period
                        + (6.2831853071795862D * (double) phase)
                        / (double) frames);
                g.copyArea(0, i, w1, 1, (int) d, 0);
                if (borderGap) {
                    g.setColor(color);
                    g.drawLine((int) d, i, 0, i);
                    g.drawLine((int) d + w1, i, w1, i);
                }
            }

        }

        private static void shearY(Graphics g, int w1, int h1, Color color) {

            int period = random.nextInt(40) + 10; // 50;

            boolean borderGap = true;
            int frames = 20;
            int phase = 7;
            for (int i = 0; i < w1; i++) {
                double d = (double) (period >> 1)
                        * Math.sin((double) i / (double) period
                        + (6.2831853071795862D * (double) phase)
                        / (double) frames);
                g.copyArea(i, 0, 1, h1, 0, (int) d);
                if (borderGap) {
                    g.setColor(color);
                    g.drawLine(i, (int) d, i, 0);
                    g.drawLine(i, (int) d + h1, i, h1);
                }

            }


    }



}

  

 

标签:14,verifySize,int,天气,param,Color,2023,new,g2
From: https://www.cnblogs.com/Christmas77/p/17726935.html

相关文章

  • 2023.9.24 lx2080 Serdes Config
    //sedesprotocolconfiguration:ubootnoprintinfo[lx2160a]bootfailwhenSRDS_PRTCL_S2=10-NXPCommunity //sys_clock与ref_clock:系统时钟内部提供;外部参考时钟可以根据外设的频率需求设计......
  • 2023-9-24 #70 苦难与欢欣 破损后沉入往昔
    ——《碎镜》(不知道怎么标作者,放个链接吧)496ARC143FCountingSubsets注意到在背包的过程中,第一个产生\(2\)的数很特殊,假设此时左右侧均有\(k\)个\(1\)。归纳可证,背包左右侧\(1\)个数恒为\(k\),且转移形如“将中间部分复制一遍,并在两份中间插入长度\([a,2a]\)的段”......
  • Linux第二章文件管理 2023.9.24
    计算机科学与技术1班 学号:20218503姓名:曾庆玲一目录操作首先:cd/cd//切换到根目录1、创建目录mkdirswxy2、查看目录(1)pwd 显示当前所在目录(2)pwd-p显示实际工作目录(3)ls-a查看隐藏的目录与文件(4)ls-l 查看目录与文件的属性3、切换目录(1)cd不加任何路......
  • 2023 9.18~9.23 总结
    这周的比赛情况不是很好,很多题都犯了不应该犯的错误。其实很多替我都是有能力做出来的,但是不熟练或没有好好想。很多数据结构需要多打,如:ST表、dijkstra,这写数据结构虽然会写,但不能很灵活地运用。这周打了两场ZROJ的比赛,感觉都不好,以后做题还是要多多想想,把题意简化。第一场比......
  • 20211314王艺达学习笔记3
    sh编程sh脚本与C程序·C程序必须先编译链接到一个二进制可执行文件,再通过主sh的子进程运行该二进制可执行文件;sh则可直接执行行命令。·sh脚本不需要main函数。编写sh脚本shell的基本语法主要就是如何输入命令运行程序以及如何在程序之间通过shell的一些参数提供便利手段来进......
  • 20230924
    //deposit,difficulty,intention,marine,materialize,oblige,party,pilferage,profit,quotation,steamship,gooverdeposit-存款Adepositreferstoasumofmoneythatisplacedintoabankaccountorheldbyaninstitutionforsafekeeping.Itcanal......
  • 20230924天七集训测试总结
    这场考试败在策略,节奏被T1完全打乱了,导致T3甚至把题读错了(竟然有分)。按理来说是应该先把题看完的,但可能是前几次考试比较能平推的原因,这次没有先看所有题。暴力拿稳其实有很多分的。吸取一个教训吧。感觉这一整套题的思维难度都并没有那么深,但带有迷惑性且细节巨多。T1卡空......
  • 20230924学习总结
    1、DataGrip连接hive数据库DataGrip是JetBrains旗下的一款数据库管理软件,通过它能更方便的操作虚拟机中的hive数据库 依次点击+ ->数据源->ApacheHive进入配置链接界面 主机处填虚拟地址,用户密码填虚拟机账号密码(配置无误情况下仍可能连接失败,等候几分钟重试即可)2......
  • P3514 [POI2011] LIZ-Lollipop
    很神奇的题题意:给你一个由\(0\)和\(1\)组成的序列,给出\(q\)个询问,每次询问是否有原序列是否有总和为\(x\)的子段。考虑递推,但是小答案对大答案的影响不好算。考虑大区间对小区间的影响。设当前区间为\([l,r]\),总和为sum,有\(4\)种情况\(a[l]=2,a[r]=2\);这是无......
  • NOIP2023板刷记录
    目录NOIP2023板刷记录CodeforcesCodeforcesRound895(Div.3)PinelyRound2(Div.1+Div.2)A~ECodeforcesRound425(Div.2)CodeforcesRound888(Div.3)AtCoderAtCoderBeginnerContest133AtCoderBeginnerContest312NOIP2023板刷记录CodeforcesCodeforcesRou......