JAVA生成验证码

来源:互联网 发布:中考倒计时软件下载 编辑:程序博客网 时间:2024/06/05 02:33

验证码是验证用户,防止机器注册的问题的方法之一。这里博主将介绍使用Java生成验证码,验证是字母加数字格式。

生成验证码的主要代码

public class ImgFontByte {    //设置字体    public Font getFont(int fontHeight) {        try {            Font baseFont = Font.createFont(0, new ByteArrayInputStream(hex2byte(getFontByteStr())));            return baseFont.deriveFont(0, fontHeight);        } catch (Exception e) {        }        return new Font("Arial", 0, 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());            }            return b;        } catch (Exception e) {        }        return null;    }    private String getFontByteStr()    {        return ("0046004700480049004a004b004c004d004e004f0050005100520053005400550056005700580059005a005b005c005d005e005f00600061006200630010006400650066006700680069006a0000000000010000001a0001000200060001000600320025ffd90032003cff9900000000001000000070090805000707030605050505030303020503050203050506050506050505050203030503050506050505050607060506060607060505050505060706070706060303030505030605050505060706050606060706050505050506070607070606030303050802030303030606070000000a0805000808030606060606030303020603060304060606050606060606050303030603050607060606060607070607070708070606060505070707080806060304030606030706060606060707060707070807060606050507070708080606030403060802030303030606080000000b0906000909030706060606040303030604060304070607060607060606060304030603060608060606070708080707070809070606060606080808090807070304030606040806060607070808070707080907060606060608080809080707030403060903040404040707080000000c0a06000a0a04080707070704040303070407030507070806070707070706030404070306070807070707080908070808090a080707070606080908090908080404030707040807070707080908070808090a08070707060608090809090808040503070a03040404040808090000000d0b07000b0b04080808080804040403070408030508070807080807080707030404080407080908080808080a09080909090b090707070707090a090a0a08080405040808040908080808080a09080909090b090707070707090a090a0a0808040504080b030404040408080a0000000e0c07000c0c04090808080805040403080508040508080907080908080807040504080407080a08080808090a0a0909090a0b0908080807070a0a0a0b0b09090405040808050a08080808090a0a0909090a0b0908080807070a0a0a0b0b0909040504080c030505050509090b0000000f0d08000c0c050a0909090905040404090509040609080908090908090808040504090408090a090909090a0b0b090a0a0b0c0a09090808080a0b0a0c0b0a0a0406040909050a090909090a0b0b090a0a0b0c0a09090808080a0b0a0c0b0a0a040604090d04050505050a0a0c000000100d08000d0d050a0909090905050504090509040609090a08090a09090908040505090508090b090909090a0c0b0a0b0b0b0d0b09090908080b0c0b0d0c0a0a0506050909060b090909090a0c0b0a0b0b0b0d0b09090908080b0c0b0d0c0a0a050605090d04060505060a0a0c000000110e09000e0e050b0a0a0a0a060505040a060a05060a090b090a0a0a0a0a090506050a05090a0c0a0a0a0a0b0c0c0a0b0b0c0e0b0a0a0909090c0d0c0d0d0b0b0506050a0a060c0a0a0a0a0b0c0c0a0b0b0c0e0b0a0a0909090c0d0c0d0d0b0b0506050a0e04060606060b0b0d000000120f09000f0f050b0a0a0a0a060505040a060a05070b0a0b090a0b0a0b0a090506050a05090a0c0b0a0b0b0c0d0d0b0c0c0d0f0c0a0a0a09090c0d0c0e0e0c0b0507050a0a060c0b0a0b0b0c0d0d0b0c0c0d0f0c0a0a0a09090c0d0c0e0e0c0b0507050a0f04060606060b0b0e00000013100a001010060c0b0b0b0b060605050b060b05070b0b0c0a0b0c0b0b0b0a0506060b050a0b0d0b0b0b0b0c0e0d0c0c0c0e100d0b0b0b0a0a0d0e0d0f0e0c0c0607050b0b070d0b0b0b0b0c0e0d0c0c0c0e100d0b0b0b0a0a0d0e0d0f0e0c0c0607050b1005070606070c0c0f00000014110a001111060d0c0c0c0c070606050b070c05080c0b0d0a0c0c0b0c0b0a0507060c060a0c0e0c0c0c0c0d0f0e0c0d0d0e100d0b0b0b0a0a0e0f0e100f0d0d0607060c0c070e0c0c0c0c0d0f0e0c0d0d0e100d0b0b0b0a0a0e0f0e100f0d0d0608060c1105070707070d0d0f00000015120b001111060d0c0c0c0c070606050c070c06080c0c0d0b0c0d0c0c0c0b0607060c060b0c0f0c0c0c0c0d0f0f0d0e0e0f110e0c0c0c0b0b0e100e10100e0d0608060c0c070f0c0c0c0c0d0f0f0d0e0e0f110e0c0c0c0b0b0e100e10100e0d0608060c1205070707070d0d1000000016120b001212070e0d0d0d0d070706050d070d06080d0c0e0b0d0e0c0d0c0b0607070d060b0d0f0d0d0d0d0e10100d0e0e10120f0d0c0c0b0b0f100f11110e0e0708060d0d080f0d0d0d0d0e10100d0e0e10120f0d0c0c0b0b0f100f11110e0e0708060d1205080707080e0e1100000017130c001313070f0d0d0d0d080706060d080d06090e0d0e0c0d0e0d0d0d0c0608070d060c0d100d0d0e0e0f11100e0f0f10130f0d0d0d0c0c10111012110f0f0709060d0d08100d0d0e0e0f11100e0f0f10130f0d0d0d0c0c10111012110f0f0709060d1306080808080f0f1200000018140c001414070f0e0e0e0e080707060e080e06090e0d0f0c0e0f0e0e0e0d0608070e070c0e110e0e0e0e0f12110f10101114100e0e0d0d0d11121013120f0f0709070e0e08110e0e0e0e0f12110f10101114100e0e0d0d0d11121013120f0f0709070e1406080808080f0f120000000001029c01900005000002bc028a0000008f02bc028a000001c5003201030000000004000000000000000000000300000000000000000000000046726f670040002020260355ff5c0000035500a4000000010000000000000001000080000000033d02f10000600002dd0275416374696f6e204a61636b7320202020ffffffff37fffffe4143545230300000000000000001000000010000fae7a6eb5f0f3cf5000003e800000000b2050dda00000000b20549240000ff5c03280356000000030002000100000000000100000356ff38000003430000000d032800010000000000000000000000000000006b00010000006b0227001d01030009000200080040000a00000097013000030003").intern();    }}
public class ValidateCode {    //默认的高和宽    private int width = 160;    private int height = 40;    //验证码的字符数    private int codeCount = 5;    /**     * 在验证码中都会有模糊的效果,通过使字体模糊,     * 添加横线的是图片相对难以识别,这样防止机器识别     */    //设置横线的数量    private int lineCount = 150;    private String code = null;    private BufferedImage buffImg = null;    //设置随机的字符    private char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',            'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W','X', 'Y', 'Z',            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j','k', 'l', 'm',            'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w','x', 'y', 'z',            '1', '2', '3', '4', '5', '6', '7', '8', '9','0'};    public ValidateCode() {        createCode();    }    public ValidateCode(int width, int height) {        this.width = width;        this.height = height;        createCode();    }    public ValidateCode(int width, int height, int codeCount, int lineCount) {        this.width = width;        this.height = height;        this.codeCount = codeCount;        this.lineCount = lineCount;        createCode();    }    /**     * 创建验证码     */    public void createCode() {        int x = 0;        int fontHeight = 0;        int codeY = 0;        int red = 0;        int green = 0;        int blue = 0;        x = this.width / (this.codeCount + 2);        fontHeight = this.height - 2;        codeY = this.height - 4;        this.buffImg = new BufferedImage(this.width, this.height, 1);        Graphics2D g = this.buffImg.createGraphics();        Random random = new Random();        g.setColor(Color.WHITE);        g.fillRect(0, 0, this.width, this.height);        ImgFontByte imgFont = new ImgFontByte();        Font font = imgFont.getFont(fontHeight);        g.setFont(font);        for (int i = 0; i < this.lineCount; i++) {            int xs = random.nextInt(this.width);            int ys = random.nextInt(this.height);            int xe = xs + random.nextInt(this.width / 8);            int ye = ys + random.nextInt(this.height / 8);            red = random.nextInt(255);            green = random.nextInt(255);            blue = random.nextInt(255);            g.setColor(new Color(red, green, blue));            g.drawLine(xs, ys, xe, ye);        }        StringBuffer randomCode = new StringBuffer();        for (int i = 0; i < this.codeCount; i++) {            String strRand = String.valueOf(this.codeSequence[random.nextInt(this.codeSequence.length)]);            red = random.nextInt(255);            green = random.nextInt(255);            blue = random.nextInt(255);            g.setColor(new Color(red, green, blue));            g.drawString(strRand, (i + 1) * x, codeY);            randomCode.append(strRand);        }        this.code = randomCode.toString();    }    /**     * 将生成的验证码写入指定的路径     * @param path     * @throws IOException     */    public void write(String path)            throws IOException {        OutputStream sos = new FileOutputStream(path);        write(sos);    }    /**     * 将生成的验证码写入指定的输出流     * @param sos     * @throws IOException     */    public void write(OutputStream sos)            throws IOException {        ImageIO.write(this.buffImg, "png", sos);        sos.close();    }    public BufferedImage getBuffImg() {        return this.buffImg;    }    //得到生成的验证码    public String getCode() {        return this.code;    }}

调用代码将生成的验证码图片保存到本地

ValidateCode validateCode = new ValidateCode(200,50,6,150);//将得到的验证码图片保存到本地String path = "D:/"+new Date().getTime()+".png";System.out.println(validateCode.getCode());validateCode.write(path);

所生成的验证码
这里写图片描述
生成的验证码图片,这里验证码的字符数量和干扰的线条数都可以自己设置
这里写图片描述

在网页上显示

居然是要在网页显示,当然需要项目是web项目。这里博主就不讲解web项目的创建,直接输出验证码。

 @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {        /**         * 将生成的验证码图片在页面显示         */        resp.setContentType("image/jpeg");        resp.setHeader("Pragma", "no-cache");        resp.setHeader("Cache-Control", "no-cache");        resp.setDateHeader("Expires", 0L);        HttpSession session = req.getSession();        ValidateCode validateCode = new ValidateCode(150,40,5,150);        //将验证码保存到session中,方便验证        session.setAttribute("code",validateCode);        validateCode.write(resp.getOutputStream());    }

页面代码

<img src="${pageContext.request.contextPath}/ValidataCodeServlet">

这里写图片描述

另外可以将该项目打包成jar包,方便使用。这里提供一个博主打包的jar包:jar包下载地址