派生自 projectDept/qhighschool

EricsHu
2022-12-05 068fc7f2e81178e55fa191a13709af64b1a163f6
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
package com.qxueyou.scc.base.util;
 
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
 
import javax.imageio.ImageIO;
 
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
//import com.qxueyou.scc.base.service.IConfigService;
import com.qxueyou.scc.shorturl.util.ShortUrlUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
 
/**
 * 二维码生成工具类
 * 
 * @author 德虎
 * 
 */
public final class QrCodeUtils {
    
    private static Logger log = LogManager.getLogger(QrCodeUtils.class);
 
    /**
     * 生成二维码图片并输出到流
     * @param msg 需要生成的二维码信息
     * @param os 二维码图片输出流,输出后会自动关闭
     * @param logoFlag 是否添加logo
     * 
     */
    public static void createQRCodeImgAndSend(String msg, OutputStream os, boolean logoFlag) {
        try {
            //如果是网址,则转换为短网址
            /*if(ShortUrlUtils.IsUrl(msg)){
                IShortUrlService shortUrlService = SpringUtil.getBean(IShortUrlService.class);
                ShortUrl oldShortUrl = shortUrlService.queryShortUrl(msg);
                if(oldShortUrl != null){//判断是否已经存在数据
                    msg = oldShortUrl.getShortUrl();
                }else{
                    //获取域
                    String domain = SpringUtil.getBean(IConfigService.class).getConfigByEnv("domain-name");
                    //获取短链接编码
                    String strShortUrlCode = ShortUrlUtils.shortUrl(msg);
                    //拼接成短链接
                    String shortUrl = domain+(domain.indexOf("/qxueyou", 10)>=0?"/s/":"/qxueyou/s/")+strShortUrlCode;
                    ShortUrl objShortUrl = new ShortUrl();
                    //保存短链接的值
                    objShortUrl.setShortUrl(shortUrl);
                    objShortUrl.setLongUrl(msg);
                    objShortUrl.setShortUrlCode(strShortUrlCode);
                    objShortUrl.setDeleteFlag(false);
                    TraceUtils.setCreateTrace(objShortUrl);
                    msg = shortUrl;
                    shortUrlService.saveShortUrl(objShortUrl);
                }
            }*/
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            
            Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(
                    2);
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
 
            BitMatrix bitMatrix = multiFormatWriter.encode(
                    msg,
                    BarcodeFormat.QR_CODE, 300, 300, hints);
            MatrixToImageConfig config = new MatrixToImageConfig(0xFF000000, -1);
            //添加机构图标
            if(StringUtils.isNoneBlank(ClientUtils.getOrgLogoPath()) && ShortUrlUtils.IsUrl(msg) && logoFlag){
                int width = bitMatrix.getWidth();  
                int height = bitMatrix.getHeight();  
                BufferedImage image = new BufferedImage(width, height,  
                        BufferedImage.TYPE_INT_RGB);  
                for (int x = 0; x < width; x++) {  
                    for (int y = 0; y < height; y++) {  
                        image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000  
                                : 0xFFFFFFFF);  
                    }  
                } 
                addLogo_QRCode(image, os);
            }else{
                MatrixToImageWriter.writeToStream(bitMatrix, "PNG",
                        os, config);
            }
 
        } catch (WriterException e) {
            log.error(e, e);
        } catch (IOException e) {
            log.error(e, e);
        } catch (Exception e) {
            log.error(e, e);
        }
    }
    
    /**
     * 添加机构logo
     * 
     * @param buf 生成的二维码
     * @return
     * @throws IOException 
     */
    public static void addLogo_QRCode(BufferedImage buf, OutputStream os) throws IOException{
        CloseableHttpClient httpClient = null;
        HttpGet httpget = null;
        try{
//            String domain = SpringUtil.getBean(IConfigService.class).getConfigByEnv("oss-domain");
            String domain =null;
            httpClient = HttpClients.createDefault();
            //获取机构logo
            httpget = new HttpGet(domain+ClientUtils.getOrgLogoPath());  
            HttpResponse response = httpClient.execute(httpget);
            HttpEntity entity = response.getEntity();
            /**
            * 读取Logo图片
            */
            BufferedImage logo = ImageIO.read(entity.getContent());
            /**
            * 读取二维码图片,并构建绘图对象
            */
            Graphics2D g = buf.createGraphics();
            int widthLogo = 65, heightLogo = 65;
        
            // 计算图片放置位置
            int x = (buf.getWidth() - widthLogo) / 2;
            int y = (buf.getHeight() - heightLogo) / 2;
        
            //开始绘制图片
            g.setColor(Color.WHITE);
            g.drawImage(logo, x, y, widthLogo, heightLogo, null);
            g.setStroke(new BasicStroke(1));  
            g.dispose();
            ImageIO.write(buf, "PNG", os);
        }catch (Exception e) {
            ImageIO.write(buf, "PNG", os);
            log.error("二维码添加logo失败"+e);
        }finally {
            httpClient.close();
        }
    }
}