验证码相关
来源:互联网 发布:几个c语言小游戏源码 编辑:程序博客网 时间:2024/05/17 03:50
package com.springmvc.Common;import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.OutputStream;import java.util.Random;/** * Created by lenovo on 2017/9/10. */public class Validation { public static final char[] CHARS = {'1','2','3','4','5','6','7','8','9','A','B','C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; Random random = new Random(); public String getRandomString(){ StringBuffer stringBuffer = new StringBuffer(); for (int i = 1;i < 4;i++){ stringBuffer.append(CHARS[random.nextInt(CHARS.length)]); } return stringBuffer.toString(); } public Color getRandomColor(){ return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)); } public Color getReverseColor(Color color){ return new Color(255 - color.getRed(),255 - color.getGreen(),255 - color.getBlue()); } String text = getRandomString(); public String getText(){ return text; } public BufferedImage getBufferedImage(int width,int height){ Color color = getRandomColor(); Color reverse = getReverseColor(color); BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = bufferedImage.createGraphics(); graphics2D.setFont(new Font(Font.SANS_SERIF,Font.BOLD,20)); graphics2D.setColor(color); graphics2D.fillRect(0,0,width,height); graphics2D.setColor(reverse); graphics2D.drawString(text,10,22); for (int i = 0,n = random.nextInt(80);i < n;i++){ graphics2D.drawRect(random.nextInt(width),random.nextInt(height),1,1); } return bufferedImage; } public void output(BufferedImage bufferedImage, OutputStream outputStream) throws IOException { ImageIO.write(bufferedImage,"JPEG",outputStream); }}
@RequestMapping("/validation") public void validate(HttpServletResponse response) throws IOException { Validation validation = new Validation(); BufferedImage bufferedImage = validation.getBufferedImage(100,30);//设置验证图片的大小 HttpSession session = getSession(); session.setAttribute("sessionValidation",validation.getText()); validation.output(bufferedImage,response.getOutputStream()); }
阅读全文
0 0
- 验证码相关资料
- 验证码相关
- 验证码相关
- 短信验证码相关
- 验证码相关
- Struts2 验证码刷新相关
- 验证码相关新解
- java 验证码相关问题
- 验证码识别相关知识
- iOS 计时器-验证码相关
- 验证相关
- 关于验证码的相关文章
- 关于验证码图片成生相关
- 短信验证码通道相关误区
- Asp.Net验证相关
- 网络验证相关资料
- 网络验证相关资料
- thinkphp自动验证相关
- bzoj 1697: [Usaco2007 Feb]Cow Sorting牛排序(置换)
- http 常见状态码
- HDU6195 | 2017 ACM-ICPC 亚洲区(沈阳赛区)网络赛-B cable cable cable
- 训练数据的充分性(2):分类器的一致收敛
- iOS 解决网络请求和界面刷新顺序问题(GCD信号量实现)
- 验证码相关
- 2017.9.10 ricehub 思考记录
- SpringBoot定时任务的设置
- ActiveMQ架构设计与最佳实践
- 10、函数的扩展 为函数参数指定默认值、函数的 rest 参数、箭头函数—ES6学习笔记
- 李沐深度学习
- IntelliJ IDEA配置Tomcat
- io框架练习
- stl函数大全