1.合成海报保存到本地
public static void drawImage(double x,double y,String backgroundUrl,String qrCodeUrl,String text,Integer color) throws IOException {
BufferedImage bgBufImage = ImageIO.read(new URL(backgroundUrl)); //获取图片
Graphics2D bgBufImageGraphics = bgBufImage.createGraphics(); //创建一个图形
BufferedImage qrCodeImage = ImageIO.read(new URL(qrCodeUrl)); //获取图片
Integer width = Integer.valueOf(new BigDecimal(x * bgBufImage.getWidth() / 100).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
Integer height = Integer.valueOf(new BigDecimal(y * bgBufImage.getHeight() / 100).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
bgBufImageGraphics.drawImage(qrCodeImage, width, height, qrCodeImage.getWidth(), qrCodeImage.getHeight(), null); //设置图片的位置
if (!StringUtils.isEmpty(text)){
bgBufImageGraphics.setColor(new Color(color));
bgBufImageGraphics.setFont(new Font("黑体",Font.PLAIN, 47));
FontMetrics fm = bgBufImageGraphics.getFontMetrics(new Font("黑体",Font.PLAIN, 47));
int textWidth = fm.stringWidth(text);
int qrCodeWidth = qrCodeImage.getWidth();
bgBufImageGraphics.drawString(text,width + ((qrCodeWidth - textWidth) / 2),(height - 50));
}
bgBufImageGraphics.dispose();
ImageIO.write(bgBufImage, "png", new File("D:\\file\\demo1.png"));
System.out.println("生成图片完成");
}
2.合成海报保存到oss服务器
public String compositePoster(double x,double y,String backgroundUrl,String qrCodeUrl,String text,Integer color) throws IOException {
BufferedImage bgBufImage = ImageIO.read(new URL(backgroundUrl)); //获取图片
Graphics2D bgBufImageGraphics = bgBufImage.createGraphics(); //创建一个图形
BufferedImage qrCodeImage = ImageIO.read(new URL(qrCodeUrl)); //获取图片
Integer width = Integer.valueOf(new BigDecimal(x * bgBufImage.getWidth() / 100).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
Integer height = Integer.valueOf(new BigDecimal(y * bgBufImage.getHeight() / 100).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
bgBufImageGraphics.drawImage(qrCodeImage, width, height, qrCodeImage.getWidth(), qrCodeImage.getHeight(), null); //设置图片的位置
if (!StringUtils.isEmpty(text)){
bgBufImageGraphics.setColor(new Color(color));
bgBufImageGraphics.setFont(new Font("黑体",Font.PLAIN, 47));
FontMetrics fm = bgBufImageGraphics.getFontMetrics(new Font("黑体",Font.PLAIN, 47));
int textWidth = fm.stringWidth(text);
int qrCodeWidth = qrCodeImage.getWidth();
bgBufImageGraphics.drawString(text,width + ((qrCodeWidth - textWidth) / 2),(height - 50));
}
bgBufImageGraphics.dispose();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imgOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(bgBufImage, "png", imgOut);
InputStream inSteam = new ByteArrayInputStream(bs.toByteArray());
String fileName = URLDecoder.decode(backgroundUrl.split("/")[backgroundUrl.split("/").length -1 ], "UTF-8");
String uuid = UUID.randomUUID().toString() + ".png";
ossUtils.uploadFiles(inSteam,uuid,fileName);
return ossUtils.getObjectUrl(uuid);
}
传入的参数
![](https://img-blog.csdnimg.cn/20200512163550780.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1lhbmdfSHVpX0xpYW5n,size_16,color_FFFFFF,t_70)
合成的效果
![](https://img-blog.csdnimg.cn/20200512163306401.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1lhbmdfSHVpX0xpYW5n,size_16,color_FFFFFF,t_70)