首先确定一下我们需要实现的效果图
核心代码如下:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URL;
import javax.imageio.ImageIO;
public void drawCouponPosterImage(String host, Long userId, GoodsInfoVO vo,String filePath) {
try {
String pngString = vo.getPicUrl().substring(vo.getPicUrl().length() - 3);
String savePath = ZshopConstants.UploadFilesConstants.STATICFILESTEMPPATH;
String imageName = System.currentTimeMillis() + ".png";
if (pngString.equals("png")) {
URL url = new URL(vo.getPicUrl());
InputStream is = url.openStream();
byte[] buffer = new byte[1024];
int len = 0;
File imageFile = new File(savePath + File.separator + imageName);
OutputStream os = new FileOutputStream(imageFile);
if (!imageFile.exists()) {
imageFile.mkdirs();
}
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
os.close();
is.close();
System.out.println("保存图片");
}
JSONObject json = new JSONObject();
json.put("userId", userId);
json.put("goodsId", vo.getGoodsId());
// 平台
String platPath = host + "/resources/images/invite/taobao.png";
if ("tmall".equals(vo.getFrom())) {
platPath = host + "/resources/images/invite/tianmao.png";
} else if ("jd".equals(vo.getFrom())) {
platPath = host + "/resources/images/invite/jd.png";
} else if ("pdd".equals(vo.getFrom())) {
platPath = host + "/resources/images/invite/pdd.png";
}
BufferedImage platImage = ImageIO.read(new URL(platPath));
// 透明底
BufferedImage bg = new BufferedImage(750, 1250, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bg.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, 750, 1250);// 填充整个屏幕
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 消除画图锯齿
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // 消除文字锯齿
// 写入商品图
Image src = Toolkit.getDefaultToolkit().getImage(new URL(vo.getPicUrl()));
BufferedImage goodsImg = BufferedImageBuilder.toBufferedImage(src);
if (pngString.equals("png")) { // png单独处理
String strImg = ZshopConstants.UploadFilesConstants.STATICFILESTEMPPATH + File.separator + imageName;
goodsImg = ImageIO.read(new File(strImg));
}
int y = 10;
Rectangle rectangle = new Rectangle(10, y, 730, 730);
g.drawImage(goodsImg.getScaledInstance(rectangle.width, rectangle.height, Image.SCALE_SMOOTH), rectangle.x, rectangle.y, null);
y+=735;
// 优惠券背景
String couponPath = host + "/resources/images/invite/goodsDetailCoupon.png";
BufferedImage couponImg = ImageIO.read(new URL(couponPath));
g.drawImage(couponImg.getScaledInstance(730, 158, Image.SCALE_SMOOTH), 10, y+10, 730, 158, null);
y+=75;
Double price = -1.0;
Double rebatePrice = 0.0;
try{
rebatePrice = (Double.valueOf(vo.getCommission()) * 0.33);
price = Double.valueOf(vo.getPrice()) - rebatePrice;
price = new BigDecimal(price).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
rebatePrice = new BigDecimal(rebatePrice).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}catch(Exception e){
logger.error("计算到手价失败", e);
}
String couponPrice = vo.getCouponPrice() == null ? "0" : vo.getCouponPrice();
String info =vo.getHasCoupon() ? "领" + couponPrice + "元券,下单返"+rebatePrice.toString() + "元" : "下单返"+rebatePrice.toString() + "元";
g.setFont(new Font("Microsoft YaHei", Font.BOLD, 30));
g.setColor(new Color(255, 255, 255));
g.drawString(info , vo.getHasCoupon() ? 40 : 110, y+15);
g.setFont(new Font("Microsoft YaHei", Font.PLAIN, 28));
g.setColor(new Color(255, 255, 255));
g.drawString("超值好物,买就返现" , 90, y+65);
String btnTitle ="长按获取";
g.setColor(new Color(255, 255, 255));
g.setFont(new Font("Microsoft YaHei", Font.PLAIN, 32));
g.drawString(btnTitle, 530, y+25);
y+=88;
// 写入二维码
String prefix = "jd".equals(vo.getFrom()) ? ZshopConstants.JD_PREFIX : "pdd".equals(vo.getFrom()) ? ZshopConstants.PDD_PREFIX : ZshopConstants.TAOBAO_PREFIX;
BufferedImage qrcode = QrcodeGenerator.encode( ZshopConstants.MOBILE_HOST + "/#/detail/" + prefix + vo.getGoodsId(), 230, 230);
g.drawImage(qrcode, 510, y+5, 230, 230, null);
g.setFont(new Font("Microsoft YaHei", Font.PLAIN, 22));
g.setColor(Color.gray);
g.drawString("长按识别二维码" , 550, y+240);
y+=25;
// 写入平台图片
g.drawImage(platImage.getScaledInstance(52, 28, Image.SCALE_SMOOTH), 15, y+7, 52, 28, null);
y+=30;
// 写入商品名
g.setFont(new Font("Microsoft YaHei", Font.PLAIN, 32));
g.setColor(Color.BLACK);
String title = vo.getTitle();
char[] sc = title.toCharArray();
int titleOffsetY = 0;
String titleSub = "";
int row = 0;
int j = 0;
for (int i = 0; i < title.length(); i++) {
titleSub = title.substring(j, i);
int titleSubW = g.getFontMetrics().charsWidth(sc, j, i-j);
int len = row >0 ? 450 : 400;
if(titleSubW > len){
j = i;
int ox = row > 0 ? 15 : 75;
row++;
if( row == 3 && j < title.length()-1 ){
titleSub = titleSub.substring(0, titleSub.length()-1) + "...";
}
g.drawString(titleSub, ox, y + titleOffsetY );
if(row == 3){
break;
}else{
titleOffsetY += 40;
}
}
}
if(row == 0){
titleSub = title.substring(j, title.length());
g.drawString(titleSub, 75, y + titleOffsetY );
}else if(row == 1 || row == 2){
titleSub = title.substring(j, title.length());
g.drawString(titleSub, 10, y + titleOffsetY );
}
y =y + titleOffsetY + 40;
g.setFont(new Font("Microsoft YaHei", Font.PLAIN, 25));
g.setColor(Color.gray);
g.drawString("商品价格以实际价格为准", 15, y);
y+=30;
// 到手价背景
g.drawRect(15, y +10,90,40);
g.setColor(new Color(255, 0, 61));
g.fillRect(15, y +10,90,40);
// 到手价
g.setFont(new Font("Microsoft YaHei", Font.BOLD, 25));
g.setColor(Color.white);
g.drawString("到手价" , 23, y+38);
// 到手价人民币图标
g.setFont(new Font("Microsoft YaHei", Font.PLAIN, 36));
g.setColor(Color.red);
g.drawString("¥" , 125, y+45);
// 到手价金额
g.setFont(new Font("Microsoft YaHei", Font.BOLD, 60));
g.setColor(Color.red);
g.drawString(price <0? vo.getPrice() : price.toString() , 145, y+45);
// 原价
g.setFont(new Font("Microsoft YaHei", Font.PLAIN, 30));
g.setColor(new Color(89, 89, 89));
g.drawString("原价: " , 350, y+45);
// 原价金额
g.setFont(new Font("Microsoft YaHei", Font.PLAIN, 30));
g.setColor(new Color(89, 89, 89));
g.drawString("¥" + vo.getOrgPrice() , 415, y+45);
y+=110;
g.setFont(new Font("Microsoft YaHei", Font.PLAIN, 36));
g.setColor(Color.gray);
g.drawString("花得值", 110, y);
g.setFont(new Font("Microsoft YaHei", Font.PLAIN, 25));
g.setColor(Color.gray);
g.drawString("—— 淘宝、天猫、京东...优惠专享APP", 225, y-5);
y+=10;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 消除画图锯齿
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // 消除文字锯齿
g.dispose();
ImageIO.write(bg, "jpg", new File(filePath));
} catch (IOException e) {
logger.error("生成优惠券海报失败", e);
}
}
注:其中GoodsInfoVO 为商品对象,其中生成二维码的方法可参考另一篇博客 ,地址:https://blog.csdn.net/weixin_38340467/article/details/84784107
核心代码写完之后只要调用一下就好了
@Override
public String createCouponPoster(String host, Long userId, GoodsInfoVO vo) throws Exception{
String fileUrl = (String)redisUtils.get("couponPoster:u"+userId+"_g"+vo.getGoodsId());
if(StringUtil.isBlank(fileUrl)){
//生成图片
String imagePath = ZshopConstants.UploadFilesConstants.STATICFILESTEMPPATH + System.currentTimeMillis()+".jpg";
File file = new File(imagePath);
drawCouponPosterImage(host, userId, vo, imagePath);
//上传至阿里云
JSONObject res = ossService.sendFile(imagePath);
if(res.getIntValue(SysCommand.CODE) == 1){
fileUrl = res.getString("file")+"?x-oss-process=style/w750";
redisUtils.set("couponPoster:u"+userId+"_g"+vo.getGoodsId(), fileUrl, 0);
}
//删除服务器端文件
file.delete();
}
return fileUrl;
}