派生自 projectDept/qhighschool

Administrator
2022-11-29 8c99e2d8b6c1e0d9cde6abbe80b4df75be19f6d1
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();
        }
    }
}