如何在Java中自动裁剪图像白色边框? [英] How to auto crop an image white border in Java?

查看:872
本文介绍了如何在Java中自动裁剪图像白色边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java中从图像中自动裁剪白色边框的最简单方法是什么?在此先感谢...

What's the easiest way to auto crop the white border out of an image in java? Thanks in advance...

推荐答案

如果您希望白色部分不可见,最好的方法是使用图像滤镜并制作白色像素透明,它是在这里讨论由@PhiLho提供一些好的样本,如果你想调整你的图像大小为
所以它的边框不会有白色,你可以用四个简单的循环来做,
这个小我为你写的方法可以解决这个问题,请注意它只是裁剪图像的上半部分,你可以编写剩下的部分,

If you want the white parts to be invisible, best way is to use image filters and make white pixels transparent, it is discussed here by @PhiLho with some good samples, if you want to resize your image so it's borders won't have white colors, you can do it with four simple loops, this little method that I've write for you does the trick, note that it just crop upper part of image, you can write the rest,

    private Image getCroppedImage(String address) throws IOException{
    BufferedImage source = ImageIO.read(new File(address)) ;

    boolean flag = false ;
    int upperBorder = -1 ; 
    do{
        upperBorder ++ ;
        for (int c1 =0 ; c1 < source.getWidth() ; c1++){
            if(source.getRGB(c1, upperBorder) != Color.white.getRGB() ){
                flag = true;
                break ;
            }
        }

        if (upperBorder >= source.getHeight())
            flag = true ;
    }while(!flag) ;

    BufferedImage destination = new BufferedImage(source.getWidth(), source.getHeight() - upperBorder, BufferedImage.TYPE_INT_ARGB) ;
    destination.getGraphics().drawImage(source, 0, upperBorder*-1, null) ;

    return destination ;
}

这篇关于如何在Java中自动裁剪图像白色边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆