【工具类】图片验证码

java 后台生成图片验证码,返回base64格式字符串

// ====生成二维码工具方法=======================================//
    /**
     * 使用方法
     * import java.awt.Graphics2D;
       import java.awt.image.BufferedImage;
       
       BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) image.getGraphics();
        //1、设置背景色
        Utils.setBackGround(g,width,height);
        //2、设置边框
        Utils.setBorder(g,width,height);
        //3、画干扰线
        Utils.drawRandomLine(g,width,height);
        //4、写随机数
        String verifycode = Utils.drawRandomNum(g);
        //5、写给浏览器
        g.dispose();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(image, "JPEG", bos);
        byte[] buf = bos.toByteArray();
        String img = Base64Util.encode(buf);// "/9j/4AAQSkZJRgABAgA" --图片base64
        bos.close();
        return img;
     */
    /**
     * description 生成图形验证码
     * @param width 图片宽度 默认96
     * @param height 图片高度 默认42
     * @return 图片base64 ,img文件字符串 "/9j/4AAQSkZJRgABAgA" 去除了“data:image/jpeg;base64,” 部分
     * @author 
     * @date 2019年9月20日
     */
    public static String imgVerifycode(int width,int height) throws Exception{
        if(width < 96){
            width = 96;
        }
        if(height < 42){
            height = 42;
        }
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) image.getGraphics();
        //1、设置背景色
        setBackGround(g,width,height);
        //2、设置边框
        setBorder(g,width,height);
        //3、画干扰线
        drawRandomLine(g,width,height);
        //4、写随机数    该随机数可返回
        String verifycode = drawRandomNum(g);
        //5、写给浏览器
        g.dispose();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(image, "JPEG", bos);
        byte[] buf = bos.toByteArray();
        String img = Base64Util.encode(buf);// "/9j/4AAQSkZJRgABAgA" --图片base64
        bos.close();
        return img;
    }
    // 设置背景颜色
    public static void setBackGround(Graphics g, Integer width, Integer height) {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, width, height);
        for (int i = 0; i < 120; i++) {
            int x = (int) (Math.random() * width);
            int y = (int) (Math.random() * height);
            int red = (int) (Math.random() * 255);
            int green = (int) (Math.random() * 255);
            int blue = (int) (Math.random() * 255);
            g.setColor(new Color(red, green, blue));
            g.drawOval(x, y, 1, 0);
        }
    }
    // 设置边框
    public static void setBorder(Graphics g, Integer width, Integer height) {
        g.setColor(Color.LIGHT_GRAY);
        g.drawRect(1, 1, width - 2, height - 2);
    }
    // 画干扰线
    public static void drawRandomLine(Graphics g, Integer width, Integer height) {
        g.setColor(Color.GREEN);
        for (int i = 0; i < 5; i++) {
            int x1 = new Random().nextInt(width);
            int y1 = new Random().nextInt(height);
            int x2 = new Random().nextInt(width);
            int y2 = new Random().nextInt(height);
            g.drawLine(x1, y1, x2, y2);
        }
    }
    // 产生随机数
    public static String drawRandomNum(Graphics2D g) {
        // 设置字体颜色
        g.setColor(Color.RED);
        // 设置字体样式
        g.setFont(new Font("宋体", Font.BOLD, 26));
        String rand4 = "";
        int x = 5;// 字体的位置
        for (int i = 0; i < 4; i++) {// 产生4个随机字符
            int degree = new Random().nextInt() % 30;// 旋转角度
            int index = new Random().nextInt(STR_NUM.length());
            String ch = STR_NUM.charAt(index) + "";
            rand4 += ch;
            g.rotate(degree * Math.PI / 180, x, 20);
            g.drawString(ch, x, 30);
            g.rotate(-degree * Math.PI / 180, x, 20);
            x += 24;// 字体位置右移15px
        }
        return rand4;
    }
    // ====生成二维码工具方法=======================================//

猜你喜欢

转载自www.cnblogs.com/itxs/p/12066588.html
今日推荐