java 图片裁剪与合并

前言

在使用阿里云人数检测时,为降低成本,我们需要将两个图片合并成一张图片,提交给阿里云图像识别,但我发现识别时由于一些感染因素,会有一定的错误率,所以就需要将图片进行裁剪后再拼接。

具体操作逻辑:

  1. 首先将图片裁剪
  2. 将裁剪后的图片合并(这里测试用的是原图+裁剪后的图)
  3. 后续业务操作

代码如下


    /**
     * 裁剪(多边形)
     *
     * @param inputFilePath 图片输入路径
     * @param outFilePath   图片输出路径
     * @param x             x轴坐标点数组
     * @param y             y轴坐标点数组
     * @param n             坐标点数量
     * @throws IOException
     */
    public static void cutPolygonImage(String inputFilePath, String outFilePath, int[] x, int[] y, int n) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(inputFilePath);
            fileOutputStream = new FileOutputStream(outFilePath);
            cutPolygonImage(fileInputStream, fileOutputStream, x, y, n);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            if(fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                }
            }
            if(fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                }
            }
        }
    }
    /**
     * 裁剪(多边形)
     *
     * @param inputFilePath 图片输入路径
     * @param outFilePath   图片输出路径
     * @param x             x轴坐标点数组
     * @param y             y轴坐标点数组
     * @param n             坐标点数量
     * @throws IOException
     */
    public static void cutPolygonImage(InputStream inputFilePath, OutputStream outFilePath, int[] x, int[] y, int n) {
        try {
            BufferedImage image = ImageIO.read(inputFilePath);
            GeneralPath clip = new GeneralPath(GeneralPath.WIND_EVEN_ODD, n);
            clip.moveTo(x[0], y[0]);
            for (int i = 1; i < x.length; i++) {
                clip.lineTo(x[i], y[i]);
            }
            clip.closePath();
            
            Rectangle bounds = clip.getBounds();
            BufferedImage img = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_BGR);
            Graphics2D g2d = img.createGraphics();
            
            g2d.translate(-bounds.getMinX(), -bounds.getMinY());
            g2d.setComposite(AlphaComposite.SrcOver);
            
            g2d.setClip(clip);
            g2d.drawImage(image, 0, 0, null);
            g2d.dispose();
            
            ImageIO.write(img, "jpg", outFilePath);
        } catch (Exception e) {
            log.error("图片错误",e);
        }
    }
    
    /**
     * 图片合并
     * @param image1 图片1
     * @param image2 图片2
     * @param outputStream 输出位置
     */
    public static void splicingImage(BufferedImage image1, BufferedImage image2,OutputStream outputStream) {
        int width = image1.getWidth() + image2.getWidth();
        int height = NumberUtil.max(image1.getHeight(), image2.getHeight());
        BufferedImage combinedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = combinedImage.createGraphics();
        
        g.drawImage(image1, 0, 0, null);
        g.drawImage(image2, image1.getWidth(), 0, null);
        try {
            ImageIO.write(combinedImage, "PNG", outputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        
        
    }
    
    public static void main(String[] args) {
        int[] x = { 1491, 1491, 143,143};
        int[] y = { 0, 1000, 1000,0};
        cutPolygonImage("D:\\logs\\wallhaven-rdmzg7.png", "D:\\logs\\wallhaven-rdmzg8.png", x, y, 4);
        try {
            BufferedImage image1 = ImageIO.read(new File("D:\\logs\\wallhaven-rdmzg7.png"));
            BufferedImage image2 = ImageIO.read(new File("D:\\logs\\wallhaven-rdmzg8.png"));
            splicingImage(image1, image2,new FileOutputStream("D:\\logs\\wallhaven-rdmzg9.png"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        
        
    }
  • 13
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ImageMagick 是一款强大的图像处理软件,通过它我们可以实现多张图片合并。在 Java 中,我们可以通过调用 ImageMagick 的命令行工具来实现图片合并。 首先,我们需要确保系统中已经安装了 ImageMagick。可以在终端中输入 `magick -version` 命令来检查是否已经安装成功。 接下来,我们可以使用 Java 的 `Runtime` 类来执行 ImageMagick 的命令行工具。首先,我们需要构建一个字符串数组,其中包含我们要执行的命令。例如,如果我们想要合并两张图片A.jpg和B.jpg,那么我们可以构建如下的命令数组: ```java String[] command = {"magick", "convert", "A.jpg", "B.jpg", "+append", "output.jpg"}; ``` 这个命令数组中,`magick` 是 ImageMagick 的命令行工具,`convert` 是 ImageMagick 的功能之一,`A.jpg` 和 `B.jpg` 是要合并的两张图片的文件名,`+append` 是合并图片的参数,`output.jpg` 是合并后的图片的输出文件名。 接下来,我们可以使用 `Runtime` 类的 `exec` 方法来执行命令: ```java Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec(command); ``` 执行完毕后,我们可以通过 `process.waitFor()` 方法等待命令执行完成。 最后,我们可以通过 `ImageIO` 类将合并后的图片输出到本地文件中: ```java BufferedImage mergedImage = ImageIO.read(new File("output.jpg")); ImageIO.write(mergedImage, "jpg", new File("merged.jpg")); ``` 以上就是使用 ImageMagick 在 Java合并图片的过程。需要注意的是,使用 ImageMagick 进行图片合并的同时也可以对图片进行其他处理,例如裁剪、缩放、旋转等。具体的命令和参数可以根据需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值