在
java中自动裁剪白色边框的图片最简单的方法是什么?提前致谢…
解决方法
如果你想让白色的部分看不见,最好的方法是使用图像滤镜,使白色像素透明,这是由@PhiLho提供的一些很好的样品是
discussed here,
如果你想调整你的图像大小,它的边框不会有白色的颜色,你可以用四个简单的循环,
我为你写的这个小方法是诀窍,请注意,它只是裁剪图像的上半部分,你可以写其余的,
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,upperBorder*-1,null) ;
return destination ;
}
