java实现一个简单的验证码生成器
Posted JavaPanda
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java实现一个简单的验证码生成器相关的知识,希望对你有一定的参考价值。
最近看了网上很多大佬们写的验证码生成,寻思着自己也写一个,话不多说,代码如下:
1 import java.awt.BasicStroke; 2 import java.awt.Color; 3 import java.awt.Font; 4 import java.awt.Graphics2D; 5 import java.awt.image.BufferedImage; 6 import java.io.FileNotFoundException; 7 import java.io.IOException; 8 import java.io.OutputStream; 9 import java.util.Random; 10 11 import javax.imageio.ImageIO; 12 13 public class VerifyCode { 14 private int width = 90; 15 private int high = 40; 16 private Random random = new Random(); 17 private String[] fonts = {"宋体", "微软雅黑", "TimesRoman", "Cambria"}; 18 private String chars = "1234567890"; 19 private String operator = "+-x"; 20 private Color backColor = Color.white; 21 private String text; 22 private int no1; 23 private int no2; 24 private String op; 25 26 //获取随机颜色 27 private Color randomColor() 28 { 29 int red = random.nextInt(150); 30 int green = random.nextInt(150); 31 int blue =random.nextInt(150); 32 return new Color(red,green,blue); 33 } 34 35 //method:获取随机字体 36 private Font 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 new Font(fontName,style,size); 44 } 45 //method:获取随机操作符号 46 private String randomOperator() 47 { 48 int index = random.nextInt(3); 49 return operator.charAt(2)+""; 50 } 51 52 //method:获取随机数字 53 private String randomNum() 54 { 55 int index = random.nextInt(chars.length()); 56 return chars.charAt(index)+""; 57 } 58 59 //method:添加干扰线 60 private void drawLine(BufferedImage image) 61 { 62 int num = 3; 63 Graphics2D graphic = (Graphics2D)image.getGraphics(); 64 for(int i=0;i<num;i++) 65 { 66 int x1 = random.nextInt(width); 67 int x2 = random.nextInt(width); 68 int y1 = random.nextInt(high); 69 int y2 = random.nextInt(high); 70 graphic.setColor(this.randomColor()); 71 graphic.setStroke(new BasicStroke(1.5F)); 72 graphic.drawLine(x1,y1,x2,y2); 73 } 74 } 75 //method:绘制方法 76 private void drawString(Graphics2D graphics,String s,float position) 77 { 78 graphics.setColor(randomColor()); 79 graphics.setFont(randomFont()); 80 graphics.drawString(s,position,high -5); 81 } 82 83 //获取图片缓存 84 public BufferedImage getImage() 85 { 86 BufferedImage bi = new BufferedImage(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 = new StringBuilder(); 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 return bi; 124 } 125 126 //获取算术的运算结果 127 public int getValue() 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 return value; 138 } 139 140 //将图片缓存bi输出到指定的输出流out 141 public static void output(BufferedImage bi, OutputStream out) 142 throws FileNotFoundException, IOException 143 { 144 ImageIO.write(bi,"JPEG",out); 145 } 146 }
测试代码如下:
@Test public void test1() { VerifyCode verifyCode = new VerifyCode(); BufferedImage bi = verifyCode.getImage(); FileOutputStream out; try { out = new FileOutputStream("H:\\\\verifyCode.jpg"); VerifyCode.output(bi, out); }catch (Exception e) { e.printStackTrace(); } System.out.println(verifyCode.getValue()); }
生成的验证码图片如下:
程序输出结果如下:
18
以上,只实现了10以内的加减乘,除法都没有,毕竟作为验证码,除法还要考虑除数非0、整除等内容,因此未添加除法。各位看看就好,水平有限,请多多指教!
以上是关于java实现一个简单的验证码生成器的主要内容,如果未能解决你的问题,请参考以下文章