k94314517
2024-10-10 d0c4ab05405817381fe6df28d6b4aa15dceef260
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package com.doumee.core.utils;
 
import com.google.zxing.common.BitMatrix;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
 
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URL;
 
public class GeneratePicUtil {
 
    static String bg="https://shoeslxkj.oss-cn-beijing.aliyuncs.com/file/20230404/SysBaseBackGround/D56231512C0B4A4C9D06B02F9566B0B0.png";
 
    static String headUrl = "https://shoeslxkj.oss-cn-beijing.aliyuncs.com/file/20230404/SysBaseBackGround/B802B64FE31447DCB030F13ECC7CA9F9.png";
 
    private static float jPEGcompression = 0.75f;// 图片清晰比率
    /**
     * test
     *
     * @param args
     * @throws
     */
    public static void main(String[] args) {
        long starttime = System.currentTimeMillis();
        System.out.println("开始:" + starttime);
        try {
//            String a="https://shoeslxkj.oss-cn-beijing.aliyuncs.com/file/20230403/SysBaseBackGround/883D63E108F44FC68ED856B42B9AB063.png";
//            generateImg(a,"D:/","baidu.com","清澄白河马克杯-节日版定制日版定制", BigDecimal.valueOf(129.88) );
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("结束:" + (System.currentTimeMillis() - starttime) / 1000);
    }
 
    private static final int width = 898;
    private static final int height = 1308;
 
    /**
     * 生成分享图片
     * @param headImg 头部图片
     * @param wxQrCodeImg 微信小程序二维码
     * @param goodsName 商品名称
     * @param price  商品价格
     * @return
     * @throws Exception
     */
    public static InputStream generateImg(String headImg,String wxQrCodeImg, String goodsName, BigDecimal price) throws Exception {
        BufferedImage bi =  new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //得到它的绘制环境(这张图片的笔)
        Graphics2D g2 = (Graphics2D) bi.getGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        g2.fillRect(0, 0, width, height);
        //背景图片
        BufferedImage titles = ImageIO.read(new URL(bg));
        g2.drawImage(titles, 1, 1, 894, 1305, null);
        g2.drawRect(0, 0, width - 1, height - 1);
        //头部图片
        BufferedImage headBg = ImageIO.read(new URL(headImg));
        g2.drawImage(headBg, 1, 1, 894, 900, null);
        g2.drawRect(0, 0, width - 1, height - 1);
 
        //商品名称
        if(goodsName.length()>13){
            g2.setColor(Color.black);
            g2.setFont(new Font("黑体", Font.PLAIN, 40));
            g2.drawString(goodsName.substring(0,13), 46, 1000);
            g2.setColor(Color.black);
            g2.setFont(new Font("黑体", Font.PLAIN, 40));
            g2.drawString(goodsName.substring(13), 46, 1054);
        }else{
            g2.setColor(Color.black);
            g2.setFont(new Font("黑体", Font.PLAIN, 40));
            g2.drawString(goodsName, 46, 1020);
        }
 
        //商品价格
        g2.setColor(Color.red);
        g2.setFont(new Font("黑体", Font.PLAIN, 30));
        g2.drawString("¥", 620, 1020);
        BigDecimal integerPart = price.setScale(0, RoundingMode.DOWN);
        BigDecimal fractionalPart  = price.subtract(integerPart);
        if(price.compareTo(new BigDecimal(1000))>=0){
            //整数部分
            g2.setColor(Color.red);
            g2.setFont(new Font("黑体", Font.PLAIN, 40));
            g2.drawString(price.setScale(0, RoundingMode.DOWN).toString(), 660, 1020);
            //小数部分
            g2.setColor(Color.red);
            g2.setFont(new Font("黑体", Font.PLAIN, 30));
            g2.drawString(fractionalPart.toString().substring(1), 740, 1020);
        }else if(price.compareTo(new BigDecimal(100))>=0){
            //整数部分
            g2.setColor(Color.red);
            g2.setFont(new Font("黑体", Font.PLAIN, 40));
            g2.drawString(price.setScale(0, RoundingMode.DOWN).toString(), 660, 1020);
            //小数部分
            g2.setColor(Color.red);
            g2.setFont(new Font("黑体", Font.PLAIN, 30));
            g2.drawString(fractionalPart.toString().substring(1), 720, 1020);
        }else if(price.compareTo(new BigDecimal(10))>=0){
            //整数部分
            g2.setColor(Color.red);
            g2.setFont(new Font("黑体", Font.PLAIN, 40));
            g2.drawString(price.setScale(0, RoundingMode.DOWN).toString(), 660, 1020);
            //小数部分
            g2.setColor(Color.red);
            g2.setFont(new Font("黑体", Font.PLAIN, 30));
            g2.drawString(fractionalPart.toString().substring(1), 700, 1020);
        }else{
            //整数部分
            g2.setColor(Color.red);
            g2.setFont(new Font("黑体", Font.PLAIN, 40));
            g2.drawString(price.setScale(0, RoundingMode.DOWN).toString(), 660, 1020);
            //小数部分
            g2.setColor(Color.red);
            g2.setFont(new Font("黑体", Font.PLAIN, 30));
            g2.drawString(fractionalPart.toString().substring(1), 680, 1020);
        }
        //设置二维码图片
        BufferedImage barcodeCode = ImageIO.read(new URL(wxQrCodeImg));
        g2.drawImage(barcodeCode, 670, 1100, 180, 180,null);
        g2.drawRect(0, 0, width - 1, height - 1);
        InputStream inputStream = bufferedImageToInputStream(bi);
        return inputStream;
//        String fileName = "8701EAC36DEC405A94C530F0E6796589.jpg"; //ID.nextGUID() + ".jpg";
//        GeneratePicUtil.saveFile(inputStream, filePath+ File.separator+fileName);
//        return filePath + File.separator+ fileName;
    }
 
    public  static MultipartFile getMultipartFile(InputStream inputStream, String fileName) {
        FileItem fileItem = createFileItem(inputStream, fileName);
        return new CommonsMultipartFile(fileItem);
    }
 
    /**
     * FileItem类对象创建
     *
     * @param inputStream inputStream
     * @param fileName    fileName
     * @return FileItem
     */
    public static FileItem createFileItem(InputStream inputStream, String fileName) {
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        String textFieldName = "file";
        FileItem item = factory.createItem(textFieldName, MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        OutputStream os = null;
        //使用输出流输出输入流的字节
        try {
            os = item.getOutputStream();
            while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            inputStream.close();
        } catch (IOException e) {
            throw new IllegalArgumentException("文件上传失败");
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();;
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();;
                }
            }
        }
 
        return item;
    }
 
    public static void saveFile(InputStream is, String fileName) throws IOException {
        try (BufferedInputStream in = new BufferedInputStream(is);
             BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fileName))) {
            int len;
            byte[] b = new byte[1024];
            while ((len = in.read(b)) != -1) {
                out.write(b, 0, len);
            }
        }
    }
 
    public static InputStream bufferedImageToInputStream(BufferedImage backgroundImage) throws IOException {
        return bufferedImageToInputStream(backgroundImage, "jpg");
    }
 
    /**
     * backgroundImage 转换为输出流
     *
     * @param backgroundImage
     * @param format
     * @return
     * @throws IOException
     */
    public static InputStream bufferedImageToInputStream(BufferedImage backgroundImage, String format) throws IOException {
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        try (
                ImageOutputStream
                        imOut = ImageIO.createImageOutputStream(bs)) {
            ImageIO.write(backgroundImage, format, imOut);
            InputStream is = new ByteArrayInputStream(bs.toByteArray());
            return is;
        }
    }
 
    /**
     * 传入的图像必须是正方形的 才会 圆形 如果是长方形的比例则会变成椭圆的
     *
     * 用户头像地址
     * @return
     * @throws IOException
     */
    public static BufferedImage convertCircular(BufferedImage bi1) throws IOException {
 
        BufferedImage bi2 = new BufferedImage(bi1.getWidth(), bi1.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
 
        Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, bi1.getWidth(), bi1.getHeight());
 
        Graphics2D g2 = bi2.createGraphics();
 
        g2.setClip(shape);
 
        // 使用 setRenderingHint 设置抗锯齿
 
        g2.drawImage(bi1, 0, 0, null);
 
        // 设置颜色
 
        g2.setBackground(Color.green);
 
        g2.dispose();
 
        return bi2;
 
    }
 
 
    //去除二维码白边
    public static BitMatrix deleteWhite(BitMatrix matrix) {
        int[] rec = matrix.getEnclosingRectangle();
        int resWidth = rec[2] + 1;
        int resHeight = rec[3] + 1;
 
        BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
        resMatrix.clear();
        for (int i = 0; i < resWidth; i++) {
            for (int j = 0; j < resHeight; j++) {
                if (matrix.get(i + rec[0], j + rec[1])) resMatrix.set(i, j);
            }
        }
        return resMatrix;
    }
 
 
 
 
}