当前位置:   article > 正文

java 图片 白边,如何在Java中自动裁剪图像白色边框?

java 图片外框裁剪

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

解决方案

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 ;

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/604356
推荐阅读
相关标签
  

闽ICP备14008679号