Java多张图片合成一张(横向或竖向)

代码说话

package com.example.demo.image;

import sun.misc.BASE64Decoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.List;

public class PicImageTest {


    public static void main(String[] args) throws Exception{
        List<File> files = new ArrayList<>();
        File file1 = new File("D:/1.jpg");
        File file2 = new File("D:/11.jpg");
        files.add(file1);
        files.add(file2);
        String path = "D:/222.jpg";
        mergeHorizontal(files, path);
    }
    //竖向合成
    public static void mergeVertical(List<File> files, String path){
        try {
            Integer allWidth = 0;	//计算画布总宽
            Integer allHeight = 0;	//计算画布总高
            List<BufferedImage> imgs = new ArrayList<>();
            for(int i=0; i<files.size(); i++){
                imgs.add(ImageIO.read(files.get(i)));
                //因为是竖向合成,拿图片里最大的一个宽度就行
                allWidth = Math.max(allWidth,imgs.get(i).getWidth());
                allHeight += imgs.get(i).getHeight();
            }
            BufferedImage combined = new BufferedImage(allWidth, allHeight,BufferedImage.TYPE_INT_RGB);
            Graphics g = combined.getGraphics();
            //设置画布背景颜色 ,默认黑色
            g.setColor(Color.white);
            g.fillRect(0, 0, allWidth, allHeight);
            Integer height = 0;
            for(int i=0; i< imgs.size(); i++){
                g.drawImage(imgs.get(i), 0, height,null);
                //+10为了设置上下两个图片间距
                height +=  imgs.get(i).getHeight()+10;
            }
            ImageIO.write(combined, "jpg", new File(path));
            System.out.println("===合成成功====");
        } catch (Exception e) {
            System.out.println("===合成失败====");
            e.printStackTrace();
        }
    }

    //横向合成
    public static void mergeHorizontal(List<File> files, String path){
        try {
            Integer allWidth = 0;
            Integer allHeight = 0;
            List<BufferedImage> imgs = new ArrayList<>();
            for(int i=0; i<files.size(); i++){
                imgs.add(ImageIO.read(files.get(i)));
                allHeight = Math.max(allHeight,imgs.get(i).getHeight());
                allWidth += imgs.get(i).getWidth();
            }
            BufferedImage combined = new BufferedImage(allWidth, allHeight, BufferedImage.TYPE_INT_RGB);
            Graphics g = combined.getGraphics();
            g.setColor(Color.white);
            g.fillRect(0, 0, allWidth, allHeight);
            Integer width = 0;
            for(int i=0; i< imgs.size(); i++){
                g.drawImage(imgs.get(i), width, 0, null);
                //设置横向间距
                width +=  imgs.get(i).getWidth()+10;
            }
            ImageIO.write(combined, "jpg", new File(path));
            System.out.println("===合成成功====");
        } catch (Exception e) {
            System.out.println("===合成失败====");
            e.printStackTrace();
        }
    }
	//字符串类型图片转换
    public static BufferedImage base64StringToImg(String base64String) {
        try {
          byte[] bytes = Base64.getDecoder().decode(data);
         ByteArrayInputStream in = new ByteArrayInputStream(bytes);
         return ImageIO.read(in);
        } catch (final IOException ioe) {
            throw new UncheckedIOException(ioe);
        }
    }

}

可以使用Java的ImageIO和BufferedImage类实现多张图片的拼接。下面是一个示例代码,将多张图片纵向拼接为一张图片: ```java import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class ImageConcatenation { public static void main(String[] args) throws IOException { // 设置图片路径 String[] imagePaths = new String[]{"image1.jpg", "image2.jpg", "image3.jpg"}; // 读取第一张图片 BufferedImage resultImage = ImageIO.read(new File(imagePaths[0])); // 获取图片宽度和高度 int width = resultImage.getWidth(); int height = resultImage.getHeight(); // 循环读取剩余的图片,并拼接到结果图片中 for (int i = 1; i < imagePaths.length; i++) { BufferedImage nextImage = ImageIO.read(new File(imagePaths[i])); height += nextImage.getHeight(); // 创建新的空白图片 BufferedImage combined = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 将原始图片拷贝到新的图片中 combined.getGraphics().drawImage(resultImage, 0, 0, null); // 将下一张图片拷贝到新的图片中 combined.getGraphics().drawImage(nextImage, 0, resultImage.getHeight(), null); // 更新结果图片 resultImage = combined; } // 保存拼接后的图片 ImageIO.write(resultImage, "jpg", new File("result.jpg")); } } ``` 如果要将多张图片横向拼接,只需要将循环中的高度改为宽度,将resultImage和nextImage的高度改为宽度,以及将drawImage方法的参数修改为(resultImage.getWidth(), 0)和(nextImage.getWidth(), 0)即可。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值