java后端通过BufferedImage绘制海报图片,上传到OSS返回图片路径
1.成图如下,楼主随意调试了下写了个模板,废话不多说直接贴代码
2.service层
// An highlighted block
*/
@Service
public class PosterServiceImpl implements PosterService {
//定义一个图片缓存类
private BufferedImage image;
private int imageWidth = 300; //图片的宽度
private int imageHeight = 500; //图片的高度
/*oss的配置项,桶名、路径等*/
private String endpoint= OSSProperties.ALIYUN_ENDPOINT;
private String accessKeyId=OSSProperties.ALIYUN_ACCESS_ID;
private String accessKeySecret=OSSProperties.ALIYUN_ACCESS_KEY;
private String bucketName=OSSProperties.ALIYUN_BUCKET;
/**
*
* @param productname 产品名称
* @param originalPrice 原价
* @param price 现价
* @param urlone 图片路径
* @param twourl 图片路径
* @param threeurl 图片路径
* @return
* @throws IOException
*/
public String graphicsGeneration(String productname, String originalPrice, String price,String urlone,String twourl,String threeurl) throws IOException {
int H_title = 30; //头部高度
int H_image =50;
int H_image_Text =50;
int H_mainPic = 230; //轮播广告高度
int tit_image_text=(H_title+H_image+H_image_Text);
int shops_2_top = H_title+H_image+H_image_Text+H_mainPic+20;
//实例化缓存类
image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
//设置图片的背景色
Graphics2D main = image.createGraphics();
main.setColor(Color.white);
main.fillRect(0, 0, imageWidth, imageHeight);
//***********************页面头部
Graphics title = image.createGraphics();
//设置区域颜色
title.setColor(new Color(255, 255, 255));
//填充区域并确定区域大小位置
title.fillRect(0, 0, imageWidth, H_title+20);
//设置字体颜色,先设置颜色,再填充内容
title.setColor(Color.black);
//设置字体
Font titleFont = new Font("黑体", Font.BOLD, 20);
title.setFont(titleFont);
title.drawString("驿货·精选