什麼是最簡單的方法來自動從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