java图片合成
2021-03-17 18:26
标签:code image getc output vat tput font 微软雅黑 try /** // ewmurl =compoundPath; } java图片合成 标签:code image getc output vat tput font 微软雅黑 try 原文地址:https://blog.51cto.com/tianjian/2549757
//获取底图
String path="/template/background.png";
InputStream inputStream = this.getClass().getResourceAsStream(path);
QrConfig config = new QrConfig(118, 118);
// 设置边距,既二维码和背景之间的边距
// config.setMargin(1);
// 设置前景色,既二维码颜色(黑色)
config.setForeColor(Color.BLACK.getRGB());
// 设置背景色(白色)
config.setBackColor(Color.WHITE.getRGB());
// 生成二维码到文件,也可以到流
BufferedImage bi= QrCodeUtil.generate(content, config);
String base64=CompoundImage(inputStream,bi);
return "data:image/png;base64,"+base64;
}
*/
private String CompoundImage(InputStream inputStream ,BufferedImage erweima ) {
String ewmurl="";
try {
//1、获取主图片
BufferedImage big = ImageIO.read(inputStream);
//2、拿到二维码
int width = big.getWidth();
int height = big.getHeight();
Image image = big.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage bufferedImage2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
//3、开始绘图
Graphics2D g = bufferedImage2.createGraphics();
g.drawImage(image, 0, 0, width,height,null);
g.drawImage(erweima, 66, 128,118,118,null);
Font font = new Font("微软雅黑", Font.BOLD, 38);
g.setFont(font);
g.setPaint(Color.BLACK);
//4、设置位置
// int wordWidth = ImageUtil.getWordWidth(font, text);
// int i = width / 2;
// int i1 = (i - wordWidth) / 2;
// int numWidth=i+i1;
// g.drawString(text, numWidth-10, 310+28);
g.dispose();
// File file= new File(ewmurl);
// ImageIO.write(bufferedImage2, "jpg", file);
// System.out.println("图片生成完毕"+ewmurl); ByteArrayOutputStream stream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage2, "png", stream);
ewmurl = Base64.encode(stream.toByteArray());
} catch (Exception e) {
e.printStackTrace();
}
return ewmurl;
上一篇:数组的定义和访问