Helius
2021-06-29 5252d1396e21a16774be699a5ba1c8d39c14a22e
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
package com.xzx.gc.common.utils.image;
 
 
import cn.hutool.core.io.FileUtil;
import com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.utils.BusinessUtil;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
 
@Component
@Slf4j
public class GraphicsUtils {
 
    @Autowired
    private BusinessUtil businessUtil;
 
    /**
     * @param imgName       图片名称
     * @param codePath      二维码路径
     * @param avata         头像全路径
     * @param backgroundUrl 背景图地址
     * @param spuName       文字
     * @throws Exception
     */
    public Map<String, String> generatePoster(String imgName, String codePath, String avata,
                                              String backgroundUrl, String spuName, Integer x1, Integer y1, Integer x2, Integer y2, Integer x3, Integer y3, int size) {
        String urlPrefix = null;
        String imgPath = null;
        try {
            urlPrefix = businessUtil.getViewUrl();
            imgPath = Constants.IMG_UPLOAD_PATH + imgName;
            if (!FileUtil.exist(imgPath)) {
                //背景图
                BufferedImage bufferImage = QrCodeBaseUtils.imageToBufferedImage(backgroundUrl);
                Graphics2D graphics = bufferImage.createGraphics();
                graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                //小程序码
                BufferedImage qrCodeImage = ImageIO.read(new File(codePath));
                graphics.drawImage(qrCodeImage, x1, y1, null);
                //头像
                if (x2 != null) {
                    BufferedImage logoBufferImage = Thumbnails.of(new URL(avata)).size(150, 150).asBufferedImage();
                    graphics.drawImage(logoBufferImage, x2, y2, null);
                }
                //文字
                if (x3 != null) {
                    Font font = new Font("黑体", Font.BOLD, size);
                    graphics.setFont(font);
                    graphics.setPaint(Color.black);
                    graphics.drawString(spuName, x3, y3);
                }
 
                QrCodeGraphicsUtils.savePic(bufferImage, 1, "jpg", 0.8, imgPath);
                graphics.dispose();
                File file = new File(imgPath);
                long uploadUrl = FileUtil.size(file);
                //小于10kb重新生成
                if (uploadUrl <= Constants.MIN_FILE_SIZE) {
                    log.error("生成海报失败:图片大小异常:{}", uploadUrl);
                    return null;
                }
            } else {
                //判断文件是否正常 不正常 删除
                File file = new File(imgPath);
                long uploadUrl = FileUtil.size(file);
                if (uploadUrl <= Constants.MIN_FILE_SIZE) {
                    FileUtil.del(file);
                    return null;
                }
            }
        } catch (Exception e) {
            return null;
        }
        log.debug("生成海报成功,路径:" + imgPath);
        Map<String, String> map = new HashMap<>();
        map.put("viewUrl", urlPrefix + "/" + imgName);
        map.put("uploadUrl", imgPath);
        return map;
 
    }
 
 
//    public static void createPosterByRedTemplate(byte[] linkUrl, boolean logoStatus, String logoPath,
//                                                 String backgroundUrl, String spuPicUrl, String spuLeaguerPrice, String spuPrice, String spuName)
//            throws Exception {
//        // qrCode
////        BufferedImage qrCodeImage = QrCodeGraphicsUtils.createQrCode(linkUrl, false, logoStatus, logoPath, true, 160);
//        ByteArrayInputStream in = new ByteArrayInputStream(linkUrl);
////        BufferedImage qrCodeImage = ImageIO.read(in );
//
//        File file = FileUtil.writeFromStream(in, new File("d:/a.jpg"));
//
//        // 海报背景
//        BufferedImage bufferImage = QrCodeBaseUtils.imageToBufferedImage(backgroundUrl);
//
//        Graphics2D graphics = bufferImage.createGraphics();
//        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//        graphics = bufferImage.createGraphics();
//        // 绘制 qrCode
////        graphics.drawImage(qrCodeImage, 437, 616, null);
//
//        // 绘制 头像
//        // graphics = QrCodeGraphicsUtils.drawAvatar(graphics, logoPath, bufferImage, 32, 714);
//
//        // 商品主图
//        BufferedImage spuPicBufferImage = null;
//        spuPicBufferImage = QrCodeBaseUtils.imageToBufferedImage(spuPicUrl);
//        // 绘制商品主图
//        // graphics.drawImage(spuPicBufferImage, 244, 249, null);
//
//        // 文本
//        //  QrCodeGraphicsUtils.drawRightTextNewLine(graphics, spuName, 235, 132, 35, 346, Color.WHITE, 24, 2, 350);
//
//        // 会员特价
//        Font font = new Font("微软雅黑", Font.PLAIN, 26);
//        graphics.setFont(font);
//        //  QrCodeGraphicsUtils.drawText(graphics, spuLeaguerPrice, 232, 65, Color.WHITE);
//        // 原价
//        Font font2 = new Font("微软雅黑", Font.PLAIN, 22);
//        graphics.setFont(font2);
//        //   QrCodeGraphicsUtils.drawText(graphics, spuPrice, 232, 100, Color.WHITE);
//
//        graphics.dispose();
//
//        ByteArrayOutputStream os = new ByteArrayOutputStream();
//        ImageIO.write(bufferImage, "JPG", os);
//        byte[] arrayImage = os.toByteArray();
//        InputStream inputStream = new ByteArrayInputStream(arrayImage);
//        if (os != null) {
//            os.close();
//        }
//        if (inputStream != null) {
//            inputStream.close();
//        }
//
//        QrCodeGraphicsUtils.savePic(bufferImage, 1, "jpg", 0.8, System.currentTimeMillis() + "");
//    }
 
}