java生成随机校验码图片

RandomValidateCode.java

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

/**
 * @see https://my.oschina.net/cnzxzc/blog/657088
 * 
 * @author: 
 * @date: 2016年9月18日 下午12:20:53
 */
public class RandomValidateCode {
    private static Random random = new Random();
    private static int width = 70;//图片宽
    private static int height = 30;//图片高
    private static int lineNum = 5;//干扰线数量
    private static int pointNum = 30;//噪点数量
    private static int stringNum = 4;//随机产生字符数量
    private static Font font = new Font("Times New Roman", Font.ROMAN_BASELINE, 20);
    private static char[] index = {'a','A','0'};//验证码索引
    /**
     * 生成验证码字符串
     */
    public static char[] getValidateString(){
        char[] c = new char[stringNum];
        for(int i = 0; i<stringNum; i++) {
            int offset = random.nextInt(3);
            switch (offset) {
                case 0:
                case 1:
                    c[i] = (char) ((int) index[offset] + random.nextInt(26));
                    break;
                case 2:
                    c[i] = (char) ((int) index[offset] + random.nextInt(10));
                    break;
            }
        }
        return c;
    }
    /**
     * 生成图片并输出
     * */
    public static String makeValidateImage(OutputStream out){
        //验证码
        char[] code = getValidateString();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        g.setFont(font);
        g.setColor(Color.GREEN);
        g.drawRect(0, 0, width, height);
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, width, height);
        //绘制干扰线
        drowLine(g);
        //绘制噪点
        drowPoint(g);
        //绘制随机字符
        drowString(g, code);
        try {
            ImageIO.write(image, "JPEG",  out);
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            g.dispose();
        }
        return new String(code);
    }
    /**
     * 绘制字符串
     * */
    private static void drowString(Graphics g, char[] text){
        g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121)));
        int x = random.nextInt(width);
        int y = random.nextInt(height);
        //防止画图画出范围
        while(x>25){ x -= 20; }
        while(y<13){ y += 10; }
        g.drawChars(text, 0, stringNum, x, y);
    }
    /**
     * 绘制干扰线
     */
    private static void drowLine(Graphics g){
        for(int i = 0; i < lineNum; i++) {
            g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121)));
            int x1 = random.nextInt(width);
            int y1 = random.nextInt(height);
            int x2 = random.nextInt(width);
            int y2 = random.nextInt(height);
            g.drawLine(x1, y1, x2, y2);
        }
    }
    /**
     * 绘制噪声点
     */
    private static void drowPoint(Graphics g){
        for(int i = 0; i < pointNum; i++) {
            g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121)));
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            g.drawLine(x, y, x, y);
        }
    }
    
    public static void main(String[] args) {
    	//验证码
        char[] text = getValidateString();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        g.setFont(font);
        g.setColor(Color.GREEN);
        g.drawRect(0, 0, width, height);
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, width, height);
        //绘制干扰线
        drowLine(g);
        //绘制噪点
        drowPoint(g);
        //绘制随机字符
        drowString(g, text);
        try {
            ImageIO.write(image, "JPEG",  new FileOutputStream("C:/Users/Administrator/Desktop/text.jpg"));
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            g.dispose();
        }
	}
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值