Was ist der einfachste Weg, den weißen Rahmen aus einem Bild in Java automatisch zu beschneiden? Vielen Dank im Voraus ...Wie schneidet man einen Bildrand in Java automatisch ab?
Antwort
Wenn Sie die weißen Teile unsichtbar sein sollen, ist am bester Weg Bild-Filter zu verwenden und weißen Pixel transparent zu machen, es discussed here durch @PhiLho mit einigen guten Proben ist, , wenn Sie ändern mögen Ihr Bild, so dass es Grenzen wird keine weißen Farben haben, können Sie es mit vier einfachen Schleifen tun, diese kleine Methode, die ich für Sie schreiben tut den Trick, beachten Sie, dass es nur obere Teil des Bildes beschneiden, können Sie die schreiben Ruhe,
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 ;
}
Hier ist ein Weg, um alle vier Seiten zu beschneiden, als Basis der Farbe aus den sehr linken oberen Pixeln verwenden und so das Rauschen der im für eine Toleranz von Farbvariationen ermöglichen Alter wird nicht machen die Ernte nutzlos
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);
}
Perfekt! Vielen Dank! – mbelow
Was bedeutet diese Toleranz? –
Die Toleranz ermöglicht, dass Bilder ohne eine perfekt durchgehende Hintergrundfarbe abgeschnitten werden. Wenn Sie beispielsweise eine Zeichnung von einem Blatt Papier scannen, wird das Papier nicht als exaktes Weiß angezeigt, sondern besteht aus einer Reihe von Farben in der Nähe von Weiß. Wenn Sie versuchen, zuzuschneiden, indem Sie nur eine bestimmte Farbe von Weiß auswählen, wird wenig (wenn überhaupt) abgeschnitten. Wenn Sie die Farbe des Hintergrunds, der gerade beschnitten wird, etwas variieren können, kann er alle unnötigen umgebenden Hintergrund entfernen und Sie nur mit der Zeichnung belassen. – Todd
Und hier nur ein weiteres Beispiel
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. Wie schneidet man einen String mit str.format in Python ab?
- 2. Wie schneidet man $ filter in MongoDB ab?
- 3. H2 - Wie schneidet man alle Tabellen ab?
- 4. Wie man einen Std_logic_vector in VHDL "schneidet"?
- 5. Wie schneidet man eine Datei in C ab?
- 6. Wie schneidet man die Tabelle in Symfony 3 ab?
- 7. Wie schneidet man ein Bild mit einem gedrehten Rechteck ab?
- 8. Bildrand in QML
- 9. Canvas.clipPath (Pfad) schneidet nicht wie erwartet ab
- 10. dbfunctions.truncatetime schneidet keine Zeit ab
- 11. Wie schneidet man eine Zeichenfolge ab, um in einen Container zu passen?
- 12. Wie schneidet man einen Teil eines Textes in HTML5-Canvas ab?
- 13. Wie man 'Null' schneidet?
- 14. Wie schneidet einen String
- 15. Halbtransparenter Bildrand
- 16. Wie schneidet man zwei Polygone?
- 17. Wie schneidet man eine Datei in Java in die Zwischenablage?
- 18. Wie schneidet man einen NSString basierend auf der grafischen Breite ab?
- 19. Wie schneidet man den Hintergrund eines Elements ab, um nur einen Teil des Bildes anzuzeigen?
- 20. Eclipse CSV Download schneidet Daten ab
- 21. Wie schneidet man() alle Elemente in einer Liste <String> am besten ab?
- 22. SVG - Wie schneidet man ein Bild in einen Kreis?
- 23. Warum schneidet Android meinen ActionBar-Titel ab?
- 24. Warum schneidet Beautiful Soup diese Seite ab?
- 25. YouTube-API schneidet Videos ab [PHP]
- 26. WKHTMLTOPDF schneidet Text vom linken Rand ab
- 27. Wie schneidet man ein Bild in Android?
- 28. NavigationExperimental Header schneidet die AnimatedView-Szenen ab
- 29. Kröte schneidet/rundet große Oracle-Zahlen ab?
- 30. Wie schneidet man das UIImage?
Versuchen Sie, diese http://stackoverflow.com/questions/273530/java-image-library-to-deskew- und-crop-images – blitzen