首页 > 分享 > 生成数字相加验证码图片并验证

生成数字相加验证码图片并验证

package com.xz.common;  

   import java.awt.Color;  
import java.awt.Font;  
import java.awt.Graphics2D;  
import java.awt.image.BufferedImage;  
import java.io.ByteArrayInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.OutputStream;  
import java.util.Random;  

   import javax.imageio.ImageIO;  

   /** 
 * 数字相加类型验证码生成器 
 *  
 */  
public class CreateVer {  
    // 图片的宽度。  
    private int width = 120; //120 
    // 图片的高度。  
    private int height = 40;  
    // 验证码字符个数  
    private int codeCount = 2;  
    // 验证码干扰线数  
    private int lineCount = 25;  
    // 验证码  
    private String code = null;  
    // 验证码图片Buffer  
    private BufferedImage buffImg = null;  
    //验证数字和
    private Integer sum = 0;

       private char[] codeSequence = { '1','2', '3', '4', '5', '6', '7', '8', '9' }; 
    // 生成随机数  
    private Random random = new Random();  

       public CreateVer() {  
        this.createCode();  
    }  

       /** 
     *  
     * @param width 
     *            图片宽 
     * @param height 
     *            图片高 
     */  
    public CreateVer(int width, int height) {  
        this.width = width;  
        this.height = height;  
        this.createCode();  
    }  

       /** 
     *  
     * @param width 
     *            图片宽 
     * @param height 
     *            图片高 
     * @param codeCount 
     *            字符个数 
     * @param lineCount 
     *            干扰线条数 
     */  
    public CreateVer(int width, int height, int codeCount, int lineCount) {  
        this.width = width;  
        this.height = height;  
        this.codeCount = codeCount;  
        this.lineCount = lineCount;  
        this.createCode();  
    }  

       public void createCode() {  
        int codeX = 0;  
        int fontHeight = 0;  
        fontHeight = height - 5;// 字体的高度  
        codeX = width / (codeCount+3);// 每个字符的宽度  

           // 图像buffer  
        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
        Graphics2D g = buffImg.createGraphics();  

           // 将图像填充为白色  
        g.setColor(Color.WHITE);  
        g.fillRect(0, 0, width, height);  

           // 创建字体  
        ImgFontByte imgFont = new ImgFontByte();  
        Font font = imgFont.getFont(fontHeight);  
        g.setFont(font);  

           // 绘制干扰线  
        for (int i = 0; i < lineCount; i++) {  
            int xs = getRandomNumber(width);  
            int ys = getRandomNumber(height);  
            int xe = xs + getRandomNumber(width / 8);  
            int ye = ys + getRandomNumber(height / 8);  
            g.setColor(getRandomColor());  
            g.drawLine(xs, ys, xe, ye);  
        }  

           StringBuffer randomCode = new StringBuffer();  
        // 随机产生验证码字符  
        int index = 1;
        for (int i = 0; i < codeCount; i++) {  
            String strRand = String.valueOf(codeSequence[random  
                    .nextInt(codeSequence.length)]);  
            sum += Integer.parseInt(strRand);
            // 设置字体颜色  
            g.setColor(getRandomColor());  
            // 设置字体位置  
            g.drawString(strRand, (index) * codeX,  
                    getRandomNumber(height / 2) + 25); 
            index ++;
            randomCode.append(strRand);  
            if(!(i == codeCount-1)){
                g.drawString("+", (index) * codeX,  
                        getRandomNumber(height / 2) + 25); 
                randomCode.append("+"); 
                index ++;
            }else{
                g.drawString("=", (index) * codeX,  
                        getRandomNumber(height / 2) + 25); 
                randomCode.append("="); 
                index ++;
            }
        }  
        code = randomCode.toString();  
    }  

       /** 获取随机颜色 */  
    private Color getRandomColor() {  
        int r = getRandomNumber(255);  
        int g = getRandomNumber(255);  
        int b = getRandomNumber(255);  
        return new Color(r, g, b);  
    }  

       /** 获取随机数 */  
    private int getRandomNumber(int number) {  
        return random.nextInt(number);  
    }  

       public void write(String path) throws IOException {  
        OutputStream sos = new FileOutputStream(path);  
        this.write(sos);  
    }  

       public void write(OutputStream sos) throws IOException {  
        ImageIO.write(buffImg, "png", sos);  
        sos.close();  
    }  

       public BufferedImage getBuffImg() {  
        return buffImg;  
    }  

       public String getCode() {  
        return code;  
    }  
    public Integer getSum() {  
        return sum;  
    } 

       /** 字体样式类 */  
    class ImgFontByte {  
        public Font getFont(int fontHeight) {  
            try {  
                Font baseFont = Font.createFont(Font.TRUETYPE_FONT,  
                        new ByteArrayInputStream(hex2byte(getFontByteStr())));  
                return baseFont.deriveFont(Font.PLAIN, fontHeight);  
            } catch (Exception e) {  
                return new Font("Arial", Font.PLAIN, fontHeight);  
            }  
        }  

           private byte[] hex2byte(String str) {  
            if (str == null)  
                return null;  
            str = str.trim();  
            int len = str.length();  
            if (len == 0 || len % 2 == 1)  
                return null;  

               byte[] b = new byte[len / 2];  
            try {  
                for (int i = 0; i < str.length(); i += 2) {  
                    b[i / 2] = (byte) Integer.decode(  
                            "0x" + str.substring(i, i + 2)).intValue();  

相关知识

PHP数字字母汉字验证码实现
tensorflow生成图片标签
发送验证码的时候就说系统繁忙,= = 怎么办
VUE项目编写验证码
输验证码扣299元 消费者被困贷款“会员劫”
验证码图片识别训练记录
在线文字生成图片
即创
求Sn=a+aa+aaa+aaaa+aaaaa的前5项之和,其中a是一个数字,
steam“您对 CAPTCHA 的响应似乎无效。请在下方重新验证您不是机器人

网址: 生成数字相加验证码图片并验证 https://m.huajiangbk.com/newsview764194.html

所属分类:花卉
上一篇: 关于举办河南省第五届“LA花之都
下一篇: 花创意字体ps艺术字体