shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.iplatform.base.util;
 
import com.iplatform.base.PlatformRuntimeException;
import com.walker.infrastructure.utils.Base64Utils;
import com.walker.infrastructure.utils.FileCopyUtils;
import com.walker.infrastructure.utils.StringUtils;
 
import javax.imageio.ImageIO;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.SecureRandom;
//import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
 
/**
 * 移植对象,拼图滑块对象处理工具,后续需要重构优化。
 * @author 时克英
 * @date 2023-04-06
 */
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 = StringUtils.EMPTY_STRING;
        try {
            ImageIO.write(bi, "png", outputStream);
//            res = Base64.getEncoder().encodeToString(outputStream.toByteArray());
            res = Base64Utils.encode(outputStream.toByteArray());
            res = "data:image/png;base64," + res;
        } catch (Exception e) {
            throw new PlatformRuntimeException("验证码图片传base64错误:" + e.getMessage(), e);
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return res;
    }
 
    /**
     * hash 转换
     *
     * @param base64 String 图片流
     * @return String
 
     * @since 2020-06-03
     */
    public static String getBase64Image(String base64) {
        return "data:image/png;base64," + base64;
    }
 
    /*
     * @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;
    }
 
    /**
     * base64 字符串转图片
     *
     * @param base64String
     * @return
     */
    public static BufferedImage getBase64StrToImage(String base64String) {
        ByteArrayInputStream inputStream = null;
        try {
//            Base64.Decoder decoder = Base64.getDecoder();
//            byte[] bytes = decoder.decode(base64String);
            byte[] bytes = Base64Utils.decode(base64String);
            inputStream = new ByteArrayInputStream(bytes);
            return ImageIO.read(inputStream);
        } catch (Exception e) {
            throw new PlatformRuntimeException("验证码base64转文件错误:" + e.getMessage(), e);
        } finally {
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 
    public static void writeBase64ToFile(String base64String, File outFile){
        BufferedOutputStream outputStream = null;
        try{
            byte[] bytes = Base64Utils.decode(base64String);
            outputStream = new BufferedOutputStream(new FileOutputStream(outFile));
            FileCopyUtils.copy(bytes, outputStream);
        }catch (Exception ex){
            throw new PlatformRuntimeException("文件base64保存文件错误:" + ex.getMessage() + ", file=" + outFile.getAbsolutePath(), ex);
        }
    }
}