什么是最简单的方法来自动从Java中的图像裁剪白色边框?在此先感谢...如何在Java中自动裁剪图像白色边框?
9
A
回答
5
如果你想白色部分是无形的,最好的办法是用图像过滤器,使白色像素透明的,它是通过discussed here与@PhiLho一些很好的示例, 如果要调整你的图片所以它的边框不会有白色,你可以用四个简单的循环来做, 这个我为你写的小方法就是诀窍,注意它只是裁剪图片的上半部分,你可以写休息,
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 ;
}
23
这里的裁剪所有4个方面的方式,利用颜色从最左上角的像素为基准,并允许颜色变化的耐受性,从而在IM的噪音年龄不会使作物无用
public BufferedImage getCroppedImage(BufferedImage source, double tolerance) {
// Get our top-left pixel color as our "baseline" for cropping
int baseColor = source.getRGB(0, 0);
int width = source.getWidth();
int height = source.getHeight();
int topY = Integer.MAX_VALUE, topX = Integer.MAX_VALUE;
int bottomY = -1, bottomX = -1;
for(int y=0; y<height; y++) {
for(int x=0; x<width; x++) {
if (colorWithinTolerance(baseColor, source.getRGB(x, y), tolerance)) {
if (x < topX) topX = x;
if (y < topY) topY = y;
if (x > bottomX) bottomX = x;
if (y > bottomY) bottomY = y;
}
}
}
BufferedImage destination = new BufferedImage((bottomX-topX+1),
(bottomY-topY+1), BufferedImage.TYPE_INT_ARGB);
destination.getGraphics().drawImage(source, 0, 0,
destination.getWidth(), destination.getHeight(),
topX, topY, bottomX, bottomY, null);
return destination;
}
private boolean colorWithinTolerance(int a, int b, double tolerance) {
int aAlpha = (int)((a & 0xFF000000) >>> 24); // Alpha level
int aRed = (int)((a & 0x00FF0000) >>> 16); // Red level
int aGreen = (int)((a & 0x0000FF00) >>> 8); // Green level
int aBlue = (int)(a & 0x000000FF); // Blue level
int bAlpha = (int)((b & 0xFF000000) >>> 24); // Alpha level
int bRed = (int)((b & 0x00FF0000) >>> 16); // Red level
int bGreen = (int)((b & 0x0000FF00) >>> 8); // Green level
int bBlue = (int)(b & 0x000000FF); // Blue level
double distance = Math.sqrt((aAlpha-bAlpha)*(aAlpha-bAlpha) +
(aRed-bRed)*(aRed-bRed) +
(aGreen-bGreen)*(aGreen-bGreen) +
(aBlue-bBlue)*(aBlue-bBlue));
// 510.0 is the maximum distance between two colors
// (0,0,0,0 -> 255,255,255,255)
double percentAway = distance/510.0d;
return (percentAway > tolerance);
}
0
而这里只是一个例子
private static BufferedImage autoCrop(BufferedImage sourceImage) {
int left = 0;
int right = 0;
int top = 0;
int bottom = 0;
boolean firstFind = true;
for (int x = 0; x < sourceImage.getWidth(); x++) {
for (int y = 0; y < sourceImage.getWidth(); y++) {
// pixel is not empty
if (sourceImage.getRGB(x, y) != 0) {
// we walk from left to right, thus x can be applied as left on first finding
if (firstFind) {
left = x;
}
// update right on each finding, because x can grow only
right = x;
// on first find apply y as top
if (firstFind) {
top = y;
} else {
// on each further find apply y to top only if a lower has been found
top = Math.min(top, y);
}
// on first find apply y as bottom
if (bottom == 0) {
bottom = y;
} else {
// on each further find apply y to bottom only if a higher has been found
bottom = Math.max(bottom, y);
}
firstFind = false;
}
}
}
return sourceImage.getSubimage(left, top, right - left, bottom - top);
}
相关问题
- 1. 自动裁剪图像边界
- 2. 如何裁剪图像,使用imagemagick图像中的颜色边框
- 3. 在Java中裁剪图像
- 4. 在Java中裁剪图像
- 5. 如何在Java中裁剪图像?
- 6. 自动裁剪图像
- 7. 自动图像裁剪
- 8. c#自动裁剪图像
- 9. 如何自动裁剪图像?
- 10. Matlab-改进图像裁剪黑色边框中的代码
- 11. 如何为滑动的圆形裁剪图像制作边框
- 12. 如何使用python在白色背景上裁剪图像?
- 13. 使用边框的裁剪图像
- 14. 使用PIL裁剪图像的边框
- 15. 图像在wordpress中自动裁剪
- 16. 在C#中裁剪图像的空白#
- 17. 修剪UIImage白色边框
- 18. java中的图像裁剪
- 19. Ruby Rmagick:裁剪/“修剪”图像的过多(白色)空间
- 20. WPF剪裁边框
- 21. 从图像中裁剪空白
- 22. 为什么在使用UIGraphicsContext裁剪图像时出现白色边缘?
- 23. 在图像上没有白色边框
- 24. 在屏幕边缘裁剪图像html
- 25. 如何基于图像中的边界框裁剪图像(以编程方式)?
- 26. Ghostscript:如何自动裁剪STDIN到“边界框”并写入PDF?
- 27. 使用jQuery自动裁剪图像空白空间
- 28. php imagemagick - 如何在没有裁剪的白色方块中间放置图像
- 29. 使用Java裁剪图像
- 30. 在Java中裁剪图像的类?
试试这个http://stackoverflow.com/questions/273530/java-image-library-to-deskew-和 - 作物图像 – blitzen