Bonjour,
je débute en programmation et plus particulièrement en java.
J'ai une image trouvée sur internet (peu importe la source) que je transforme en un tableau de pixel.
Code java : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 public static int[] convertToPixels(Image img) { int width = img.getWidth(null); int height = img.getHeight(null); int[] pixel = new int[width * height]; PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixel, 0, width); try { pg.grabPixels(); } catch (InterruptedException e) { throw new IllegalStateException("Error: Interrupted Waiting for Pixels"); } if ((pg.getStatus() & ImageObserver.ABORT) != 0) { throw new IllegalStateException("Error: Image Fetch Aborted"); } return pixel; }
Cette méthode marche très bien. J'essaye actuellement d’effecteur une rotation sur ces pixels afin de pouvoir faire tourner l'image selon un angle choisis. J'utilise cette méthode :
Code java : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 //Méthode pour la rotation des pixels d'une image public int[] rotate( double angle, int[] pixels, int width, int height ) { final double radians = Math.toRadians( angle ); final double cos = Math.cos( radians ); final double sin = Math.sin( radians ); final int[ ] pixels2 = new int[ pixels.length ]; for( int pixel = 0; pixel < pixels2.length; pixel++ ) { pixels2[pixel] = 0xFFFFFF; } for( int x = 0; x < width; x++ ) { for( int y = 0; y < height; y++ ) { final int centerx = width / 2; final int centery = height / 2; final int m = x - centerx; final int n = y - centery; final int j = ( ( int ) ( m * cos + n * sin ) ) + centerx; final int k = ( ( int ) ( n * cos - m * sin ) ) + centery; if( j >= 0 && j < width && k >= 0 && k < height ){ pixels2[ ( y * width + x ) ] = pixels[ ( k * width + j ) ]; } } } return pixels2; }
Premier problème : les bornes de l'image sont toujours définis par les width et height de l'image en entrée. Donc en sortie, je n'ai pas toute l'image à laquelle on a appliquer la rotation. J'aimerai savoir comment connaitre la taille de mon image en sortie après rotation. Il doit y avoir un moyen de connaitre les bornes min et max en x et y et d'y appliquer des calculs de cosinus et de sinus afin de connaître les futures bornes.
Est ce quelqu'un à une idée ?
Merci
Partager