在做自动化测试的时候,有时候需要对屏幕进行截图,还需要图片进行合并,合并的代码如下:
package com.jingshou.image;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageTest {
public static String path = "pic\\";
public static void main(String[] args) throws IOException {
File file1 = new File(path, "1.jpg");
File file2 = new File(path, "2.jpg");
mergeImage(file1, file2);
}
public static void mergeImage(File file1, File file2) throws IOException {
BufferedImage image1 = ImageIO.read(file1);
BufferedImage image2 = ImageIO.read(file2);
BufferedImage combined = new BufferedImage(image1.getWidth() * 2, image1.getHeight(), BufferedImage.TYPE_INT_RGB);
// paint both images, preserving the alpha channels
Graphics g = combined.getGraphics();
g.drawImage(image1, 0, 0, null);
g.drawImage(image2, image1.getWidth(), 0, null);
// Save as new image
ImageIO.write(combined, "JPG", new File(path, "3.jpg"));
}
}
不管是不是尺寸相同,都可以合并,只是尺寸相同的图片合并出来效果好一点
本文出自"lijingshou"博客,转载请务必保留此出处http://lijingshou.iteye.com/blog/2019323