【Java】两张图片垂直、水平拼接实现方式,附代码


一、图片水平拼接

代码

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));
    }

}

实现结果

拼接前:
在这里插入图片描述
拼接后:
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值