Jo der Titel sagt eigentlich alles. Das Problem hierbei ist eindeutig, das man nicht jeden Punkt eines Bild braucht. Man könnte es sich einfach machen und einfach die Ecke eines Bildes nehmen basierend auf der transparenten Farbe.
Code:
X X
X O
X = Transparenz, O = nicht Transparenz
Allerdings frage ich mich ob es nicht einen anderen Weg gibt... Also was meint ihr? Ich hab hier schon ein Methode, die allerdings nur mit der Alphatransparenz arbeitet, dazu kommt das es nur für Außenränder geht, nicht für innere Ränder:
Code (Java):
public static Polygon getPolygonFromImage( Image image) {
BufferedImage bi = new BufferedImage ( image.getWidth ( null ) ,
image.getHeight ( null ) , BufferedImage .TYPE_INT_ARGB ) ;
Graphics g = bi.createGraphics ( ) ;
g.drawImage ( image, 0, 0, null ) ;
ArrayList< Point> poi = new ArrayList< Point> ( ) ;
// nimm transparente punkte die neben nicht transparenten punkten sind
for ( int i = 0 ; i < bi.getHeight ( ) ; i++ ) {
for ( int j = 0 ; j < bi.getWidth ( ) ; j++ ) {
if ( bi.getRGB ( j, i) == 0) {
try {
if ( bi.getRGB ( j - 1, i) != 0
&& ! poi.contains ( new Point ( j, i) ) )
poi.add ( new Point ( j, i) ) ;
} catch ( ArrayIndexOutOfBoundsException e) { }
try {
if ( bi.getRGB ( j + 1, i) != 0
&& ! poi.contains ( new Point ( j, i) ) ) {
poi.add ( new Point ( j, i) ) ;
}
} catch ( ArrayIndexOutOfBoundsException e) { }
try {
if ( bi.getRGB ( j, i - 1) != 0
&& ! poi.contains ( new Point ( j, i) ) )
poi.add ( new Point ( j, i) ) ;
} catch ( ArrayIndexOutOfBoundsException e) { }
try {
if ( bi.getRGB ( j, i + 1) != 0
&& ! poi.contains ( new Point ( j, i) ) ) {
poi.add ( new Point ( j, i) ) ;
}
} catch ( ArrayIndexOutOfBoundsException e) { }
}
}
}
ArrayList< Point> tmp = new ArrayList< Point> ( ) ;
tmp.add ( poi.get ( 0) ) ;
poi.remove ( 0) ;
boolean found = false ;
// verbinde die punkte mit dem nächsten
while ( poi.size ( ) > 0) {
found = false ;
for ( int j = 0 ; j < poi.size ( ) && ! found; j++ ) {
// nachbar an der kante
if ( ( Math .abs ( tmp.get ( tmp.size ( ) - 1) .x - poi.get ( j) .x ) == 1 && tmp
.get ( tmp.size ( ) - 1) .y - poi.get ( j) .y == 0)
|| ( Math .abs ( tmp.get ( tmp.size ( ) - 1) .y - poi.get ( j) .y ) == 1 && tmp
.get ( tmp.size ( ) - 1) .x - poi.get ( j) .x == 0) ) {
tmp.add ( poi.get ( j) ) ;
poi.remove ( j) ;
found = true ;
}
}
for ( int j = 0 ; j < poi.size ( ) && ! found; j++ ) {
// nachbar an der ecke
if ( Math .abs ( tmp.get ( tmp.size ( ) - 1) .x - poi.get ( j) .x ) == 1
&& Math .abs ( tmp.get ( tmp.size ( ) - 1) .y - poi.get ( j) .y ) == 1) {
tmp.add ( poi.get ( j) ) ;
poi.remove ( j) ;
found = true ;
}
}
if ( ! found) {
// es sind noch punkte übrig aber wir sind wieder am anfang
if ( ( Math .abs ( tmp.get ( tmp.size ( ) - 1) .x - tmp.get ( 0) .x ) == 1 && tmp
.get ( tmp.size ( ) - 1) .y - tmp.get ( 0) .y == 0)
|| ( Math .abs ( tmp.get ( tmp.size ( ) - 1) .y - tmp.get ( 0) .y ) == 1 && tmp
.get ( tmp.size ( ) - 1) .x - tmp.get ( 0) .x == 0) )
break ;
else if ( Math .abs ( tmp.get ( tmp.size ( ) - 1) .x - tmp.get ( 0) .x ) == 1
&& Math .abs ( tmp.get ( tmp.size ( ) - 1) .y - tmp.get ( 0) .y ) == 1)
break ;
else
// sackgasse -> zurück
tmp.remove ( tmp.size ( ) - 1) ;
}
}
Polygon polygon = new Polygon ( ) ;
for ( int i = 0 ; i < tmp.size ( ) ; i++ ) {
polygon.addPoint ( tmp.get ( i) .x , tmp.get ( i) .y ) ;
}
return polygon;
}
Also wir würdet ihr da ran gehen?
P.S. kein Code highlighting mehr