java实现图片验证码

一、验证码生成类

  1 package hbi.tech.utils;
  2 import javax.imageio.ImageIO;
  3 import java.awt.*;
  4 import java.awt.image.BufferedImage;
  5 import java.io.ByteArrayInputStream;
  6 import java.io.FileOutputStream;
  7 import java.io.IOException;
  8 import java.io.OutputStream;
  9 import java.util.Random;
 10
 11 /**
 12  * 验证码生成器
 13  *
 14  */
 15 public class SCaptcha {
 16     // 图片的宽度。
 17     private int width = 120;
 18     // 图片的高度。
 19     private int height = 40;
 20     // 验证码字符个数
 21     private int codeCount = 4;
 22     // 验证码干扰线数
 23     private int lineCount = 50;
 24     // 验证码
 25     private String code = null;
 26     // 验证码图片Buffer
 27     private BufferedImage buffImg = null;
 28
 29     private char[] codeSequence = { ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘M‘, ‘N‘, ‘P‘, ‘Q‘, ‘R‘,
 30             ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘ };
 31     // 生成随机数
 32     private Random random = new Random();
 33
 34     public SCaptcha() {
 35         this.createCode();
 36     }
 37
 38     /**
 39      *
 40      * @param width 图片宽
 41      * @param height 图片高
 42      */
 43     public SCaptcha(int width, int height) {
 44         this.width = width;
 45         this.height = height;
 46         this.createCode();
 47     }
 48
 49     /**
 50      *
 51      * @param width 图片宽
 52      * @param height 图片高
 53      * @param codeCount 字符个数
 54      * @param lineCount 干扰线条数
 55      */
 56     public SCaptcha(int width, int height, int codeCount, int lineCount) {
 57         this.width = width;
 58         this.height = height;
 59         this.codeCount = codeCount;
 60         this.lineCount = lineCount;
 61         this.createCode();
 62     }
 63
 64     public void createCode() {
 65         int codeX = 0;
 66         int fontHeight = 0;
 67         fontHeight = height - 5;// 字体的高度
 68         codeX = width / (codeCount + 3);// 每个字符的宽度
 69
 70         // 图像buffer
 71         buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
 72         Graphics2D g = buffImg.createGraphics();
 73
 74         // 将图像填充为白色
 75         g.setColor(Color.WHITE);
 76         g.fillRect(0, 0, width, height);
 77
 78         // 创建字体
 79         ImgFontByte imgFont = new ImgFontByte();
 80         Font font = imgFont.getFont(fontHeight);
 81         g.setFont(font);
 82
 83         StringBuffer randomCode = new StringBuffer();
 84         // 随机产生验证码字符
 85         for (int i = 0; i < codeCount; i++) {
 86             String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
 87             // 设置字体颜色
 88             g.setColor(getRandomColor());
 89             // 设置字体位置
 90             g.drawString(strRand, (i + 1) * codeX, getRandomNumber(height / 2) + 25);
 91             randomCode.append(strRand);
 92         }
 93         code = randomCode.toString();
 94     }
 95
 96     /** 获取随机颜色 */
 97     private Color getRandomColor() {
 98         int r = getRandomNumber(255);
 99         int g = getRandomNumber(255);
100         int b = getRandomNumber(255);
101         return new Color(r, g, b);
102     }
103
104     /** 获取随机数 */
105     private int getRandomNumber(int number) {
106         return random.nextInt(number);
107     }
108
109     public void write(String path) throws IOException {
110         OutputStream sos = new FileOutputStream(path);
111         this.write(sos);
112     }
113
114     public void write(OutputStream sos) throws IOException {
115         ImageIO.write(buffImg, "png", sos);
116         sos.close();
117     }
118
119     public BufferedImage getBuffImg() {
120         return buffImg;
121     }
122
123     public String getCode() {
124         return code;
125     }
126
127     /** 字体样式类 */
128     class ImgFontByte {
129         public Font getFont(int fontHeight) {
130             try {
131                 Font baseFont = Font.createFont(Font.HANGING_BASELINE, new ByteArrayInputStream(
132                         hex2byte(getFontByteStr())));
133                 return baseFont.deriveFont(Font.PLAIN, fontHeight);
134             } catch (Exception e) {
135                 return new Font("Arial", Font.PLAIN, fontHeight);
136             }
137         }
138
139         private byte[] hex2byte(String str) {
140             if (str == null)
141                 return null;
142             str = str.trim();
143             int len = str.length();
144             if (len == 0 || len % 2 == 1)
145                 return null;
146
147             byte[] b = new byte[len / 2];
148             try {
149                 for (int i = 0; i < str.length(); i += 2) {
150                     b[i / 2] = (byte) Integer.decode("0x" + str.substring(i, i + 2)).intValue();
151                 }
152                 return b;
153             } catch (Exception e) {
154                 return null;
155             }
156         }
157
158         // 字体文件的十六进制字符串
159         private String getFontByteStr() {
160             //防止报字符串长度过长错误,改为从配置文件读取
161             return ReadFontByteProperties.getFontByteStr();
162         }
163     }
164 }

二、读取字体文件类

 1 package hbi.tech.utils;
 2 import java.io.InputStream;
 3 import java.util.Properties;
 4 public class ReadFontByteProperties {
 5     static private String fontByteStr = null;
 6     static {
 7         loads();
 8     }
 9     synchronized static public void loads() {
10         if (fontByteStr == null) {
11             InputStream is = ReadFontByteProperties.class.getResourceAsStream("/fontByte.properties");
12             Properties dbproperties = new Properties();
13             try {
14                 dbproperties.load(is);
15                 fontByteStr = dbproperties.getProperty("fontByteStr").toString();
16             } catch (Exception e) {
17                 //System.err.println("不能读取属性文件. " + "请确保fontByte.properties在CLASSPATH指定的路径中");
18             }
19         }
20     }
21     public static String getFontByteStr() {
22         if (fontByteStr == null)
23             loads();
24         return fontByteStr;
25     }
26 }

三、生成验证码接口

 1    /**
 2      * @author [email protected]
 3      * @date 2017/8/23
 4      * @description 生成图片验证码
 5      */
 6     @RequestMapping(value = "/userInfo/verification", method = {RequestMethod.POST, RequestMethod.GET})
 7     @ResponseBody
 8     public void verification(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
 9         // 设置响应的类型格式为图片格式
10         response.setContentType("image/jpeg");
11         // 禁止图像缓存。
12         response.setHeader("Pragma", "no-cache");
13         response.setHeader("Cache-Control", "no-cache");
14         response.setDateHeader("Expires", 0);
15         //实例生成验证码对象
16         SCaptcha instance = new SCaptcha();
17         //将验证码存入session
18         session.setAttribute("verification", instance.getCode());
19         //向页面输出验证码图片
20         instance.write(response.getOutputStream());
21     }

将生成的验证码图片存在session中,当用户登录时即可和用户输入的验证码的值进行判断,如果验证相同,则进行后续操作。

时间: 2024-07-31 04:39:59

java实现图片验证码的相关文章

java实现图片验证码登陆

java实现图片验证码登陆 话不多说 直接上图 ↓↓↓↓↓↓↓↓↓↓ 成果图 ↓↓↓↓↓↓↓↓↓↓ ↓↓↓↓↓↓↓↓↓↓ ↓↓↓↓↓↓↓ 差不多这就是演示图啦 如需源码请联系QQ:11234013 可手把手讲解知识点 原文地址:https://www.cnblogs.com/zhetiankj/p/12641439.html

java创建图片验证码

package com; import java.awt.BasicStroke;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random; import javax.imageio.ImageIO;import javax.servlet.h

java web中图片验证码功能实现

用户在注册网站信息的时候基本上都要数据验证码验证.那么图片验证码功能该如何实现呢? 大概步骤是: 1.在内存中创建缓存图片 2.设置背景色 3.画边框 4.写字母 5.绘制干扰信息 6.图片输出 废话不多说,直接上代码 package com.lsgjzhuwei.servlet.response; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.Buffer

工作笔记5.JAVA图片验证码

本文主要内容为:利用JAVA图片制作验证码. 设计思路: 1.拷贝AuthImageServlet.class图片验证码 2.配置web.xml 3.JSP中,调用封装好的AuthImageServlet,实现载入验证码的功能. 4.取出存放在Session中的验证码.在Action中推断验证码的正确性 相比較上一篇博客<工作笔记5.JAVA文本框验证码>而言,图片验证码添加了安全性. 在Action中,通过取出Session中的验证码与输入的验证码是否匹配进行推断. 步骤: 1.拷贝Auth

JAVA生成一次性图片验证码

现在很多地方都需要写验证码登录验证,这样的好处是可以减轻服务器的压力等,下面就用java实现一次性登录验证码的书写. 1.验证码生成类: package com.easyteam; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedI

java图片验证码包括文字和图片的旋转

java图片验证码包括文字图片的旋转: 此例子演示的是两位数的加减运算,需要的可以通过自己的修改获得更多的方式: 或者我上传的资源中也有其他的两种方式供选择:http://download.csdn.net/detail/huitoukest/8043711 package com.utils; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; im

java实现生成简单图片验证码

原文:java实现生成简单图片验证码 源代码下载地址:http://www.zuidaima.com/share/1550463428840448.htm 项目载图: 该项目要转换为Dynamic web project http://www.zuidaima.com/blog/1618162161323008.htm 另外缺少jar包: http://www.zuidaima.com/jar/search/jstl-1.0.1.htm http://www.zuidaima.com/jar/s

Java Web:使用Servlet生成网页随机图片验证码

最近在学习Java Web开发,做了一个生成网页随机图片验证码的例子,在此记录. 一.新建Servlet项目: 在MyEclipse中新建Servlet项目,一步步操作就OK,在此不再赘述.建好之后文件目录树如下图: 二.源代码实现: (1)java代码: package com.zdt.identity; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.

问题记录-java图片验证码显示乱码

部署机器 操作系统:centos 7 java版本: java version "1.7.0_80" 问题症状 将一个java web的程序部署到了两台配置相同的服务器上之后(服务器1.服务器2),程序都正常启动,当前端请求注册服务时,会向后台请求图片验证码,然而(服务器1)验证码出现了如图1所示的乱码问题,图二为(服务器2)正常的验证码显示. 图1 图2 问题探索 猜测1:java字符的编码问题 探索过程:将字符编码转换成utf-8编码,但是乱码问题没有得到解决 猜测2:服务器字体问