yujian
2019-05-20 fa5a54ef993f05cf353e13652eec4bb15e2a9d66
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
package org.fanli.dynamic;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.RenderingHints.Key;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
 
import javax.imageio.ImageIO;
 
import org.junit.Test;
import org.yeshi.utils.DateLunarUtil;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.QRCodeUtil;
import org.yeshi.utils.entity.DateInfo;
 
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.FileUtil;
import com.yeshi.fanli.util.ImageUtil;
import com.yeshi.fanli.util.StringUtil;
 
public class Test_DrawInviteMaterial {
 
    @Test
    public void draw1() {
        try {
            long uid = 911382;
            String targetPath = FileUtil.getCacheDir() + "/share_" + uid +".jpg";
            String erCodeTempPath = FileUtil.getCacheDir() + "/" + uid + "_" + System.currentTimeMillis() + ".jpg";
 
            
            String erCode = HttpUtil.getShortLink("http://" + Constant.wxGZConfig.getLoginHost() + "/"
                    + Constant.systemCommonConfig.getProjectName() + "/client/threeShareNew?uid=" + uid);
            // 生成
            try {
                QRCodeUtil.getInstance(250).encode(erCode, erCodeTempPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
 
            
            InputStream erCodeInputStream = new FileInputStream(new File(erCodeTempPath)); // 二维码
            // 开始根据 url(背景图), qrCode(二维码), portrait(头像) 生成图片
            inviteFriendImgLower(erCodeInputStream, targetPath, null);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 
     * @param urlInputStream
     * @param portraitInputStream
     * @param erCodeInputStream
     * @param targetPath
     * @param pX                  -二维码的横坐标
     * @param pY                  -二维码的纵坐标
     * @param size                -二维码的尺寸
     * @param inviteCode          -邀请码
     * @throws IOException
     */
    // 邀请好友图片 - 底部
    public static void inviteFriendImgLower(InputStream erCodeInputStream, String targetPath, Date date) throws IOException {
 
        String inviteCode = "LKFK5P";
 
        BufferedImage bgImage = ImageIO
                .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/invite/bg.jpg"));
 
        final BufferedImage targetImg = new BufferedImage(bgImage.getWidth(), bgImage.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        HashMap<Key, Object> mapH = new HashMap<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.setBackground(Color.BLACK);
        
        // 画 蒙层
        BufferedImage mengCeng = ImageIO
                .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/invite/mc.png"));
        g2d.drawImage(mengCeng, 0, bgImage.getHeight()/2, bgImage.getWidth(), bgImage.getHeight()/2, null);
 
        int width = bgImage.getWidth();
        int height = bgImage.getHeight();
        
        int pX = width - 250;
        int pY = height - 290; // 320
        int size = 210; // 230
        // 画 二维码
        BufferedImage qrCodeImage = ImageIO.read(erCodeInputStream);
        qrCodeImage = ImageUtil.qrCodeImage(g2d, qrCodeImage, pX, pY, size, size); // 二维码长宽
        // 230*230
        
 
        // 画 头像
        // BufferedImage portraitImg = ImageIO.read(portraitInputStream);
        BufferedImage portraitImg = ImageIO
                .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/invite/hd.jpg"));
 
        
        int portraitSize = size * 5 / 23;
 
        int pPX = pX + size / 2 - portraitSize / 2;
        int pPY = pY + size / 2 - portraitSize / 2;
        portraitImg = ImageUtil.portraitImg(g2d, portraitImg, pPX, pPY, portraitSize, portraitSize);// 头像长宽
 
        // 文字白色
        Color color = new Color(255, 255, 255);
        String os = System.getProperty("os.name");
        String fontBoldPath = "/usr/share/fonts/yahei.ttf";
        if (os.toLowerCase().startsWith("win")) {
            fontBoldPath = "D:/yahei.ttf";
        }
        
        Font boldFont = null;
        // 画邀请码(邀请码的大小与字体固定)
        if (!StringUtil.isNullOrEmpty(inviteCode)) {
            String st = "邀请码:" + inviteCode;
            try {
                boldFont = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(32.0f);
            } catch (FontFormatException e) {
                e.printStackTrace();
            }
 
            g2d.setFont(boldFont);
            g2d.setColor(new Color(255, 255, 255));
 
            FontMetrics fm = g2d.getFontMetrics(boldFont);
            int textLength = fm.stringWidth(st);
 
            int ax = (width - textLength) - 30;
            int ay = bgImage.getHeight() - 50 + boldFont.getSize() / 2 - 5;
 
            g2d.setColor(new Color(255, 255, 255));
            g2d.drawString(st, ax, ay);
 
        }
        
        int padding_left = 30;
        
        // 日历
        try {
            if (date == null) {
                date = new Date();
            }
            
            boldFont = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(36.0f);
            g2d.setFont(boldFont);
            g2d.setColor(color);
            
            DateInfo dateInfo = DateLunarUtil.getDateInfo(date);
            // 农历
            g2d.drawString(dateInfo.getLunar_month() + dateInfo.getLunar_day(), padding_left, height - 80 );
            // 天干地支
            g2d.drawString(dateInfo.getAgenary_month() +" " + dateInfo.getAgenary_day(), padding_left, height - 140 );
            
            // 当前日期
            Font boldFontMax = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(100.0f);
            g2d.setFont(boldFontMax);
            
            if (dateInfo.getDay() < 10) {
                g2d.drawString("0" + dateInfo.getDay().toString(), padding_left, height - 200 );
            } else {
                g2d.drawString(dateInfo.getDay().toString(), padding_left, height - 200 );
            }
            
            
            String theDate= "";
            if (dateInfo.getMonth() < 10) {
                theDate ="/0";
            } else {
                theDate ="/";
            }
            theDate += dateInfo.getMonth() + "/"+ dateInfo.getYear() + " " + dateInfo.getWeekDay();
            g2d.setFont(boldFont);
            g2d.drawString(theDate, padding_left + boldFontMax.getSize() + 15, height - 200 );
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        
 
        
        // 文字内容
        try {
            boldFont = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(36.0f);
            g2d.setFont(boldFont);
            g2d.setColor(color);
            
            String content = "对自己严格一点儿,时间长了,努力便成为一种心理习惯,一种生活方式。未必人人都可以做到优秀,但至少可以努力做到比昨天的自己更优秀。";
            System.out.println(content.length());
            int lineHeight = 70;
            int lineTextLength = 19;
            
            if (content.length() <= lineTextLength) {
                g2d.drawString(content, padding_left, height - 350 );
            } else {
                String text = "";
                int mod = content.length() % lineTextLength;
                if (mod == 0) {
                    text = content.substring(content.length() -lineTextLength,content.length());
                    content = content.substring(0, content.length() -lineTextLength);
                } else {
                    text = content.substring(content.length() -mod ,content.length());
                    content = content.substring(0, content.length() -mod);
                }
                g2d.drawString(text, padding_left, height - 350);
                
                
                int i = 1;
                while (true) {
                    if (content.length() <= 17) {
                        g2d.drawString(content, padding_left, height - 350 - lineHeight * i);
                        break;
                    } else {
                        text = content.substring(content.length() -lineTextLength,content.length());
                        content = content.substring(0, content.length() -lineTextLength);
                        g2d.drawString(text, padding_left, height - 350 - lineHeight * i);
                        i ++;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
 
        OutputStream out = new FileOutputStream(new File(targetPath));
        ImageIO.write(targetImg, "JPEG", out);
        out.flush();
        out.close();
    }
 
}