上下拼接图片,且宽度保持一致
package com.szpnr.mulitplanlggp.lggpsnjh.util;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
public class TestUtil {
public static boolean merge(File[] file, String type, String mergePic) {
int maxWidth = 0;
int dstHeight = 0;
int dstWidth = 0;
// 获取需要拼接的图片长度
int len = file.length;
// 判断长度是否大于0
if (len < 1) {
return false;
}
BufferedImage[] images = new BufferedImage[len];
int[][] ImageArrays = new int[len][];
for (int i = 0; i < len; i++) {
try {
BufferedImage image = ImageIO.read(file[i]);
//比较大小,找出最宽的长度
maxWidth = Math.max(maxWidth, image.getWidth());
//转换出同样宽的图片,放入需要拼接的数组里面
BufferedImage newImage = resizeImage(image,maxWidth,image.getHeight());
images[i] = newImage;
} catch (Exception e) {
e.printStackTrace();
return false;
}
int width = images[i].getWidth();
int height = images[i].getHeight();
// 从图片中读取RGB 像素
ImageArrays[i] = new int[width * height];
ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width);
// 计算合并的宽度和高度
dstWidth = Math.max(dstWidth, width);
dstHeight += height;
}
// 合成图片像素
System.out.println("宽度:" + dstWidth);
System.out.println("高度:" + dstHeight);
if (dstHeight < 1) {
System.out.println("dstHeight < 1");
return false;
}
// 生成新图片
try {
BufferedImage imageNew = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_RGB);
int width_i = 0;
int height_i = 0;
for (int i = 0; i < images.length; i++) {
int width = images[i].getWidth();
int height = images[i].getHeight();
imageNew.setRGB(0, height_i, width, height, ImageArrays[i], 0, width);
height_i += height;
}
File outFile = new File(mergePic);
// 写图片,输出到硬盘
ImageIO.write(imageNew, type, outFile);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 通过BufferedImage图片流调整图片大小
*/
public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws IOException {
Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_AREA_AVERAGING);
BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
outputImage.getGraphics().drawImage(resultingImage, 0, 0, null);
return outputImage;
}
/**
* BufferedImage图片流转byte[]数组
*/
public static byte[] imageToBytes(BufferedImage bImage) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(bImage, "jpg", out);
} catch (IOException e) {
e.printStackTrace();
}
return out.toByteArray();
}
/**
* byte[]数组转BufferedImage图片流
*/
private static BufferedImage bytesToBufferedImage(byte[] ImageByte) {
ByteArrayInputStream in = new ByteArrayInputStream(ImageByte);
BufferedImage image = null;
try {
image = ImageIO.read(in);
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
public static void main(String[] args) throws Exception {
String saveFile = "D:\\upload\\dghy_lggp\\3.png";
File images[] = {new File("D:\\upload\\dghy_lggp\\1.png"),new File("D:\\upload\\dghy_lggp\\2.png")};
merge(images, "png", saveFile);
}
}