1
zj
4 mins ago b426757e13490d9d88a75faf7ba94455b890f582
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
package com.yami.trading.service.user.impl;
 
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.github.binarywang.utils.qrcode.MatrixToImageWriter;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.yami.trading.common.constants.Constants;
import com.yami.trading.common.http.HttpHelper;
import com.yami.trading.common.util.ImageUtils;
import com.yami.trading.service.QRGenerateService;
import com.yami.trading.service.syspara.SysparaService;
 
import lombok.extern.slf4j.Slf4j;
 
@Slf4j
@Service
public class QRGenerateServiceImpl  implements QRGenerateService {
 
    @Autowired
    private  SysparaService sysparaService;
    
    @Override
    public String generate(String content) {
        String image_name = "/qr/" + content + ".png";
        content = Constants.WEB_URL + "/register.html?usercode=" + content;
        boolean openButton = sysparaService.find("short_url_open_button").getBoolean() ;
        if(openButton) {
            content = sysparaService.find("agent_qr_url").getSvalue() + "/register.html?usercode=" + content;
            boolean isCn = sysparaService.find("short_url_cn_button").getBoolean() ;
            if(isCn) {
                content = shortUrlCn(content);
            }else {
                content = shortUrl(content);
            }
        }
 
        String filepath = Constants.IMAGES_DIR + image_name;
        File file = new File(filepath);
        int width = 260;
        int height = 260;
        String format = "png";
        Map hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            MatrixToImageWriter.writeToFile(bitMatrix, format, file);
        } catch (Exception e) {
            log.error("write to image error:", e);
        }
 
        return image_name;
    }
 
    public String shortUrl(String longUrl) {
        String url = "https://cutt.ly/scripts/shortenUrl.php";
        Map<String,Object> param = new HashMap<>();
        param.put("url",longUrl);
        return HttpHelper.sendPostHttp(url, param, false);
    }
    
    public String shortUrlCn(String longUrl) {
        String url = "https://www.xyixy.com/api/";
        Map<String,Object> param = new HashMap<>();
        param.put("url",longUrl);
        param.put("key",sysparaService.find("cn_short_url_key").getSvalue());
        String s = HttpHelper.sendGetHttp(url, param);
        return s;
    }
    
    public String generate(String content,String imgName) {
        String image_uri = "/qr/" + imgName + ".png";
 
        String filepath = Constants.IMAGES_DIR + image_uri;
        File file = new File(filepath);
//        if(file.exists()) {//存在则删了重新建,保证内容最新,不删除则内容不会覆盖
//            file.delete();
//            file = new File(filepath);
//        }
        int width = 260;
        int height = 260;
        String format = "png";
        Map hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            MatrixToImageWriter.writeToFile(bitMatrix, format, file);
        } catch (Exception e) {
            log.error("write to image error:", e);
        }
 
        return image_uri;
    }
    
    @Override
    public String generate185(String content) {
        String image_name = "/qr/" + content + "2.png";
        content = Constants.WEB_URL + "/register.html?usercode=" + content;
        // String image_name = "/qr/" + UUIDGenerator.getUUID() + ".png";
        String filepath = Constants.IMAGES_DIR + image_name;
        File file = new File(filepath);
        int width = 185;
        int height = 185;
        String format = "png";
        Map hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.MARGIN, 1);// 二维码空白区域,最小为0也有白边,只是很小,最小是6像素左右
 
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            MatrixToImageWriter.writeToFile(bitMatrix, format, file);
        } catch (Exception e) {
            log.error("write to image error:", e);
        }
 
        return image_name;
    }
 
    public void generate_poster(String image_name, String usercode) {
        String backgroundPath = "";
        String smallPath = Constants.IMAGES_DIR + image_name;
        String resultPaht = "";
        for (int i = 0; i < 5; i++) {
            backgroundPath = Constants.IMAGES_DIR + "/poster/poster_" + i + "_zh-CN.png";
            resultPaht = Constants.IMAGES_DIR + "/qr/" + usercode + "_poster_" + i + "_zh-CN.png";
            ImageUtils.image_usercode(backgroundPath, smallPath, "png", resultPaht);
        }
        for (int i = 0; i < 5; i++) {
            backgroundPath = Constants.IMAGES_DIR + "/poster/poster_" + i + "_CN.png";
            resultPaht = Constants.IMAGES_DIR + "/qr/" + usercode + "_poster_" + i + "_CN.png";
            ImageUtils.image_usercode(backgroundPath, smallPath, "png", resultPaht);
        }
        for (int i = 0; i < 5; i++) {
            backgroundPath = Constants.IMAGES_DIR + "/poster/poster_" + i + "_en.png";
            resultPaht = Constants.IMAGES_DIR + "/qr/" + usercode + "_poster_" + i + "_en.png";
            ImageUtils.image_usercode(backgroundPath, smallPath, "png", resultPaht);
        }
 
    }
 
    @Override
    public String generateWithdraw(String content, String address) {
        String image_name = "/qr/" + content + ".png";
        String filepath = Constants.IMAGES_DIR + image_name;
        File file=new File( Constants.IMAGES_DIR);
        if (!file.isDirectory()){
            file.mkdirs();
        }
        file = new File(filepath);
        int width = 260;
        int height = 260;
        String format = "png";
        Map hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(address, BarcodeFormat.QR_CODE, width, height, hints);
            MatrixToImageWriter.writeToFile(bitMatrix, format, file);
        } catch (Exception e) {
            log.error("write to image error:", e);
        }
 
        return image_name;
    }
 
    public List<Map<String, String>> generate_poster_base64(String image_name, String usercode, String img_language) {
        List<Map<String, String>> list_image = new ArrayList<Map<String, String>>();
        
        String backgroundPath = "";
        String smallPath = Constants.IMAGES_DIR + image_name;
        String resultPaht = "";
 
        for (int i = 0; i < 5; i++) {
            backgroundPath = Constants.IMAGES_DIR + "/poster/poster_" + i + "_" + img_language + ".png";
            resultPaht = usercode + "_poster_" + i + "_" + img_language;
            Map<String, String> map_image = new HashMap<String, String>();
            map_image.put(resultPaht, ImageUtils.image_usercodeBase64(backgroundPath, smallPath, "png", resultPaht));
            list_image.add(map_image);
        }
 
        return list_image;
    }
}