java编写简单漂亮的验证码
来源:互联网 发布:墨尔本海鲜市场 知乎 编辑:程序博客网 时间:2024/04/28 09:30
代码如下
package com.bdqn.common;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.HashSet;import java.util.Random;import java.util.Set;import javax.imageio.ImageIO;import org.apache.struts2.ServletActionContext;/** * 生成验证码 * <p>Title:RandomCodeGenerator<p> * <p>Description: </p> * <p>Company: </p> * @author 123456 * <p>yangyang</p> */public class RandomCodeGenerator { private static final char[] CHARS = "abcdefghijkmnpqrstovwxyz0123456789".toCharArray(); private int length; private int fontSize = 14; private String randomCode = null; public RandomCodeGenerator(int length) { this.length = length; } public RandomCodeGenerator() { this(4); } public String getRandomCode() { if (randomCode == null) { // 生成验证码 this.randomCode = initRandomCode(); } return randomCode; } // 初始化验证码 private String initRandomCode() { Set<Character> chars = new HashSet<>(); Random r = new Random(); while(chars.size() != length) { chars.add(CHARS[r.nextInt(CHARS.length)]); } char[] cs = new char[chars.size()]; int index = 0; for (char c : chars) { cs[index++] = c; } return new String(cs).toUpperCase(); } public BufferedImage generator(OutputStream output) throws IOException { int width = length * (fontSize + 2) + 10; int height = fontSize + 10; //创建一个缓存图片对象 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //得到画笔对象 Graphics2D g = (Graphics2D)image.getGraphics(); //设置前景色 g.setColor(Color.PINK); //使用前景色填充距形 g.fillRect(0, 0, width, height); Random r = new Random(); //绘制干扰线 for(int i = 0; i < 3; i++){ //生成随机颜色 Color c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)); g.setColor(c); //画线 g.drawLine(r.nextInt(width / 2), r.nextInt(height), r.nextInt(width / 2) + width / 2, r.nextInt(height)); } g.setColor(Color.WHITE); Font font = new Font("Cooper Std", Font.PLAIN, fontSize); //设置字体 g.setFont(font); if(randomCode == null) this.randomCode = getRandomCode(); for(int i = 0; i < randomCode.length(); i++){ //画字符 g.drawString(randomCode.substring(i, i+1), 8 + fontSize * i, height / 2 + fontSize / 2); } //完成,并将验证码放入session中 //此处是将验证码存入session中进行验证码验证 //ServletActionContext.getRequest().getSession().setAttribute("checkCode", randomCode); image.flush(); //输出 ImageIO.write(image, "PNG", output); return image; } public int getFontSize() { return fontSize; } public void setFontSize(int fontSize) { this.fontSize = fontSize; } public static void main(String[] args) { RandomCodeGenerator rcg = new RandomCodeGenerator(6); System.out.println(rcg.getRandomCode()); try { rcg.generator(new FileOutputStream("F:/a.png")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
效果图如下:
阅读全文
0 0
- java编写简单漂亮的验证码
- 漂亮的自制java验证码
- JAVA编写的验证码
- Java验证码的编写
- 漂亮好使的验证码
- java 超经漂亮验证码
- Java编写验证码
- Java编写验证码
- Java 编写验证码
- java验证码的编写和校验
- Java编写生成的验证码
- 一个简单漂亮的Java博客系统
- 一款漂亮的验证码(jsp)
- asp.net 一款漂亮的验证码
- 漂亮的 PHP 验证码类
- asp.net 生成漂亮的验证码
- PHP一个漂亮的验证码
- 一个漂亮的PHP验证码
- vim编程环境准备
- 字符数组与任意数字的转化
- java播放mp3格式音频文件
- css字体垂直&水平居中
- phpmyadmin新姿势getshell
- java编写简单漂亮的验证码
- 教您如何导出小影里的视频到手机相册
- css居中的多种方法
- websphere服务器之was应用部署(集群部署)
- EJB的理解
- 在前人基础上搭建samba服务器
- n进制下表达式成立
- 自然语言处理常用算法
- python基础之元组、文件操作、编码、函数、变量