Java生成验证码

Java代码如下:   收藏代码
  1. package org.springrain.frame.util;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import java.awt.Graphics;  
  6. import java.awt.image.BufferedImage;  
  7. import java.io.File;  
  8. import java.io.FileOutputStream;  
  9. import java.util.Random;   
  10. import javax.imageio.ImageIO;  
  11.   
  12. public class CaptchaUtils {   
  13.     // 随机产生的字符串  
  14.     private static final String RANDOM_STRS = "23456789ABCDEFGHJKMNPQRSTUVWXYZ";  
  15.   
  16.     private static final String FONT_NAME = "Times New Roman";//字体  
  17.     private static final int FONT_SIZE = 24;//字体大小  
  18.     private int width = 85;// 图片宽  
  19.     private int height = 25;// 图片高  
  20.     private int lineNum = 155;// 干扰线数量  
  21.     private int strNum = 4;// 随机产生字符数量  
  22.     private Random random = new Random();  
  23.   
  24.     /** 
  25.      * 生成随机图片 
  26.      */  
  27.     public BufferedImage genRandomCodeImage(StringBuffer randomCode) {  
  28.         // BufferedImage类是具有缓冲区的Image类  
  29.         BufferedImage image = new BufferedImage(width, height,  
  30.                 BufferedImage.TYPE_INT_RGB);  
  31.         // 获取Graphics对象,便于对图像进行各种绘制操作  
  32.         Graphics g = image.getGraphics();  
  33.         g.setColor(getRandColor(200250));  
  34.         g.fillRect(00, width, height);  
  35.         //g.setColor(new Color());   
  36.         //g.drawRect(0,0,width-1,height-1);   
  37.         g.setColor(getRandColor(160200));  
  38.         // 绘制干扰线  
  39.         for (int i = 0; i <= lineNum; i++) {  
  40.             drowLine(g);  
  41.         }  
  42.         // 绘制随机字符  
  43.         g.setFont(new Font(FONT_NAME, Font.PLAIN, FONT_SIZE));   
  44.         for (int i = 0; i < strNum; i++) {  
  45.             randomCode.append(drowString(g, i));  
  46.         }  
  47.         g.dispose();  
  48.         return image;  
  49.     }  
  50.     /** 
  51.      * 给定范围获得随机颜色 
  52.      */  
  53.     private Color getRandColor(int fc, int bc) {  
  54.         Random random = new Random();  
  55.         if (fc > 255)  
  56.             fc = 255;  
  57.         if (bc > 255)  
  58.             bc = 255;  
  59.         int r = fc + random.nextInt(bc - fc);  
  60.         int g = fc + random.nextInt(bc - fc);  
  61.         int b = fc + random.nextInt(bc - fc);  
  62.         return new Color(r, g, b);  
  63.     }  
  64.   
  65.     /** 
  66.      * 绘制字符串 
  67.      */  
  68.     private String drowString(Graphics g, int i) {  
  69.         g.setColor(new Color(20 + random.nextInt(110), 20 + random  
  70.                 .nextInt(110), 20 + random.nextInt(110)));  
  71.         String rand = String.valueOf(getRandomString(random.nextInt(RANDOM_STRS  
  72.                 .length())));  
  73.     //  g.translate(random.nextInt(3), random.nextInt(3));  
  74.         g.drawString(rand, 20 * i + 419);  
  75.         return rand;  
  76.     }  
  77.   
  78.     /** 
  79.      * 绘制干扰线 
  80.      */  
  81.     private void drowLine(Graphics g) {  
  82.         int x = random.nextInt(width);  
  83.         int y = random.nextInt(height);  
  84.         int x0 = random.nextInt(12);  
  85.         int y0 = random.nextInt(12);  
  86.         g.drawLine(x, y, x + x0, y + y0);  
  87.     }  
  88.   
  89.     /** 
  90.      * 获取随机的字符 
  91.      */  
  92.     private String getRandomString(int num) {  
  93.         return String.valueOf(RANDOM_STRS.charAt(num));  
  94.     }  
  95.   
  96.     public static void main(String[] args) {  
  97.         CaptchaUtils tool = new CaptchaUtils();  
  98.         StringBuffer code = new StringBuffer();  
  99.         BufferedImage image = tool.genRandomCodeImage(code);  
  100.         System.out.println(">>> random code =: " + code);  
  101.         try {  
  102.             // 将内存中的图片通过流动形式输出到客户端  
  103.             ImageIO.write(image, "JPEG"new FileOutputStream(new File(  
  104.                     "random-code.jpg")));  
  105.         } catch (Exception e) {  
  106.             e.printStackTrace();  
  107.         }  
  108.   
  109.     }  
  110. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值