Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.szlabsun.wqimc.web.common.action;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import java.io.BufferedInputStream;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.URLEncoder;
- import java.util.Random;
- import javax.imageio.ImageIO;
- /**
- * User: L
- * Date: 18-1-16
- * Time: 上午9:26
- */
- public class ValidateCodeUtil { // 配置文件服务类
- private Integer width; // 验证码宽度
- private Integer height; // 验证码高度
- private char[] str = "123456789qwertyuipasdfghjklzxcvbnm".toCharArray(); // 验证码最近出现的字符,已去掉0和o
- private InputStream inputStream;
- private String validateCode;
- /**
- * 创建随机函数随机出现四位验证码
- * @return String
- */
- private String generateCode(Integer length,Graphics g)
- {
- // 得到随机产生的验证码数字。
- StringBuilder code = new StringBuilder();
- Random random = new Random();
- for(int i = 0;i < length;i++)
- {
- char rand = str[random.nextInt(str.length - 1)];
- // 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
- int red = random.nextInt(255);
- int green = random.nextInt(165);
- int blue = random.nextInt(1);
- // 用随机产生的颜色将验证码绘制到图像中。
- g.setColor(new Color(red, green, blue));
- g.drawString(String.valueOf(rand), (i+1)*(width - 4) / (length + 1), height - 7);
- // 将产生的四个随机数组合在一起。
- code.append(rand);
- }
- return code.toString();
- }
- /**
- * 生成干扰点
- */
- private void generateSpot(Graphics graphics){
- Random random = new Random();
- for(int i=0;i<250;i++){
- int x = (int) (Math.random() * width);
- int y = (int) (Math.random() * height);
- graphics.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
- graphics.drawOval(x, y, 1, 0);
- }
- }
- /**
- * 生成图
- * @param g
- * @return
- */
- private Graphics generateGraphics(Graphics g){
- // 将图形填充为白色
- g.setColor(Color.LIGHT_GRAY);
- g.fillRect(0, 0, width, height);
- // 创建字体,字体的大小应该根据图片的高度来定。
- g.setFont(new Font("Fixedsys", Font.PLAIN, height - 10));
- // 画边框。
- g.setColor(Color.WHITE);
- g.drawRect(0, 0, width - 1, height - 1);
- return g;
- }
- /**
- * 生成验证码
- * @return
- */
- public String validateCode() throws IOException
- {
- if(width == null){
- width = 87;
- }
- if(height == null){
- height = 37;
- }
- BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- //生成图
- Graphics g = generateGraphics(image.getGraphics());
- // 随机产生干扰点,使图象中的认证码不易被其它程序探测到。
- generateSpot(g);
- // randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
- String randCode = generateCode(4, g);
- ByteArrayOutputStream sos = new ByteArrayOutputStream();
- ImageIO.write(image, "jpeg", sos);
- inputStream = new ByteArrayInputStream(sos.toByteArray());
- sos.close();
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement