随机生成图片验证码

java生成随机验证码

直接贴代码,注释的很清楚了。

VerifyCode.java

package cn.testone.image;

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

import javax.imageio.ImageIO;

public class VerifyCode {
    private int w = 150;
    private int h = 70;
    private Random r = new Random();
    //可选字体,随意添加
    private String[] fontNames = {"黑体","宋体","华文楷体","微软雅黑","楷体_GB2312"};
    //可选字符,剔除掉了数字1,0,小写字母i,l,大写字母I,O,容易混淆
    private String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
    //背景颜色
    private Color bgColor = new Color(255,255,255);
    //验证码的真实文本 
    private String text;

    //生成随机颜色
    private Color randomColor(){
        int red = r.nextInt(150);
        int green = r.nextInt(150);
        int blue = r.nextInt(150);
        return new Color(red,green,blue);
    }


    //生成随机字体
    private Font randomFont(){
        int index = r.nextInt(fontNames.length);
        String fontName = fontNames[index];
        int style = r.nextInt(4); //0(无) 1(粗体) 2(斜体) 3(粗+斜)
        int size = r.nextInt(5) +48;    //字体大小 
        return new Font(fontName, style, size); 
    }

    //生成干扰线
    private void drawLine (BufferedImage image) {
        int num = 3;    
        Graphics2D g2 = (Graphics2D)image.getGraphics(); //绘制环境
        for(int i = 0; i < num; i++){   //随机生成坐标
            //一条线通过两个点的坐标,即四个参数确定
            int x1 = r.nextInt(w);
            int y1 = r.nextInt(h);
            int x2 = r.nextInt(w);
            int y2 = r.nextInt(h); 
            g2.setStroke(new BasicStroke(1.5F));
            g2.setColor(Color.BLUE);    
            g2.drawLine(x1, y1, x2, y2); //画线
        }
    }


    //随机生成一个字符
    private char randomChar(){
        int index = r.nextInt(codes.length());
        return codes.charAt(index);
    }



    //创建BufferedIMage
    private BufferedImage createImage(){
        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        g2.setColor(this.bgColor);
        g2.fillRect(0, 0, w, h);
        return image;
    }

    //得到验证码
    public BufferedImage getImage(){
        BufferedImage image = createImage();//创建图片缓冲区
        Graphics2D g2 = (Graphics2D)image.getGraphics();//得到绘制环境
        StringBuilder sb = new StringBuilder();//装载生成文本
        //向图片中绘制四个字符
        for(int i = 0; i < 4; i++){
            String s = randomChar() + "";//随机生成一个字符
            sb.append(s);   //添加进去
            float x = i * 1.0F * w/4; //设置字符X轴坐标
            g2.setFont(randomFont());   
            g2.setColor(randomColor());
            g2.drawString(s,x,h-15);    //画图,字符,X轴,Y轴
        }
        this.text = sb.toString();  //正确验证码
        drawLine(image); //干扰线
        return image;
    }

    //返回正确文本
    public String getText(){
        return text;
    }

    //保存图片到指定位置
    public static void output(BufferedImage image, OutputStream out) 
            throws IOException {
        ImageIO.write(image, "JPEG", out);
    }
}

Demo.java

package cn.testone.image;

import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.Test;

public class Demo1 {
    @Test
    public void fun() throws FileNotFoundException, IOException{
        VerifyCode vc = new VerifyCode();
        BufferedImage bi = vc.getImage();
        //这里填写自己想要存放的路径
        VerifyCode.output(bi, new FileOutputStream("/Users/wanglei/Desktop/aaa.jpg"));
        System.out.println(vc.getText());
    }
}

结果是会在指定目录下,生成一个图片验证码,并且在控制台打印正确的字符。

随机生成图片验证码_第1张图片

这里写图片描述

我们再来一次,还挺好玩:
随机生成图片验证码_第2张图片

这里写图片描述

你可能感兴趣的:(JavaWeb,验证码,图片,java)