一、图片水平拼接
代码
package dandanpili.image;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ImageConnect {
public static void main(String[] args) {
String leftImagePath = "D:\\xxx\\xxx\\xx\\src\\main\\resources\\image1.jpg";
String rightImagePath = "D:\\xxx\\xxx\\xx\\src\\main\\resources\\image1.jpg";
String targetImagePath = "D:\\xxx\\xxx\\xx\\src\\main\\resources\\image-conn1.jpg";
try {
connectImageWidthHorizontal(leftImagePath, rightImagePath, targetImagePath);
} catch(Exception e) {
System.out.println("图片拼接失败!");
e.printStackTrace();
}
}
private static void connectImageWidthHorizontal(String leftImagePath, String rightImagePath, String targetImagePath) throws IOException {
BufferedImage leftBufImage = ImageIO.read(new File(leftImagePath));
BufferedImage rightBufImage = ImageIO.read(new File(rightImagePath));
int connImageWidth = leftBufImage.getWidth() + rightBufImage.getHeight();
int connImageHeight = Math.max(leftBufImage.getHeight(), rightBufImage.getHeight());
BufferedImage connImage = new BufferedImage(connImageWidth, connImageHeight, BufferedImage.TYPE_INT_RGB);
Graphics connGraphics = connImage.getGraphics();
//第一张图左上角坐标为(0, 0)
connGraphics.drawImage(leftBufImage, 0, 0, null);
//第二张图左上角坐标为(leftBufImage.getWidth(), 0),画在第一张图的右边
connGraphics.drawImage(rightBufImage, leftBufImage.getWidth(), 0, null);
String targetFileName = targetImagePath.split("\\.")[1];
ImageIO.write(connImage, targetFileName, new File(targetImagePath));
}
}
实现结果
拼接前:
拼接后:
二、图片垂直拼接
代码
package dandanpili.image;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ImageConnect {
public static void main(String[] args) {
String topImagePath= "D:\\xxx\\xxx\\xx\\src\\main\\resources\\image1.jpg";
String bottomImagePath= "D:\\xxx\\xxx\\xx\\src\\main\\resources\\image1.jpg";
String targetImagePath = "D:\\xxx\\xxx\\xx\\src\\main\\resources\\image-conn1.jpg";
try {
connectImageWidthVertical(topImagePath, bottomImagePath, targetImagePath);
} catch(Exception e) {
System.out.println("图片拼接失败!");
e.printStackTrace();
}
}
private static void connectImageWidthVertical(String topImagePath, String bottomImagePath, String targetImagePath) throws IOException {
BufferedImage topBufImage = ImageIO.read(new File(topImagePath));
BufferedImage bottomBufImage = ImageIO.read(new File(bottomImagePath));
int connImageWidth = Math.max(topBufImage.getWidth(), bottomBufImage.getHeight());
int connImageHeight = topBufImage.getHeight() + bottomBufImage.getHeight();
BufferedImage connImage = new BufferedImage(connImageWidth, connImageHeight, BufferedImage.TYPE_INT_RGB);
Graphics connGraphics = connImage.getGraphics();
//第一张图左上角坐标为(0, 0)
connGraphics.drawImage(topBufImage, 0, 0, null);
//第二张图左上角坐标为(0, topBufImage.getHeight()),画在第一张图的下边
connGraphics.drawImage(bottomBufImage, 0, topBufImage.getHeight(), null);
String targetFileName = targetImagePath.split("\\.")[1];
ImageIO.write(connImage, targetFileName, new File(targetImagePath));
}
}
实现结果
拼接前:
拼接后: