|
|
@@ -0,0 +1,220 @@
|
|
|
+package cn.reghao.dfs.store.util.image;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.geom.CubicCurve2D;
|
|
|
+import java.awt.geom.QuadCurve2D;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.security.SecureRandom;
|
|
|
+import java.util.Random;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author reghao
|
|
|
+ * @date 2021-07-24 12:52:18
|
|
|
+ */
|
|
|
+public class Captcha {
|
|
|
+ private final Random random = new SecureRandom();
|
|
|
+ private final String[] fontNames = {"宋体", "华文楷体", "黑体", "Georgia", "微软雅黑", "楷体_GB2312"};
|
|
|
+ /**
|
|
|
+ * 常用颜色
|
|
|
+ * */
|
|
|
+ public static final int[][] COLOR = {{0, 135, 255}, {51, 153, 51}, {255, 102, 102}, {255, 153, 0}, {153, 102, 0},
|
|
|
+ {153, 102, 153}, {51, 153, 153}, {102, 102, 255}, {0, 102, 204}, {204, 51, 51}, {0, 153, 204}, {0, 51, 102}
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证码显示宽度
|
|
|
+ * */
|
|
|
+ protected int width = 130;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证码显示高度
|
|
|
+ * */
|
|
|
+ protected int height = 48;
|
|
|
+
|
|
|
+ public Image imageWithDisturb(String string, int width, int height) {
|
|
|
+ BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
+ Graphics2D g2d = (Graphics2D) bi.getGraphics();
|
|
|
+ // 填充背景
|
|
|
+ g2d.setColor(Color.WHITE);
|
|
|
+ g2d.fillRect(0, 0, width, height);
|
|
|
+ // 抗锯齿
|
|
|
+ g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
+ // 画干扰圆
|
|
|
+ drawOval(2, null, g2d);
|
|
|
+ // 画干扰线
|
|
|
+ g2d.setStroke(new BasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
|
|
|
+ drawBesselLine(1, null, g2d);
|
|
|
+
|
|
|
+ FontMetrics fontMetrics = g2d.getFontMetrics();
|
|
|
+ // 每一个字符所占的宽度
|
|
|
+ int fW = width / string.length();
|
|
|
+ // 字符的左右边距
|
|
|
+ int fSp = (fW - (int) fontMetrics.getStringBounds("W", g2d).getWidth()) / 2;
|
|
|
+ for (int i = 0; i < string.length(); i++) {
|
|
|
+ g2d.setColor(color());
|
|
|
+ // 文字的纵坐标
|
|
|
+ int fY = height - ((height - (int) fontMetrics.getStringBounds(String.valueOf(string.charAt(i)), g2d).getHeight()) >> 1);
|
|
|
+ g2d.drawString(String.valueOf(string.charAt(i)), i * fW + fSp + 3, fY - 3);
|
|
|
+ }
|
|
|
+ g2d.dispose();
|
|
|
+ return bi;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 随机画干扰圆
|
|
|
+ *
|
|
|
+ * @param num 数量
|
|
|
+ * @param color 颜色
|
|
|
+ * @param g Graphics2D
|
|
|
+ */
|
|
|
+ public void drawOval(int num, Color color, Graphics2D g) {
|
|
|
+ for (int i = 0; i < num; i++) {
|
|
|
+ g.setColor(color == null ? color() : color);
|
|
|
+ int w = 5 + num(10);
|
|
|
+ g.drawOval(num(width - 25), num(height - 15), w, w);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int num(int min, int max) {
|
|
|
+ return min + new Random().nextInt(max - min);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int num(int num) {
|
|
|
+ return new Random().nextInt(num);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 随机画贝塞尔曲线
|
|
|
+ *
|
|
|
+ * @param num 数量
|
|
|
+ * @param color 颜色
|
|
|
+ * @param g Graphics2D
|
|
|
+ */
|
|
|
+ public void drawBesselLine(int num, Color color, Graphics2D g) {
|
|
|
+ for (int i = 0; i < num; i++) {
|
|
|
+ g.setColor(color == null ? color() : color);
|
|
|
+ int x1 = 5, y1 = num(5, height / 2);
|
|
|
+ int x2 = width - 5, y2 = num(height / 2, height - 5);
|
|
|
+ int ctrlx = num(width / 4, width / 4 * 3), ctrly = num(5, height - 5);
|
|
|
+ if (num(2) == 0) {
|
|
|
+ int ty = y1;
|
|
|
+ y1 = y2;
|
|
|
+ y2 = ty;
|
|
|
+ }
|
|
|
+ // 二阶贝塞尔曲线
|
|
|
+ if (num(2) == 0) {
|
|
|
+ QuadCurve2D shape = new QuadCurve2D.Double();
|
|
|
+ shape.setCurve(x1, y1, ctrlx, ctrly, x2, y2);
|
|
|
+ g.draw(shape);
|
|
|
+ } else {
|
|
|
+ // 三阶贝塞尔曲线
|
|
|
+ int ctrlx1 = num(width / 4, width / 4 * 3), ctrly1 = num(5, height - 5);
|
|
|
+ CubicCurve2D shape = new CubicCurve2D.Double(x1, y1, ctrlx, ctrly, ctrlx1, ctrly1, x2, y2);
|
|
|
+ g.draw(shape);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 给定范围获得随机颜色
|
|
|
+ *
|
|
|
+ * @param fc 0-255
|
|
|
+ * @param bc 0-255
|
|
|
+ * @return 随机颜色
|
|
|
+ */
|
|
|
+ public Color color(int fc, int bc) {
|
|
|
+ if (fc > 255) {
|
|
|
+ fc = 255;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (bc > 255) {
|
|
|
+ bc = 255;
|
|
|
+ }
|
|
|
+
|
|
|
+ int r = fc + num(bc - fc);
|
|
|
+ int g = fc + num(bc - fc);
|
|
|
+ int b = fc + num(bc - fc);
|
|
|
+ return new Color(r, g, b);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取随机常用颜色
|
|
|
+ *
|
|
|
+ * @return 随机颜色
|
|
|
+ */
|
|
|
+ public Color color() {
|
|
|
+ int[] color = COLOR[num(COLOR.length)];
|
|
|
+ return new Color(color[0], color[1], color[2]);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String drawImage(ByteArrayOutputStream output) {
|
|
|
+ int width = 96;
|
|
|
+ int height = 64;
|
|
|
+
|
|
|
+ //创建图片缓冲区
|
|
|
+ BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
|
|
|
+ Graphics2D g = bi.createGraphics();
|
|
|
+
|
|
|
+ //设置背景颜色
|
|
|
+ g.setBackground(new Color(255, 255, 255));
|
|
|
+ g.clearRect(0, 0, width, height);
|
|
|
+
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ //这里只画入四个字符
|
|
|
+ for (int i = 0; i < 4; i++) {
|
|
|
+ String s = randomChar() + ""; //随机生成字符,因为只有画字符串的方法,没有画字符的方法,所以需要将字符变成字符串再画
|
|
|
+ sb.append(s); //添加到StringBuilder里面
|
|
|
+ float x = i * 1.0F * width / 4; //定义字符的x坐标
|
|
|
+ g.setFont(randomFont()); //设置字体,随机
|
|
|
+ g.setColor(randomColor()); //设置颜色,随机
|
|
|
+ g.drawString(s, x, height - 5);
|
|
|
+ }
|
|
|
+
|
|
|
+ //定义干扰线
|
|
|
+ //定义干扰线的数量(3-5条)int num = random.nextInt(max)%(max-min+1) + min;
|
|
|
+ int num = random.nextInt(5) % 3 + 3;
|
|
|
+ Graphics2D graphics = (Graphics2D) bi.getGraphics();
|
|
|
+ for (int i = 0; i < num; i++) {
|
|
|
+ int x1 = random.nextInt(width);
|
|
|
+ int y1 = random.nextInt(height);
|
|
|
+ int x2 = random.nextInt(width);
|
|
|
+ int y2 = random.nextInt(height);
|
|
|
+ graphics.setColor(randomColor());
|
|
|
+ graphics.drawLine(x1, y1, x2, y2);
|
|
|
+ }
|
|
|
+ // 释放图形上下文
|
|
|
+ g.dispose();
|
|
|
+ try {
|
|
|
+ ImageIO.write(bi, "jpg", output);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 返回验证码字符串
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private Font randomFont() {
|
|
|
+ int index = random.nextInt(fontNames.length);
|
|
|
+ String fontName = fontNames[index];
|
|
|
+ int style = random.nextInt(4); //随机获取4种字体的样式
|
|
|
+ int size = random.nextInt(20) % 6 + 15; //随机获取字体的大小(10-20之间的值)
|
|
|
+ return new Font(fontName, style, size);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Color randomColor() {
|
|
|
+ int r = random.nextInt(225);
|
|
|
+ int g = random.nextInt(225);
|
|
|
+ int b = random.nextInt(225);
|
|
|
+ return new Color(r, g, b);
|
|
|
+ }
|
|
|
+
|
|
|
+ private char randomChar() {
|
|
|
+ //A-Z,a-z,0-9,可剔除一些难辨认的字母与数字
|
|
|
+ String str = "0123456789ABCdefghiDEFGHIJopPQRVWXYZabcjklSTUmnqrstKLMNOvuwxyz";
|
|
|
+ return str.charAt(random.nextInt(str.length()));
|
|
|
+ }
|
|
|
+}
|