|
@@ -0,0 +1,264 @@
|
|
|
+package com.caimei365.user.utils;
|
|
|
+
|
|
|
+import org.apache.commons.codec.binary.Base64;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.geom.AffineTransform;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.*;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Random;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Description
|
|
|
+ *
|
|
|
+ * @author : Charles
|
|
|
+ * @date : 2021/3/3
|
|
|
+ */
|
|
|
+public class ImageCaptchaUtil {
|
|
|
+ private static final String VERIFY_CODES = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
|
|
|
+ private static final String WWW_PREFIX = "www:vc:";
|
|
|
+ private static final String CRM_PREFIX = "miniProgram:vc:";
|
|
|
+ private static final String APPLETS_PREFIX = "crm:vc:";
|
|
|
+
|
|
|
+ private static Random random = new Random();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 定义一个验证码类
|
|
|
+ */
|
|
|
+ public static class Captcha {
|
|
|
+ /**
|
|
|
+ * 生成的字符串验证码
|
|
|
+ */
|
|
|
+ private String code;
|
|
|
+ /**
|
|
|
+ * 验证码字符串的加密
|
|
|
+ */
|
|
|
+ private String md5Code;
|
|
|
+ /**
|
|
|
+ * 验证码生成的图片(base64格式)
|
|
|
+ */
|
|
|
+ private String base64Image;
|
|
|
+
|
|
|
+ Captcha(String code, String base64Image) {
|
|
|
+ this.code = code;
|
|
|
+ this.base64Image = base64Image;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getCode() {
|
|
|
+ return code;
|
|
|
+ }
|
|
|
+ public void setCode(String code) {
|
|
|
+ this.code = code;
|
|
|
+ }
|
|
|
+ public String getMd5Code() throws Exception {
|
|
|
+ if (this.code != null) {
|
|
|
+ md5Code = code;
|
|
|
+ for (int i = 0; i < 3; i++) {
|
|
|
+ md5Code = Md5Util.md5(md5Code);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return md5Code;
|
|
|
+ }
|
|
|
+ /*public void setMd5Code(String md5Code) {
|
|
|
+ this.md5Code = md5Code;
|
|
|
+ }*/
|
|
|
+ public String getBase64Image() {
|
|
|
+ return base64Image;
|
|
|
+ }
|
|
|
+ public void setBase64Image(String base64Image) {
|
|
|
+ this.base64Image = base64Image;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取验证码缓存key
|
|
|
+ *
|
|
|
+ * @param md5Code
|
|
|
+ * @param flag 0:www,1:crm/h5,2:小程序
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getCaptchaKey(String md5Code, Integer flag) {
|
|
|
+ if (0 == flag) {
|
|
|
+ return "www:vc:" + md5Code;
|
|
|
+ } else if (1 == flag) {
|
|
|
+ return "crm:vc:" + md5Code;
|
|
|
+ } else if (2 == flag) {
|
|
|
+ return "miniProgram:vc:" + md5Code;
|
|
|
+ } else{
|
|
|
+ return md5Code;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 图片验证码
|
|
|
+ *
|
|
|
+ * @param w 验证码图片宽度
|
|
|
+ * @param h 验证码图片高度
|
|
|
+ * @param size 验证码图片里面字符串位数
|
|
|
+ */
|
|
|
+ public static Captcha getCaptcha(int w, int h, int size) throws IOException {
|
|
|
+ // 使用系统默认字符源生成验证码
|
|
|
+ String code = generateCaptcha(size);
|
|
|
+ ByteArrayOutputStream outPutStream = new ByteArrayOutputStream();
|
|
|
+ drawCaptchaImage(w, h, outPutStream, code);
|
|
|
+ InputStream in = new ByteArrayInputStream(outPutStream.toByteArray());
|
|
|
+ byte[] data = null;
|
|
|
+ // 读取图片字节数组
|
|
|
+ try {
|
|
|
+ data = new byte[in.available()];
|
|
|
+ in.read(data);
|
|
|
+ in.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ // 对字节数组Base64编码
|
|
|
+ String base64 = Base64.encodeBase64String(data);
|
|
|
+ return new Captcha(code, "data:image/jpg;base64," + base64);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成验证码
|
|
|
+ * @param size 验证码长度
|
|
|
+ *
|
|
|
+ */
|
|
|
+ private static String generateCaptcha(int size) {
|
|
|
+ int charLength = VERIFY_CODES.length();
|
|
|
+ Random rand = new Random(System.currentTimeMillis());
|
|
|
+ StringBuilder verifyCode = new StringBuilder(size);
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ verifyCode.append(VERIFY_CODES.charAt(rand.nextInt(charLength - 1)));
|
|
|
+ }
|
|
|
+ return verifyCode.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 输出指定验证码图片流
|
|
|
+ */
|
|
|
+ private static void drawCaptchaImage(int w, int h, OutputStream os, String code) throws IOException {
|
|
|
+ int size = code.length();
|
|
|
+ BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
|
|
|
+ Random rand = new Random();
|
|
|
+ Graphics2D g2 = image.createGraphics();
|
|
|
+ g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
+ Color[] colors = new Color[5];
|
|
|
+ Color[] colorSpaces = new Color[]{Color.WHITE, Color.CYAN, Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA,
|
|
|
+ Color.ORANGE, Color.PINK, Color.YELLOW};
|
|
|
+ float[] fractions = new float[colors.length];
|
|
|
+ for (int i = 0; i < colors.length; i++) {
|
|
|
+ colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)];
|
|
|
+ fractions[i] = rand.nextFloat();
|
|
|
+ }
|
|
|
+ Arrays.sort(fractions);
|
|
|
+ // 设置边框色
|
|
|
+ g2.setColor(Color.GRAY);
|
|
|
+ g2.fillRect(0, 0, w, h);
|
|
|
+ Color c = getRandColor(200, 250);
|
|
|
+ // 设置背景色
|
|
|
+ g2.setColor(c);
|
|
|
+ g2.fillRect(0, 2, w, h - 4);
|
|
|
+ // 绘制干扰线
|
|
|
+ Random random = new Random();
|
|
|
+ // 设置线条的颜色
|
|
|
+ g2.setColor(getRandColor(160, 200));
|
|
|
+ for (int i = 0; i < 20; i++) {
|
|
|
+ int x = random.nextInt(w - 1);
|
|
|
+ int y = random.nextInt(h - 1);
|
|
|
+ int xl = random.nextInt(6) + 1;
|
|
|
+ int yl = random.nextInt(12) + 1;
|
|
|
+ g2.drawLine(x, y, x + xl + 40, y + yl + 20);
|
|
|
+ }
|
|
|
+ // 添加噪点
|
|
|
+ float noiseRate = 0.05f;
|
|
|
+ int area = (int) (noiseRate * w * h);
|
|
|
+ for (int i = 0; i < area; i++) {
|
|
|
+ int x = random.nextInt(w);
|
|
|
+ int y = random.nextInt(h);
|
|
|
+ int rgb = getRandomIntColor();
|
|
|
+ image.setRGB(x, y, rgb);
|
|
|
+ }
|
|
|
+ // 使图片扭曲
|
|
|
+ distortion(g2, w, h, c);
|
|
|
+ g2.setColor(getRandColor(100, 160));
|
|
|
+ int fontSize = h - 4;
|
|
|
+ Font font = new Font("Algerian", Font.ITALIC, fontSize);
|
|
|
+ g2.setFont(font);
|
|
|
+ char[] chars = code.toCharArray();
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ AffineTransform affine = new AffineTransform();
|
|
|
+ affine.setToRotation(Math.PI / 4 * rand.nextDouble() * (rand.nextBoolean() ? 1 : -1),
|
|
|
+ (w / size) * i + fontSize / 2, h / 2);
|
|
|
+ g2.setTransform(affine);
|
|
|
+ g2.drawChars(chars, i, 1, ((w - 10) / size) * i + 5, h / 2 + fontSize / 2 - 10);
|
|
|
+ }
|
|
|
+ g2.dispose();
|
|
|
+ ImageIO.write(image, "jpg", os);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Color getRandColor(int fc, int bc) {
|
|
|
+ if (fc > 255) {
|
|
|
+ fc = 255;
|
|
|
+ }
|
|
|
+ if (bc > 255) {
|
|
|
+ bc = 255;
|
|
|
+ }
|
|
|
+ int r = fc + random.nextInt(bc - fc);
|
|
|
+ int g = fc + random.nextInt(bc - fc);
|
|
|
+ int b = fc + random.nextInt(bc - fc);
|
|
|
+ return new Color(r, g, b);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int getRandomIntColor() {
|
|
|
+ int[] rgb = getRandomRgb();
|
|
|
+ int color = 0;
|
|
|
+ for (int c : rgb) {
|
|
|
+ color = color << 8;
|
|
|
+ color = color | c;
|
|
|
+ }
|
|
|
+ return color;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int[] getRandomRgb() {
|
|
|
+ int[] rgb = new int[3];
|
|
|
+ for (int i = 0; i < 3; i++) {
|
|
|
+ rgb[i] = random.nextInt(255);
|
|
|
+ }
|
|
|
+ return rgb;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void distortion(Graphics g, int w1, int h1, Color color) {
|
|
|
+ distortionX(g, w1, h1, color);
|
|
|
+ distortionY(g, w1, h1, color);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void distortionX(Graphics g, int w1, int h1, Color color) {
|
|
|
+ int period = random.nextInt(2);
|
|
|
+ int frames = 1;
|
|
|
+ int phase = random.nextInt(2);
|
|
|
+ for (int i = 0; i < h1; i++) {
|
|
|
+ double d = (double) (period >> 1)
|
|
|
+ * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
|
|
|
+ g.copyArea(0, i, w1, 1, (int) d, 0);
|
|
|
+ g.setColor(color);
|
|
|
+ g.drawLine((int) d, i, 0, i);
|
|
|
+ g.drawLine((int) d + w1, i, w1, i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void distortionY(Graphics g, int w1, int h1, Color color) {
|
|
|
+ int period = random.nextInt(40) + 10;
|
|
|
+ int frames = 20;
|
|
|
+ int phase = 7;
|
|
|
+ for (int i = 0; i < w1; i++) {
|
|
|
+ double d = (double) (period >> 1)
|
|
|
+ * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
|
|
|
+ g.copyArea(i, 0, 1, h1, 0, (int) d);
|
|
|
+ g.setColor(color);
|
|
|
+ g.drawLine(i, (int) d, i, 0);
|
|
|
+ g.drawLine(i, (int) d + h1, i, h1);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|