1 package com.javaweb.utils; 2 3 import java.awt.BasicStroke; 4 import java.awt.Color; 5 import java.awt.Font; 6 import java.awt.Graphics; 7 import java.awt.Graphics2D; 8 import java.awt.image.BufferedImage; 9 import java.io.IOException; 10 import java.io.OutputStream; 11 import java.util.Random; 12 13 import javax.imageio.ImageIO; 14 /** 15 * 一次性图片验证码程序 16 * @author Administrator 17 * 18 */ 19 public class ValidateCode { 20 private int width = 70; // 图片宽度 21 private int height = 35; // 图片高度 22 private Color bgcolor = new Color(240,240,240); // 背景颜色 23 private Random random = new Random(); //用于生成伪随机数流 24 private StringBuilder code = new StringBuilder(4); 25 26 /** 27 * 创建一张内存图片 28 * @return 29 */ 30 private BufferedImage createImage(){ 31 BufferedImage img = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); 32 // 设置画笔颜色 33 img.getGraphics().setColor(bgcolor); 34 // 填充一个和图片一样大小的矩形 相当于改变背景颜色 35 img.getGraphics().fillRect(0, 0, width, height); 36 return img; 37 } 38 //随机颜色 39 private Color randomColor(){ 40 int r = random.nextInt(256); 41 int g = random.nextInt(256); 42 int b = random.nextInt(256); 43 return new Color(r,g,b); 44 } 45 //随机大小 46 private String[] fontName = {"宋体","华文楷体","黑体","华文新魏","华文隶书","微软雅黑","楷体_GB2312"}; 47 private int[] fontSize = {24,25,26,27,28}; 48 private Font randomFont(){ 49 int index = random.nextInt(fontName.length);// 获取fontName随机下标 50 String name = fontName[index]; 51 int style = random.nextInt(4);// style范围 0,1,2,3 52 index = random.nextInt(fontSize.length);// 获取fontSize随机下标 53 int size = fontSize[index]; 54 return new Font(name,style,size); 55 } 56 //随机字符 57 private String strs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 58 private String randomChar(){ 59 int index = random.nextInt(strs.length());// 每个字符的索引 60 return strs.charAt(index) + "";// 任何字符加上""都变字符串 61 } 62 // 设置干扰线 63 private void setLines(BufferedImage img){ 64 Graphics2D g = (Graphics2D)img.getGraphics(); 65 g.setColor(Color.BLACK);// 设置干扰线颜色 66 g.setStroke(new BasicStroke(1.5F));// 设置干扰线大小 67 for(int i=0;i<5;i++){ 68 int x1 = random.nextInt(width); 69 int y1 = random.nextInt(height); 70 int x2 = random.nextInt(width); 71 int y2 = random.nextInt(height); 72 g.drawLine(x1, y1, x2, y2); 73 } 74 } 75 /** 76 * 得到图片 77 * @return 78 */ 79 public BufferedImage getImage(){ 80 /** 81 * 1.得到图片 82 * 2.随机字符,随机大小,随机颜色,随机字体 83 * 3.返回 84 */ 85 // 获取内存图片 86 BufferedImage img = createImage(); 87 Graphics g = img.getGraphics(); 88 for(int i=0;i<4;i++){ 89 String ch = this.randomChar(); 90 code.append(ch); 91 g.setColor(this.randomColor()); 92 g.setFont(this.randomFont()); 93 g.drawString(ch, width/4*i, height-5); 94 } 95 this.setLines(img);// 画干扰线 96 return img; 97 } 98 /** 99 * 该方法必须在getImage()之后 100 * 返回生成验证吗的字符串 101 * @return 102 */ 103 public String getCode(){ 104 return code.toString(); 105 } 106 /** 107 * 保存图片 108 * @param img 109 * @param out 110 * @throws IOException 111 */ 112 public void saveImage(BufferedImage img, OutputStream out) throws IOException{ 113 ImageIO.write(img, "JPEG", out); 114 } 115 }
测试代码:
1 public class ValidateCodeTest { 2 3 @Test 4 public void fun() throws FileNotFoundException, IOException{ 5 ValidateCode vc = new ValidateCode(); 6 BufferedImage img = vc.getImage(); 7 String code = vc.getCode(); 8 System.out.println(code); 9 FileOutputStream fos = new FileOutputStream("E:/images/vcode.jpg"); 10 vc.saveImage(img, fos); 11 fos.close(); 12 } 13 }
原文:http://www.cnblogs.com/jonny-xu/p/6397563.html