Java实现图片验证码功能,存储到io流之中

  1 package cn.itcast.img;
  2 import java.awt.BasicStroke;
  3 import java.awt.Color;
  4 import java.awt.Font;
  5 import java.awt.Graphics2D;
  6 import java.awt.Image;
  7 import java.awt.image.BufferedImage;
  8 import java.io.FileNotFoundException;
  9 import java.io.FileOutputStream;
 10 import java.io.IOException;
 11 import java.io.OutputStream;
 12 import java.util.Random;
 13 import javax.imageio.ImageIO;
 14 public class VerifyCode {
 15     private int w=70;
 16     private int h=35;
 17     private Random r=new Random();
 18     //可选字体
 19     private String[] fontNames={"宋体","黑体","楷体_GB2312"};
 20     //可选字符
 21     private String codes="23456789abcdefghjkmnopqrstuvwsyzABCDEFGHJKMNPQRSTUVWXYZ";
 22     //背景色
 23     private Color bgColor=new Color(255,255,255);
 24     //验证码上的文本
 25     private String text;    
 26     //生成随机颜色
 27     private Color RandomColor(){
 28         int red=r.nextInt(150);
 29         int green=r.nextInt(150);
 30         int blue=r.nextInt(150);
 31         return new Color(red, green, blue);
 32     }    
 33     //生成随机字体样式
 34     private Font RandomFont(){
 35         int index=r.nextInt(fontNames.length);
 36         String fontName=fontNames[index];
 37         //生成随机的样式,0为无样式,1为粗体,2为斜体,3为粗体+斜体
 38         int style=r.nextInt(4);
 39         //生成随机字号24-28
 40         int size=r.nextInt(5)+24;
 41         return new Font(fontName, style, size);
 42     }    
 43     //创建BufferedImage
 44     private BufferedImage createImage(){
 45         BufferedImage image=new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
 46         Graphics2D g2=(Graphics2D)image.getGraphics();
 47         g2.setColor(this.bgColor);
 48         g2.fillRect(0, 0, w, h);
 49         return image;
 50     }    
 51     //画干扰线
 52     private void drawLine(BufferedImage image){
 53         //一共画3条
 54         int num=2;
 55         Graphics2D g2=(Graphics2D)image.getGraphics(); 
 56         //生成两个点的坐标,即4个值
 57         for (int i = 0; i < num; i++) {
 58             int x1=r.nextInt(w);
 59             int y1=r.nextInt(h);
 60             int x2=r.nextInt(w);
 61             int y2=r.nextInt(h);
 62             g2.setStroke(new BasicStroke(1.5F));
 63             //干扰线是蓝色
 64             g2.setColor(Color.BLUE);
 65             g2.drawLine(x1, y1, x2, y2);
 66         }
 67     }     
 68     //随机生成一个字符
 69     private char randomChar(){
 70         int index=r.nextInt(codes.length());
 71         return codes.charAt(index);
 72     }
 73     //调用这个方法得到验证码
 74     public BufferedImage getImage(){
 75         //创建图片缓冲区
 76         BufferedImage image=createImage();
 77         //得到绘制环境
 78         Graphics2D g2=(Graphics2D)image.getGraphics();
 79         //装用来载生成的验证码文本
 80         StringBuffer sb=new StringBuffer();
 81         //向图片中画4个字符
 82         for (int i = 0; i < 4; i++) {
 83             //随机生成一个字符
 84             String s=randomChar()+"";
 85             //把字符添加到sb中
 86             sb.append(s);
 87             //设置当前字符的x轴坐标 
 88             float x=i*1.0F*w/4;
 89             //设置随机字体
 90             g2.setFont(RandomFont());
 91             //设置随机颜色
 92             g2.setColor(RandomColor());
 93             //画图
 94             g2.drawString(s, x, h-5);
 95         }
 96         //把生成的字符串赋值给this.test
 97         this.text=sb.toString();
 98         //添加干扰线
 99         drawLine(image);
100         return image;
101     }
102     //返回验证码图片上的文本
103     public String getText(){
104         return text;
105     }
106     //保存图片到指定的输出流
107     public static void output (BufferedImage image,OutputStream out) throws IOException{
108         ImageIO.write(image, "JPEG", out);
109     }
110 }
View Code

猜你喜欢

转载自www.cnblogs.com/qiscore/p/12184386.html