验证码与base64转换的工具类
- 1、关于验证码的工具类
- 2、关于stringBase64与图像的互转
- 3、验证码效果图
1、关于验证码的工具类
package com.test.utils;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
/** * 验证码生成工具 */
public class Captcha {
private BufferedImage image;// 图像
private String captcha;// 验证码
private static char code[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789".toCharArray();
public static final String SESSION_CODE_NAME = "code";
private Captcha() {
init();// 初始化属性
}
/* * 取得RandomNumUtil实例 */
public static Captcha Instance() {
return new Captcha();
}
/* * 取得验证码图片 */
public BufferedImage getImage() {
return this.image;
}
/* * 取得图片的验证码 */
public String getString() {
return this.captcha;
}
private void init() {
// 在内存中创建图象
int width = 170, height = 40;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics graphics = image.getGraphics();
// 生成随机类
Random random = new Random();
// 设定背景色
graphics.setColor(getRandColor(245, 250));
graphics.fillRect(0, 0, width, height);
// 设定字体
graphics.setFont(new Font("Times New Roman", Font.PLAIN, 36));
for (int i = 0; i < 500; i++) {
// 随机产生500条干扰线,使图象中的认证码不易被其它程序探测到
graphics.setColor(getRandColor(150, 255));
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
int x2 = random.nextInt(12);
int y2 = random.nextInt(12);
graphics.drawLine(x1, y1, x1 + x2, y1 + y2);
}
// 取随机产生的认证码(4位数字)
String sRand = "";
for (int i = 0; i < 4; i++) {
String rand = String.valueOf(code[random.nextInt(code.length)]);
sRand += rand;
// 将认证码显示到图象中
graphics.setColor(new Color(1 + random.nextInt(255), 1 + random.nextInt(255), 1 + random.nextInt(255)));
// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
graphics.drawString(rand, 26 * i + 40, 32);
}
// 赋值验证码
this.captcha = sRand;
// 图象生效
graphics.dispose();
this.image = image;// 赋值图像
}
/* * 给定范围获得随机颜色 */
private Color getRandColor(int start, int end) {
Random random = new Random();
if (start > 255)
start = 255;
if (end > 255)
end = 255;
int r = start + random.nextInt(end - start);
int g = start + random.nextInt(end - start);
int b = start + random.nextInt(end - start);
return new Color(r, g, b);
}
}
2、关于stringBase64与图像的互转
package com.test.utils;
import sun.misc.BASE64Decoder;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Base64ToString {
//把图片转换成string类型
public static String imgToBase64String(final RenderedImage img) {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(img, "jpg", Base64.getEncoder().wrap(os));
return os.toString(StandardCharsets.ISO_8859_1.name());
} catch (final IOException ioe) {
throw new UncheckedIOException(ioe);
}
}
//把string类型转换成图片
public static BufferedImage base64StringToImg(final String base64String) {
try {
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytes = decoder.decodeBuffer(base64String);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
return ImageIO.read(bais);
} catch (final IOException ioe) {
throw new UncheckedIOException(ioe);
}
}
//绘制图片
public static void draw_image(BufferedImage image_buffer) throws IOException {
ImageIcon icon = new ImageIcon(image_buffer);
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());
frame.setSize(image_buffer.getWidth(), image_buffer.getHeight());
JLabel lbl = new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
3、验证码效果图

还没有评论,来说两句吧...