java实现图片合并

 

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class ImageUtil {
    public static boolean merge(String[] imgs, String type, String mergePic) {
        int dstHeight = 0;
        int dstWidth = 0;
        // 获取需要拼接的图片长度
        int len = imgs.length;
        // 判断长度是否大于0
        if (len < 1) {
            return false;
        }
        File[] file = new File[len];
        BufferedImage[] images = new BufferedImage[len];
        int[][] ImageArrays = new int[len][];
        for (int i = 0; i < len; i++) {
            try {
                file[i] = new File(imgs[i]);
                images[i] = ImageIO.read(file[i]);
            } 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 = dstWidth > width ? 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;
    }

    
    // 横向处理图片
    public static void xPic() {
        try {
            /* 1 读取第一张图片 */
            File fileOne = new File("D:\\log\\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("D:\\log\\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:\\log\\x-out.jpg");
            // 写图片
            ImageIO.write(imageResult, "jpg", outFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 纵向处理图片
    public static void yPic() {
        try {
            /* 1 读取第一张图片 */
            File fileOne = new File("D:\\log\\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("D:\\log\\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, 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:\\log\\y-out.jpg");
            // 写图片
            ImageIO.write(imageResult, "jpg", outFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    
    public static void main(String[] args) throws Exception {
        String saveFile = "D:\\log\\ok.png";
        String images[] = {"D:\\log\\1.png", "D:\\log\\2.png", "D:\\log\\2.png", "D:\\log\\2.png"};
        ImageUtil.merge(images, "png", saveFile);
       
        //横向合并
        xPic();
        //纵向合并
        yPic();
        
    }
}

 

posted @ 2022-12-11 12:46  这个名字想了很久~  阅读(522)  评论(0编辑  收藏  举报