admin
2022-10-28 0e9b6603d4ae9d11c1fbc90257ce816c5807b8ff
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
package com.yeshi.makemoney.app.utils;
 
import org.yeshi.utils.QRCodeUtil;
 
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.HashMap;
 
/**
 * @author hxh
 * @title: ImageUtil
 * @description: 绘图工具类
 * @date 2022/4/9 12:43
 */
public class DrawImageUtil {
 
    public static InputStream drawInvite(InputStream bg, String inviteCode, String link) throws Exception {
        BufferedImage bgImage = ImageIO.read(bg);
        final BufferedImage targetImg = new BufferedImage(bgImage.getWidth(), bgImage.getHeight(),
                BufferedImage.TYPE_INT_RGB);
 
        HashMap<RenderingHints.Key, Object> mapH = new HashMap<RenderingHints.Key, Object>();
        // 抗锯齿 (抗锯齿总开关)
        mapH.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 文字抗锯齿
        mapH.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
 
        // 画 背景图片
        final Graphics2D g2d = targetImg.createGraphics();
        g2d.drawImage(bgImage, 0, 0, bgImage.getWidth(), bgImage.getHeight(), null);
        g2d.setRenderingHints(mapH);
        g2d.setBackground(Color.WHITE);
 
        Font font = null;
        //画邀请码
        String st = "我的邀请码:" + inviteCode;
        try {
            font = Font.createFont(Font.PLAIN, DrawImageUtil.class.getClassLoader().getResourceAsStream("fonts/yahei.ttf")).deriveFont(36.0f);
        } catch (FontFormatException e) {
            e.printStackTrace();
        }
        g2d.setFont(font);
        g2d.setColor(new Color(255, 255, 255));
 
 
        FontMetrics fm = g2d.getFontMetrics(font);
        int textLength = fm.stringWidth(st);
 
 
        int ax = (bgImage.getWidth() - textLength) / 2;
        int ay = bgImage.getHeight() - bgImage.getWidth() * 8 / 15 + font.getSize();
 
        g2d.fillRoundRect(ax - 40, ay - 20 - font.getSize(), textLength + 40 * 2, font.getSize() + 25 * 2, font.getSize() + 25 * 2, font.getSize() + 25 * 2);
 
        g2d.setColor(new Color(254, 0, 62));
        g2d.drawString(st, ax, ay);
 
 
        //画二维码背景
        g2d.setColor(new Color(255, 255, 255));
 
        ay = ay - 15 - font.getSize() + (font.getSize() + 17 * 2) * 7 / 4;
 
        g2d.fillRoundRect(ax - 40, ay, textLength + 40 * 2, textLength + 30 * 2, font.getSize() + 17 * 2, font.getSize() + 17 * 2);
 
 
        BufferedImage code = QRCodeUtil.getInstance(500, 500).createImageDeleteWhite(link, null, 0xFFFE003E, 0xFFFFFFFF, true);
 
 
        g2d.drawImage(zoomInImage(code, textLength, textLength), null, ax, ay + 30);
 
 
        ByteArrayOutputStream aos = new ByteArrayOutputStream();
        ImageIO.write(targetImg, "JPEG", aos);
        return new ByteArrayInputStream(aos.toByteArray());
    }
 
    public static BufferedImage zoomInImage(BufferedImage originalImage, int width, int height) {
        int type = originalImage.getType();
        if (type == 0) {
            type = 5;
        }
        BufferedImage newImage = new BufferedImage(width, height, type);
        Graphics g = newImage.getGraphics();
        g.drawImage(originalImage, 0, 0, width, height, null);
        g.dispose();
        return newImage;
    }
    
}