Précédent   Forum du club des développeurs et IT Pro > Java > Communauté Java
Communauté Java Suivez l'actualité et contribuez à la vie de la communauté francophone Java
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Actualité déjà publiée
 
Outils de la discussion
Publicité
'
Vieux 19/01/2011, 13h44   #1
slim_java
Expert Confirmé
 
Avatar de slim_java
 
Homme Mohamed Slim
Enseignant
Inscription : septembre 2008
Messages : 2 178
Détails du profil
Informations personnelles :
Nom : Homme Mohamed Slim
Âge : 28
Localisation : Tunisie

Informations professionnelles :
Activité : Enseignant
Secteur : Enseignement

Informations forums :
Inscription : septembre 2008
Messages : 2 178
Points : 3 624
Points : 3 624
Envoyer un message via Skype™ à slim_java
Par défaut Traitement d'images en Java avec l'API Java2D



Je vous propose un article sur le traitement d'images en Java.
J'attends vos questions et vos suggestions !

Merci.
__________________

Mon Site

Vive le peuple Tunisien


langage ج (j) : langage de programmation en Arabe

slim_java est déconnecté   Envoyer un message privé Réponse avec citation 50
Vieux 23/01/2011, 21h23   #2
tulipebleu
Membre régulier
 
Inscription : mars 2003
Messages : 134
Détails du profil
Informations forums :
Inscription : mars 2003
Messages : 134
Points : 71
Points : 71
Très bon article. Il est clair, et m'a appris des choses.
J'avais jamais compris très bien compris le fonctionnement des images avec Java 2D. J'ai déjà eu des problèmes avec les images qui n'était pas en RGB. Si j'ai de nouveau ce problème dans une prochaine application, je sais grâce à ton article que le problème doit venir de la classe ColorModel qui doit être mal initialisé.
tulipebleu est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/01/2011, 21h44   #3
slim_java
Expert Confirmé
 
Avatar de slim_java
 
Homme Mohamed Slim
Enseignant
Inscription : septembre 2008
Messages : 2 178
Détails du profil
Informations personnelles :
Nom : Homme Mohamed Slim
Âge : 28
Localisation : Tunisie

Informations professionnelles :
Activité : Enseignant
Secteur : Enseignement

Informations forums :
Inscription : septembre 2008
Messages : 2 178
Points : 3 624
Points : 3 624
Envoyer un message via Skype™ à slim_java
Citation:
Envoyé par tulipebleu Voir le message
Très bon article. Il est clair, et m'a appris des choses.
J'avais jamais compris très bien compris le fonctionnement des images avec Java 2D. J'ai déjà eu des problèmes avec les images qui n'était pas en RGB. Si j'ai de nouveau ce problème dans une prochaine application, je sais grâce à ton article que le problème doit venir de la classe ColorModel qui doit être mal initialisé.

Merci beaucoup tulipebleu !
__________________

Mon Site

Vive le peuple Tunisien


langage ج (j) : langage de programmation en Arabe

slim_java est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 14/04/2011, 04h59   #4
galien
Membre éclairé
 
Inscription : février 2009
Messages : 288
Détails du profil
Informations forums :
Inscription : février 2009
Messages : 288
Points : 344
Points : 344
Par défaut Bon article

qui m'aurait épargné pas mal de perte de temps dans la passé.
Cependant deux ou trois remarques.

Je mentionnerais pour la création d'image la méthode createCompatibleImage de la classe GraphicsConfiguration pour un dessin rapide.

Code :
1
2
3
4
5
6
7
8
 
public static final GraphicsEnvironment GRAPHICS_ENVIRONMENT = 
            GraphicsEnvironment.getLocalGraphicsEnvironment();
    public static final GraphicsConfiguration GRAPHICS_CONFIGURATION = 
            GRAPHICS_ENVIRONMENT.getDefaultScreenDevice().getDefaultConfiguration();
...
 
BufferedImage bi = GRAPHICS_ENVIRONMENT.createCompatibleImage(width, height);
On peut influer sur la qualité du rendu/traitement en java2D via les RenderingHints.

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
 
    private static void setQuality(Graphics2D gd) {
        if(quality == ImageQuality.WORST) {
            gd.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
            gd.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
        } else if (quality == ImageQuality.MEDIUM) {
            gd.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
            gd.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        } else if (quality == ImageQuality.BEST) {
            gd.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            gd.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        }
    }
Pour ce qui est du traitement d'image on peut faire pas mal d'opérations via drawImage comme par exemple:

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
 
    /**
     * Créer une image à partir d'une tableau de bytes.
     * @param buffer
     * @param width
     * @param height
     * @param nbBand
     * @return 
     */
    public static BufferedImage createImagefromBuffer (
            byte[] buffer,
            int width,
            int height,
            int nbBand) {
        try {
            int type = (nbBand == 1)? BufferedImage.TYPE_BYTE_GRAY:
                (nbBand == 3)? BufferedImage.TYPE_3BYTE_BGR:
                (nbBand == 4)? BufferedImage.TYPE_4BYTE_ABGR:
                BufferedImage.TYPE_CUSTOM;
            BufferedImage image = new BufferedImage(width, height, type);
            //Copie les données
            DataBufferByte dbb = (DataBufferByte) image.getRaster().getDataBuffer();
            System.arraycopy(buffer, 0, dbb.getData(), 0, width*height*nbBand);
            return image;
        } catch (ArrayIndexOutOfBoundsException boe) {
            LOGGER.error("createImagefromBuffer", boe);
            return null;
        }
    }
    /**
     * Miroir horizontal
     * @param img
     * @return 
     */    
    public static BufferedImage horizontalflip(BufferedImage img) {
        try {
            int w = img.getWidth();
            int h = img.getHeight();
            BufferedImage result = new BufferedImage(w, h, img.getType());
            Graphics2D g = result.createGraphics();
            setQuality(g);
            g.drawImage(img, 0, 0, w, h, w, 0, 0, h, null);            
            return result;
        } catch (Exception e) {
            LOGGER.error("horizontalflip", e);
            return null;
        }
 
    }
    /**
     * Miroir Vertical
     * @param img
     * @return 
     */
    public static BufferedImage verticalflip(BufferedImage img) {
        try {
            int w = img.getWidth();
            int h = img.getHeight();
            BufferedImage result = new BufferedImage(w, h, img.getColorModel().getTransparency());
            Graphics2D g = result.createGraphics();
            setQuality(g);
            g.drawImage(img, 0, 0, w, h, 0, h, w, 0, null);
            g.dispose();
            return result;
        } catch (Exception e) {
            LOGGER.error("verticalflip", e);
            return null;
        }
    }
    /**
     * Tourne l'image
     * @param img
     * @param angle
     * @return 
     */
    public static BufferedImage rotate(BufferedImage img, int angle) {
        try {
            int w = img.getWidth();
            int h = img.getHeight();
            int dif = 0;
            if(angle == 90 || angle == 270) {
                dif = (w-h)/2;
                w = img.getHeight();
                h = img.getWidth();
            }
            BufferedImage result = new BufferedImage(w, h, img.getType());
            Graphics2D g = result.createGraphics();
            setQuality(g);
            g.rotate(Math.toRadians(angle), w / 2, h / 2);
            g.translate(-dif, dif);
            g.drawImage(img, null, 0, 0);
            return result;
        } catch (Exception e) {
            LOGGER.error("rotate "+angle, e);
            return null;
        }
    }
    /**
     * Redimensionne l'image
     * @param img
     * @param newWidth
     * @param newHeight
     * @return 
     */
    public static BufferedImage resize(BufferedImage img, int newWidth, int newHeight) {
        try {
            int w = img.getWidth();
            int h = img.getHeight();
            BufferedImage dimg = new BufferedImage(newWidth, newHeight, img.getType());
            Graphics2D g = dimg.createGraphics();
            setQuality(g);
            g.drawImage(img, 0, 0, newWidth, newHeight, 0, 0, w, h, null);
            g.dispose();
            return dimg;
        } catch (Exception e) {
            LOGGER.error("resize["+newHeight+", "+newHeight+"]", e);
            return null;
        }
    }
    /**
     * Copie l'image source dans l'image de destination
     * @param src Image source, non nulle.
     * @param dest Image de destination, si nulle créer l'image de retour.
     * @return Copie de l'image source.
     */
    public static BufferedImage copyImage(BufferedImage src, BufferedImage dest) {
        try {
            BufferedImage temp = (dest == null)? getBlanck(src): dest;
            Graphics2D gd = temp.createGraphics();
            gd.drawImage(src, null, 0, 0);
            gd.dispose();
            return temp;
        } catch (Exception e) {
            LOGGER.error("copyImage: src="+src+", dest="+dest, e);
            return null;
        } 
    }
galien est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 06/03/2013, 17h15   #5
tipso
Invité de passage
 
Homme Ramy Temim
Étudiant
Inscription : septembre 2010
Messages : 14
Détails du profil
Informations personnelles :
Nom : Homme Ramy Temim
Localisation : France

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : septembre 2010
Messages : 14
Points : 3
Points : 3
Bonjour tout le monde
Je voulais juste vous remercier pour ce superbe article !
Vive developpez.com
tipso est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Actualité déjà publiée
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 04h18.


 
 
 
 
Partenaires

Hébergement Web