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
package com.iplatform.base.captcha;
 
import com.iplatform.base.util.VerifyImgUtil;
import com.walker.web.CaptchaResult;
import com.walker.web.CaptchaType;
 
import java.awt.image.BufferedImage;
import java.util.Map;
 
/**
 * 拼图滑动验证码,提供者实现。<p></p>
 * 所有验证拼图图像在初始化时加载如内存,提高性能。
 * @author 时克英
 * @date 2023-04-06
 */
//public class JigsawCaptchaProvider implements CaptchaProvider<CaptchaResult> {
public class JigsawCaptchaProvider extends AbstractCaptchaProvider {
 
    private final static int IMAGE_BG_SIZE = 10;
    private final static int IMAGE_BLOCK_SIZE = 21;
 
    /**
     * 不能缓存图片,因为要在原图上扣背景,因此必须每次加载原始图片。
     * @date 2023-06-27
     */
    public JigsawCaptchaProvider(){
//        String imageName = null;
//        try{
//            BufferedImage image = null;
//            for(int i=1; i<IMAGE_BG_SIZE+2; i++){
//                imageName = "images/jigsaw_bg/" + i + ".jpg";
//                image = this.loadBufferedImage(imageName);
//                this.imageCacheMap.put(imageName, image);
//            }
//
//            for(int j=1; j<IMAGE_BLOCK_SIZE+2; j++){
//                imageName = "images/jigsaw_block/" + j + ".png";
//                image = this.loadBufferedImage(imageName);
//                this.imageCacheMap.put(imageName, image);
//            }
//            logger.info("共加载验证码拼图文件'{}'个", this.imageCacheMap.size());
//
//        } catch (Exception ex){
//            throw new ApplicationRuntimeException("验证码图片加载错误,imageName=" + imageName, ex);
//        }
    }
 
    @Override
    public CaptchaResult generateCaptcha(Object param) {
        int n = this.bgRandom.nextInt(10) + 1; //背景
        int m = this.blockRandom.nextInt(21) + 1; //滑块
 
        try{
            //原图
            String sourceName = "images/jigsaw_bg/" + n + ".jpg";
//        BufferedImage sourceImg = this.imageCacheMap.get(sourceName);
            BufferedImage sourceImg = this.loadBufferedImage(sourceName);
            //抠图模版
            String blockName = "images/jigsaw_block/" + m + ".png";
            BufferedImage templateImg = this.loadBufferedImage(blockName);
            if(sourceImg == null || templateImg == null){
                logger.error("未加载到拼图验证图片资源:" + sourceName + ", or " + blockName);
                return null;
            }
 
            int w = templateImg.getWidth();
            int h = templateImg.getHeight();
 
            //获取抠图坐标
            Map<String, Integer> coord = VerifyImgUtil.generateCutoutCoordinates(w, h);
            int x = coord.get("x");
            int y = coord.get("y");
 
            //获取抠图图形矩阵
            int[][] templateData = VerifyImgUtil.getTemplateData(templateImg);
            //抠图
            Map<String, BufferedImage> r = VerifyImgUtil.initSlidingBlock(sourceImg, templateData, x, y, w, h);
 
//            Map result = new HashMap();
//            result.put("y", y);
//            result.put("slider", VerifyImgUtil.getBase64(r.get("newTemplateImg")));
//            result.put("bg", VerifyImgUtil.getBase64(r.get("newSourceImg")));
 
            JigsawResult jigsawResult = new JigsawResult();
            jigsawResult.setX(x);
            jigsawResult.setY(y);
            jigsawResult.setImageSourceBase64(VerifyImgUtil.getBase64(r.get("newSourceImg")));
            jigsawResult.setImageBlockBase64(VerifyImgUtil.getBase64(r.get("newTemplateImg")));
            return jigsawResult;
 
        } catch (Exception ex){
            logger.error("处理拼图验证图像错误, blockName={}", ex);
            return null;
        }
    }
 
    @Override
    public CaptchaType getCaptchaType() {
        return CaptchaType.Jigsaw;
    }
}