Java横向、纵向合并图片

进行图片对比时候想把两张有差异的图片放到一起,方便人工查看下,在网上搜了一些,有纵向合并的。

将纵向合并的自己调整了下,源码如下:

package test01;

import java.io.File;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class ImageUtil {
    public static void main(String[] args) {
        xPic();
    }

    // 横向处理图片
    public static void xPic() {
        try {
            /* 1 读取第一张图片 */
            File fileOne = new File("E:\1.png");
            BufferedImage imageFirst = ImageIO.read(fileOne);
            // 图片宽度
            int width = imageFirst.getWidth();
            // 图片高度
            int height = imageFirst.getHeight();
            // 从图片中读取RGB
            int[] imageArrayFirst = new int[width * height];
            imageArrayFirst = imageFirst.getRGB(0, 0, width, height,
                    imageArrayFirst, 0, width);

            /* 1 对第二张图片做相同的处理 */
            File fileTwo = new File("E:\2.png");
            BufferedImage imageSecond = ImageIO.read(fileTwo);
            int[] imageArraySecond = new int[width * height];
            imageArraySecond = imageSecond.getRGB(0, 0, width, height,
                    imageArraySecond, 0, width);

            // 生成新图片
            BufferedImage imageResult = new BufferedImage(width * 2, height,
                    BufferedImage.TYPE_INT_RGB);
            // 设置左半部分的RGB
            imageResult.setRGB(0, 0, width, height, imageArrayFirst, 0, width);
            // 设置右半部分的RGB
            imageResult.setRGB(width, 0, width, height, imageArraySecond, 0,
                    width);
            File outFile = new File("D:\out.jpg");
            // 写图片
            ImageIO.write(imageResult, "jpg", outFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 纵向处理图片
    public static void yPic() {
        try {
            /* 1 读取第一张图片 */
            File fileOne = new File("D:\1.GIF");
            BufferedImage imageFirst = ImageIO.read(fileOne);
            // 图片宽度
            int width = imageFirst.getWidth();
            // 图片高度
            int height = imageFirst.getHeight();
            // 从图片中读取RGB
            int[] imageArrayFirst = new int[width * height];
            imageArrayFirst = imageFirst.getRGB(0, 0, width, height,
                    imageArrayFirst, 0, width);
            /* 1 对第二张图片做相同的处理 */
            File fileTwo = new File("D:\1.GIF");
            BufferedImage imageSecond = ImageIO.read(fileTwo);
            int[] imageArraySecond = new int[width * height];
            imageArraySecond = imageSecond.getRGB(0, 0, width, height,
                    imageArraySecond, 0, width);
            // 生成新图片
            BufferedImage imageResult = new BufferedImage(width, height * 2,
                    BufferedImage.TYPE_INT_RGB);
            // 设置上半部分的RGB
            imageResult.setRGB(0, 0, width, height, imageArrayFirst, 0, width);
            // 设置下半部分的RGB
            imageResult.setRGB(0, height, width, height, imageArraySecond, 0,
                    width);
            File outFile = new File("D:\out.jpg");
            // 写图片
            ImageIO.write(imageResult, "jpg", outFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

直接运行即可。

本文转自:http://cxr1217.iteye.com/blog/1638681

原文地址:https://www.cnblogs.com/dreammyle/p/4450983.html