代码如下
/**
*
* @param gb 背景图 例子 :new File(backPicPath)、new ClassPathResource("img/xxx.jpg").getInputStream()
* @param cg 子图片(二维码) 例子 :new File(backPicPath)、new ClassPathResource("img/xxx.jpg").getInputStream()
* @param filePath 输出最终图片的位置 如 D:\jar\111.png
* @param cgx 加是向右,减是向左
* @param cgy 加是向下,减是向上
* @param cgw 合成的子图片大小 如:二维码大小 宽度
* @param cgh 合成的子图片大小 如:二维码大小 高度
*/
public static final void compositePicture(InputStream gb,InputStream cg, String filePath,Integer cgx,Integer cgy,
Integer cgw,Integer cgh) {
try (InputStream gbStream = gb;
InputStream cgStream = cg;
FileOutputStream fos = new FileOutputStream(filePath)) {
BufferedImage big = ImageIO.read(gbStream);
BufferedImage small = ImageIO.read(cgStream);
Graphics2D g = big.createGraphics();
g.drawImage(small, cgx, cgy, cgw, cgh, null);
g.dispose();
ImageIO.write(big, "png", fos);
} catch (IOException e) {
}
} 简单调用
public static void main(String[] args) throws IOException {
compositePicture(
new ClassPathResource("img/微信图片_20230629140957.jpg").getInputStream(),
new ClassPathResource("img/微信图片_20230629140957.jpg").getInputStream(),
"D:\\jar\\111.png",
270,270,150,150);
} 合成结果
本文作者为DBC,转载请注明。
