1
zj
2 days ago 5e57de9b12ee136e45ce5754c7fe2e7eb12af05a
trading-order-service/src/main/java/com/yami/trading/service/user/impl/QRGenerateServiceImpl.java
@@ -1,6 +1,7 @@
package com.yami.trading.service.user.impl;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -32,10 +33,12 @@
    @Override
    public String generate(String content) {
        String image_name = "/qr/" + content + ".png";
        content = Constants.WEB_URL + "/register.html?usercode=" + content;
        final String userCode = content;
        content = Constants.buildRegisterInviteLink(userCode);
        boolean openButton = sysparaService.find("short_url_open_button").getBoolean() ;
        if(openButton) {
            content = sysparaService.find("agent_qr_url").getSvalue() + "/register.html?usercode=" + content;
            String agentBase = Constants.normalizeWebUrl(sysparaService.find("agent_qr_url").getSvalue());
            content = agentBase + "/#/register?usercode=" + userCode;
            boolean isCn = sysparaService.find("short_url_cn_button").getBoolean() ;
            if(isCn) {
                content = shortUrlCn(content);
@@ -49,11 +52,11 @@
        int width = 260;
        int height = 260;
        String format = "png";
        Map hints = new HashMap();
        Map<EncodeHintType, Object> 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);
            writeQrImageToFile(bitMatrix, format, file);
        } catch (Exception e) {
            log.error("write to image error:", e);
        }
@@ -89,11 +92,11 @@
        int width = 260;
        int height = 260;
        String format = "png";
        Map hints = new HashMap();
        Map<EncodeHintType, Object> 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);
            writeQrImageToFile(bitMatrix, format, file);
        } catch (Exception e) {
           log.error("write to image error:", e);
        }
@@ -104,20 +107,20 @@
    @Override
    public String generate185(String content) {
        String image_name = "/qr/" + content + "2.png";
        content = Constants.WEB_URL + "/register.html?usercode=" + content;
        content = Constants.buildRegisterInviteLink(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();
        Map<EncodeHintType, Object> 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);
            writeQrImageToFile(bitMatrix, format, file);
        } catch (Exception e) {
           log.error("write to image error:", e);
        }
@@ -151,19 +154,15 @@
    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);
        File file = new File(filepath);
        int width = 260;
        int height = 260;
        String format = "png";
        Map hints = new HashMap();
        Map<EncodeHintType, Object> 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);
            writeQrImageToFile(bitMatrix, format, file);
        } catch (Exception e) {
           log.error("write to image error:", e);
        }
@@ -188,4 +187,18 @@
        return list_image;
    }
    private void writeQrImageToFile(BitMatrix bitMatrix, String format, File file) throws IOException {
        if (bitMatrix == null) {
            throw new IOException("bitMatrix is null");
        }
        if (file == null) {
            throw new IOException("target file is null");
        }
        File parent = file.getParentFile();
        if (parent != null && !parent.exists() && !parent.mkdirs()) {
            throw new IOException("failed to create qr image directory: " + parent.getAbsolutePath());
        }
        MatrixToImageWriter.writeToFile(bitMatrix, format, file);
    }
}