关于JAVA 两张图片PNG 格式合并 重叠居中的方法。

终于搞定了,直接贴上代码:
/**
* 待合并的两张图必须满足这样的前提,如果水平方向合并,则高度必须相等;如果是垂直方向合并,宽度必须相等。
* mergeImage方法不做判断,自己判断。
*
* @param img1
* 待合并的第一张图
* @param img2
* 带合并的第二张图
* @return 返回合并后的BufferedImage对象
* @throws IOException
*/
private static BufferedImage mergeImage(BufferedImage img1, BufferedImage img2)
throws IOException {
int w1 = img1.getWidth();
int h1 = img1.getHeight();
int w2 = img2.getWidth();
int h2 = img2.getHeight();
// 从图片中读取RGB
int[] ImageArrayOne = new int[w1 * h1];
ImageArrayOne = img1.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
int[] ImageArrayTwo = new int[w2 * h2];
ImageArrayTwo = img2.getRGB(0, 0, w2, h2, ImageArrayTwo, 0, w2);
// 生成新图片
BufferedImage DestImage = null;
DestImage = new BufferedImage(w1,h1,BufferedImage.TYPE_INT_RGB);

DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
// 加载水印图片文件
Graphics2D resizedG = DestImage.createGraphics();
resizedG.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,(float) 1));
resizedG.drawImage(img2, w1 / 2 - w2 / 2, h1 / 2 - h2 / 2, null);
resizedG.dispose();



// 画图

return DestImage;
}[code="java"][code="java"]
[/code][/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值