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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值