java 图片裁剪与合并

2023-12-15 10:59:41

前言

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

具体操作逻辑:

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

文章来源:https://blog.csdn.net/qq_37332702/article/details/135009247
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。