生成图片验证码

来源:互联网 发布:王德民 知乎 编辑:程序博客网 时间:2024/06/06 17:06



public class SecurityCodeUtil {    private final static Logger logger                  = LoggerFactory.getLogger(SecurityCodeUtil.class);    // 验证码图片的宽度。    private final static int    WIDTH                   = 120;    // 验证码图片的高度。    private final static int    HEIGHT                  = 40;    // 验证码干扰线数    private final static int    INTERFERENCE_LINE_COUNT = 10;    private final static String RANDOM_CHARS            = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";    private final static int    MIN_WIDTH               = 20;    // 得到随机字符    public static String randomString(int n) {        Random random = new Random(System.currentTimeMillis());        StringBuffer rnd = new StringBuffer();        String chars = RANDOM_CHARS;        int len = chars.length() - 1;        double r;        for (int i = 0; i < n; i++) {            r = (random.nextDouble()) * len;            rnd.append(chars.charAt((int) r));        }        return rnd.toString();    }    public static byte[] drawImg(String securityCode) {        // 16k        ByteArrayOutputStream out = new ByteArrayOutputStream(16 * 1024);        Graphics2D g = null;        try {            int fontWidth = WIDTH / securityCode.length();// 字体的宽度            if (fontWidth < MIN_WIDTH) {                throw new RuntimeException("字符过多");            }            int fontHeight = HEIGHT - 5;// 字体的高度            int codeY = HEIGHT - 8;            // 图像buffer            BufferedImage buffImg = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);            // Graphics g = buffImg.getGraphics();            g = buffImg.createGraphics();            // 设置背景色            // g.setColor(getRandColor(200, 250));            g.setColor(new Color(255, 255, 255));            g.fillRect(0, 0, WIDTH, HEIGHT);            g.setColor(new Color(204, 204, 204));            g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);            // 设置字体            // Font font = getFont(fontHeight);            Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);            g.setFont(font);            Random random = new Random();            // 设置干扰线            for (int i = 0; i < INTERFERENCE_LINE_COUNT; i++) {                int xs = random.nextInt(WIDTH);                int ys = random.nextInt(HEIGHT);                int xe = xs + random.nextInt(WIDTH);                int ye = ys + random.nextInt(HEIGHT);                g.setColor(randColor(1, 255));                g.drawLine(xs, ys, xe, ye);            }            // 添加噪点            float yawpRate = 0.01f;// 噪声率            int area = (int) (yawpRate * WIDTH * HEIGHT);            area = 8;            for (int i = 0; i < area; i++) {                int x = random.nextInt(WIDTH);                int y = random.nextInt(HEIGHT);                buffImg.setRGB(x, y, random.nextInt(255));            }            // 得到随机字符            char[] codeChars = securityCode.toCharArray();            for (int i = 0; i < codeChars.length; i++) {                char strRand = codeChars[i];                g.setColor(randColor(1, 255));                // g.drawString(a,x,y);                // a为要画出来的东西,x和y表示要画的东西最左侧字符的基线位于此图形上下文坐标系的 (x, y) 位置处                g.drawString(String.copyValueOf(new char[] { strRand }), i * fontWidth + 1, codeY);            }            ImageIO.write(buffImg, "JPEG", out);            return out.toByteArray();        } catch (Exception e) {            logger.error("验证码生产失败!", e);        } finally {            if (g != null) {                g.dispose();            }            try {                out.close();            } catch (IOException e) {                logger.error("", e);            }        }        return new byte[0];    }    /**     * 随机颜色     *     * @param fc 给定范围获得随机颜色     * @param bc 给定范围获得随机颜色     * @return     */    private static Color randColor(int fc, int bc) {        Random random = new Random(System.currentTimeMillis());        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);    }}


对应的Response只要设置返回类型就行


response.setContentType("image/jpeg");                response.addHeader("Accept-Ranges", "bytes");



原创粉丝点击