1 importjava.awt.BasicStroke;2 importjava.awt.Color;3 importjava.awt.Font;4 importjava.awt.Graphics2D;5 importjava.awt.image.BufferedImage;6 importjava.io.FileNotFoundException;7 importjava.io.IOException;8 importjava.io.OutputStream;9 importjava.util.Random;10
11 importjavax.imageio.ImageIO;12
13 public classVerifyCode {14 private int width = 90;15 private int high = 40;16 private Random random = newRandom();17 private String[] fonts = {"宋体", "微软雅黑", "TimesRoman", "Cambria"};18 private String chars = "1234567890";19 private String operator = "+-x";20 private Color backColor =Color.white;21 privateString text;22 private intno1;23 private intno2;24 privateString op;25
26 //获取随机颜色
27 privateColor randomColor()28 {29 int red = random.nextInt(150);30 int green = random.nextInt(150);31 int blue =random.nextInt(150);32 return newColor(red,green,blue);33 }34
35 //method:获取随机字体
36 privateFont randomFont()37 {38 int index =random.nextInt(fonts.length);39 String fontName =fonts[index];40 int style = random.nextInt(4);41 int size = 24+random.nextInt(4);42
43 return newFont(fontName,style,size);44 }45 //method:获取随机操作符号
46 privateString randomOperator()47 {48 int index = random.nextInt(3);49 return operator.charAt(2)+"";50 }51
52 //method:获取随机数字
53 privateString randomNum()54 {55 int index =random.nextInt(chars.length());56 return chars.charAt(index)+"";57 }58
59 //method:添加干扰线
60 private voiddrawLine(BufferedImage image)61 {62 int num = 3;63 Graphics2D graphic =(Graphics2D)image.getGraphics();64 for(int i=0;i
76 private void drawString(Graphics2D graphics,String s,floatposition)77 {78 graphics.setColor(randomColor());79 graphics.setFont(randomFont());80 graphics.drawString(s,position,high -5);81 }82
83 //获取图片缓存
84 publicBufferedImage getImage()85 {86 BufferedImage bi = newBufferedImage(width,high,BufferedImage.TYPE_INT_RGB);87 Graphics2D g2 =(Graphics2D)bi.getGraphics();88 g2.setColor(this.backColor);89 g2.fillRect(0, 0, 90, 40);90 StringBuilder sb = newStringBuilder();91
92 //获取操作数s1,绘制
93 String s1 =randomNum();94 this.no1 =Integer.parseInt(s1);95 float p1 = 5.0F;96 sb.append(s1);97 drawString(g2,s1,p1);98
99
100 //获取操作符oper,绘制
101 String oper =randomOperator().trim();102 this.op =oper;103 sb.append(oper);104 float p2 = 1.0F*width/5;105 drawString(g2,oper,p2);106
107 //获取操作数s2,绘制
108 String s2 =randomNum();109 this.no2 =Integer.parseInt(s2);110 float p3 = 2.0F*width/5;111 sb.append(s2);112 drawString(g2,s2,p3);113
114 //绘制 等号和问号
115 String calculate = "=?";116 float p4 = 3.0F*width/5;117 drawString(g2,calculate,p4);118
119 //绘制干扰线
120 drawLine(bi);121
122 //返回BufferedImage图片
123 returnbi;124 }125
126 //获取算术的运算结果
127 public intgetValue()128 {129 int value = 0;130 switch(this.op)131 {132 case "+": value = this.no1+this.no2;break;133 case "-": value = this.no1-this.no2;break;134 case "x":value = this.no1*this.no2;break;135 default :value = 0;136 }137 returnvalue;138 }139
140 //将图片缓存bi输出到指定的输出流out
141 public static voidoutput(BufferedImage bi, OutputStream out)142 throwsFileNotFoundException, IOException143 {144 ImageIO.write(bi,"JPEG",out);145 }146 }