JAVA后端生成类似于分享海报图片的功能的实现

 

首先确定一下我们需要实现的效果图

需要实现的案例图
案例图

核心代码如下:

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.createG
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值