duhuizhe
2024-04-17 c389984b5d3e5523e1b22de1fc045dc415261330
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.yqzx.common.util;
// @formatter:off
 
import lombok.extern.slf4j.Slf4j;
 
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
 
/**
 * .-~~~~~~~~~-._       _.-~~~~~~~~~-.
 * __.'  @Author     ~.   .~    代码无Bug   `.__
 * .'//     liu.q        \./       (秘籍)      \\`.
 * .'// [916000612@qq.com]  |   欲练神功   引刀自宫  \\`.
 * .'// .-~"""""""~~~~-._     |    _,-~~~~"""""""~-.  \\`.
 * .'//.-"  2019-06-24     `-.  |  .-'     15:43       "-.\\`.
 * .'//______.============-..   \ | /   ..-============.______\\`.
 * .'______________________________\|/______________________________`.
 *
 * @Description :
 */
// @formatter:on
@Slf4j
public class VerifyImgUtil {
    private static int T_WIDTH = 320;  //源文件宽度
    private static int T_HEIGHT = 180;  //源文件高度
 
 
    /*
     * @Author : liu.q [916000612@qq.com]
     * @Date : 2019-06-24 15:46
     * @Description : 随机生成抠图坐标
     */
    public static Map<String, Integer> generateCutoutCoordinates(int w, int h) {
        Map<String, Integer> result = new HashMap();
        int x = new SecureRandom().nextInt(T_WIDTH - w);
        int y = new SecureRandom().nextInt(T_HEIGHT - h);
        while (x < w + 10) {
            x = new SecureRandom().nextInt(275);
        }
        result.put("x", x);
        result.put("y", y);
        return result;
    }
 
    /*
     * @Author : liu.q [916000612@qq.com]
     * @Date : 2019-06-24 16:22
     * @Description :生成滑块
     */
    public static Map<String, BufferedImage> initSlidingBlock(BufferedImage sourceImg, int[][] templateImgData, int x, int y, int w, int h) throws Exception {
        // 支持alpha通道的rgb图像
        BufferedImage newTemplateImg = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
 
        for (int i = 0; i < templateImgData.length; i++) {
            for (int j = 0; j < templateImgData[0].length; j++) {
                int rgb = templateImgData[i][j];
                if (rgb == 1) {
                    int rgb_ori = sourceImg.getRGB(x + i, y + j);
                    int r = (0xff & rgb_ori);
                    int g = (0xff & (rgb_ori >> 8));
                    int b = (0xff & (rgb_ori >> 16));
                    rgb_ori = r + (g << 8) + (b << 16) + (255 << 24);
                    newTemplateImg.setRGB(i, j, rgb_ori);
 
                    //创建遮罩层
                    BufferedImage cover = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
                    Graphics2D coverG2 = (Graphics2D) cover.getGraphics();
                    coverG2.setColor(Color.BLACK);
                    coverG2.fillRect(0, 0, 10, 10);
                    coverG2.dispose();
                    Graphics2D bgG2 = (Graphics2D) sourceImg.getGraphics();
                    //开启透明度
                    bgG2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
                    //描绘
                    bgG2.drawImage(cover, i + x, y + j, 1, 1, null);
                    //结束透明度
                    bgG2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
                    bgG2.dispose();
                }
                if (rgb == 0) {
                    //不用搭理
                }
                if (rgb == 2) {
                    //秒边
                    newTemplateImg.setRGB(i, j, 16777215);
                    sourceImg.setRGB(x + i, y + j, 16777215);
                }
            }
        }
        Map<String, BufferedImage> result = new HashMap<>();
        result.put("newTemplateImg", newTemplateImg);
        result.put("newSourceImg", sourceImg);
        return result;
    }
 
 
    /*
     * @Author : liu.q [916000612@qq.com]
     * @Date : 2019-06-21 15:37
     * @Description : 获取base64
     */
    public static String getBase64(BufferedImage bi) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        String res = "";
        try {
            ImageIO.write(bi, "png", outputStream);
            res = Base64.getEncoder().encodeToString(outputStream.toByteArray());
            res = "data:image/png;base64," + res;
            outputStream.close();
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        return res;
    }
 
    /*
     * @Author : liu.q [916000612@qq.com]
     * @Date : 2019-06-24 17:22
     * @Description : 生成抠图图形矩阵
     */
    public static int[][] getTemplateData(BufferedImage bi) {
        int[][] data = new int[bi.getWidth()][bi.getHeight()];
        for (int i = 0; i < bi.getWidth(); i++) {
            for (int j = 0; j < bi.getHeight(); j++) {
                int rgb = bi.getRGB(i, j);
                int leftRgb = -1, rightRgb = -1, upRgb = -1, downRgb = -1;
                if (j > 0)
                    leftRgb = bi.getRGB(i, j - 1);
                if (j < bi.getHeight() - 1)
                    rightRgb = bi.getRGB(i, j + 1);
                if (i > 0)
                    upRgb = bi.getRGB(i - 1, j);
                if (i < bi.getWidth() - 1)
                    downRgb = bi.getRGB(i + 1, j);
                if (rgb == -1 && (i == 0 || i == bi.getWidth() - 1 || j == 0 || j == bi.getHeight() - 1)) {
                    data[i][j] = 2; //边
                } else if (rgb == -1 && (leftRgb != -1 || rightRgb != -1 || upRgb != -1 || downRgb != -1)) {
                    data[i][j] = 2;//边
                } else if (rgb == -1) {
                    data[i][j] = 1;//内容区域
                } else {
                    data[i][j] = 0;//透明区域
                }
            }
        }
        return data;
    }
}