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();
|
}
|
|
}
|