import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletResponse; public class RandImgCreater { //默认字符列表 private static final String CODE_LIST = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; //验证码图片高度 private static final int HEIGHT = 20; //默认字符数 private static final int FONT_NUM = 4; //生成验证码图片存放位置 private HttpServletResponse response; //宽度 private int width = 0; //实际字符个数 private int iNum = 0; //实际字符列表 private String codeList = ""; //绘制背景 private boolean drawBgFlag = false; //R private int rBg = 0; //G private int gBg = 0; //B private int bBg = 0; //构造函数(自定义存放位置) public RandImgCreater(HttpServletResponse response) { this.response = response; this.width = 13 * FONT_NUM + 12; this.iNum = FONT_NUM; this.codeList = CODE_LIST; } //构造函数(自定义存放位置,字符数) public RandImgCreater(HttpServletResponse response,int iNum) { this.response = response; this.width = 13 * iNum + 12; this.iNum = iNum; this.codeList = CODE_LIST; } //构造函数(自定义存放位置,字符数,字符列表) public RandImgCreater(HttpServletResponse response, int iNum, String codeList) { this.response = response; this.width = 13 * iNum + 12; this.iNum = iNum; this.codeList = codeList; } //绘制验证码 public String createRandImage() { //1.生成BufferedImage对象,存放图片 BufferedImage image = new BufferedImage(width, HEIGHT, BufferedImage.TYPE_INT_RGB); //2.获得BufferedImage对象的画笔 Graphics g = image.getGraphics(); //生成随机数生成器 Random random = new Random(); //开始绘制背景 if (drawBgFlag) { //设置画笔颜色 g.setColor(new Color(rBg, gBg, bBg)); //绘制矩形 g.fillRect(0, 0, width, HEIGHT); } else { //设置画笔颜色(随机) g.setColor(getRandColor(200, 250)); //绘制矩形 g.fillRect(0, 0, width, HEIGHT); //循环155次生成随机干扰线条 for (int i = 0; i < 155; i++) { // 设置颜色 g.setColor(getRandColor(140, 200)); //起始位置x int x = random.nextInt(width); //起始位置y int y = random.nextInt(HEIGHT); //结束位置x1 int xl = random.nextInt(12); //结束位置y1 int yl = random.nextInt(12); //绘制线条 g.drawLine(x, y, x + xl, y + yl); } } //设置画笔字体、样式、大小 g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); //要返回的字符串 String sRand = ""; //循环内随机生成字符 for (int i = 0; i < iNum; i++) { //随机生成字符起始位置 int rand = random.nextInt(codeList.length()); //从字符起始位置截取1位字符 String strRand = codeList.substring(rand, rand + 1); //把新生成的字符与旧字符连接(返回用) sRand += strRand; //设置前景色(字体颜色) g.setColor(new Color(20 + random.nextInt(110), 20 + random .nextInt(110), 20 + random.nextInt(110))); //绘制字符,第一个字符从0位置开始,每个字符相隔13像素 g.drawString(strRand, 13 * i + 6, 16); } //释放资源 g.dispose(); try { //把图片输出到相应位置 ImageIO.write(image, "JPEG", response.getOutputStream()); } catch (IOException e) { } //返回验证码的字符 return sRand; } //设置背景色(R,G,B颜色) public void setBgColor(int r, int g, int b) { drawBgFlag = true; this.rBg = r; this.gBg = g; this.bBg = b; } //随机生成颜色 private Color getRandColor(int fc, int bc) { Random random = new Random(); 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); } }
效果: