visper
2023-02-09 16:12:13 +08:00
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class MergeImages {
public static void mergeImages(String bottomImagePath, String topImagePath, String resultImagePath) {
try {
// 读取底部图片
BufferedImage bottomImage = ImageIO.read(new File(bottomImagePath));
// 读取顶部图片
BufferedImage topImage = ImageIO.read(new File(topImagePath));
// 创建结果图片
BufferedImage combined = new BufferedImage(bottomImage.getWidth(), bottomImage.getHeight(),
BufferedImage.TYPE_INT_ARGB);
// 创建画笔
Graphics2D g = combined.createGraphics();
// 绘制底部图片
g.drawImage(bottomImage, 0, 0, null);
// 设置图片的透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
// 绘制顶部图片
g.drawImage(topImage, 0, 0, null);
// 释放画笔资源
g.dispose();
// 保存结果图片
ImageIO.write(combined, "PNG", new File(resultImagePath));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
mergeImages("bottom.png", "top.png", "result.png");
}
}
程序员要失业了