Java 生成验证码图片

2019-03-06 07:16:17来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

  1 import java.awt.image.BufferedImage;
  2 import java.io.BufferedReader;
  3 
  4 import java.awt.Color;
  5 import java.io.FileReader;
  6 import java.io.IOException;
  7 import java.util.ArrayList;
  8 import java.util.List;
  9 import java.util.Random;
 10 
 11 import java.awt.Font;
 12 import java.awt.Graphics;
 13 import java.awt.Graphics2D;
 14 
 15 import javax.imageio.ImageIO;
 16 import javax.servlet.ServletException;
 17 import javax.servlet.http.HttpServlet;
 18 import javax.servlet.http.HttpServletRequest;
 19 import javax.servlet.http.HttpServletResponse;
 20 
 21 /**
 22  * 验证码生成程序
 23  */
 24 public class CheckChengyu extends HttpServlet {
 25 
 26 // 集合中保存所有成语
 27     private List<String> words = new ArrayList<String>();
 28 
 29     @Override
 30     public void init() throws ServletException {
 31 // 初始化阶段,读取qy95.txt
 32 // web工程中读取 文件,必须使用绝对磁盘路径
 33         String path = getServletContext().getRealPath("/WEB-INF/chengyu.txt");
 34         try {
 35             BufferedReader reader = new BufferedReader(new FileReader(path));
 36             String line;
 37             while ((line = reader.readLine()) != null) {
 38                 words.add(line);
 39             }
 40             reader.close();
 41         } catch (IOException e) {
 42             e.printStackTrace();
 43         }
 44     }
 45 
 46     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 47 // 禁止缓存
 48 // response.setHeader("Cache-Control", "no-cache");
 49 // response.setHeader("Pragma", "no-cache");
 50 // response.setDateHeader("Expires", -1);
 51 
 52         int width = 120;
 53         int height = 30;
 54 
 55 // 步骤一 绘制一张内存中图片
 56         BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
 57 
 58 // 步骤二 图片绘制背景颜色 ---通过绘图对象
 59         Graphics graphics = bufferedImage.getGraphics();// 得到画图对象 --- 画笔
 60 // 绘制任何图形之前 都必须指定一个颜色
 61         graphics.setColor(getRandColor(200, 250));
 62         graphics.fillRect(0, 0, width, height);
 63 
 64 // 步骤三 绘制边框
 65         graphics.setColor(Color.WHITE);
 66         graphics.drawRect(0, 0, width - 1, height - 1);
 67 
 68 // 步骤四 四个随机数字
 69         Graphics2D graphics2d = (Graphics2D) graphics;
 70 // 设置输出字体
 71         graphics2d.setFont(new Font("宋体", Font.BOLD, 18));
 72 
 73         Random random = new Random();// 生成随机数
 74         int index = random.nextInt(words.size());
 75         String word = words.get(index);// 获得成语
 76 
 77 // 定义x坐标
 78         int x = 10;
 79         for (int i = 0; i < word.length(); i++) {
 80 // 随机颜色
 81         graphics2d.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
82
83 // 旋转 -30 --- 30度 84 int jiaodu = random.nextInt(60) - 30; 85 // 换算弧度 86 double theta = jiaodu * Math.PI / 180; 87 88 // 获得字母数字 89 char c = word.charAt(i); 90 91 // 将c 输出到图片 92 graphics2d.rotate(theta, x, 20); 93 graphics2d.drawString(String.valueOf(c), x, 20); 94 graphics2d.rotate(-theta, x, 20); 95 x += 30; 96 } 97 98 // 将验证码内容保存session 99 request.getSession().setAttribute("checkcode_session", word); 100 101 // 步骤五 绘制干扰线 102 graphics.setColor(getRandColor(160, 200)); 103 int x1; 104 int x2; 105 int y1; 106 int y2; 107 for (int i = 0; i < 30; i++) { 108 x1 = random.nextInt(width); 109 x2 = random.nextInt(12); 110 y1 = random.nextInt(height); 111 y2 = random.nextInt(12); 112 graphics.drawLine(x1, y1, x1 + x2, x2 + y2); 113 } 114 115 // 将上面图片输出到浏览器 ImageIO 116 graphics.dispose();// 释放资源 117 118 //将图片写到response.getOutputStream()中 119 ImageIO.write(bufferedImage, "jpg", response.getOutputStream()); 120 121 } 122 123 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 124 doGet(request, response); 125 } 126 127 /** 128 * 取其某一范围的color 129 * 130 * @param fc int 范围参数1 131 * @param bc int 范围参数2 132 * @return Color 133 */ 134 private Color getRandColor(int fc, int bc) { 135 // 取其随机颜色 136 Random random = new Random(); 137 if (fc > 255) { 138 fc = 255; 139 } 140 if (bc > 255) { 141 bc = 255; 142 } 143 int r = fc + random.nextInt(bc - fc); 144 int g = fc + random.nextInt(bc - fc); 145 int b = fc + random.nextInt(bc - fc); 146 return new Color(r, g, b); 147 } 148 149 }

 


原文链接:https://www.cnblogs.com/voidchar/p/10474972.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:零编程基础想学Java,应该从何开始?

下一篇:Docker安装ElasticSearch 以及使用LogStash实现索引库和数据库同