Bonjour,

J'ai développé une classe permettant de parcourir un répertoire (sur un serveur), dans lequel est stocké entre 2000 à 3000 photos en vu de les redimenssionner.

voici mon code:

Code : 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
 
public class TraitementsImage{   
 
 /**
 
 * Type de compression BMP
 
 */
 
   public final static String IMAGE_TYPE_BMP = "BMP";
 
 
 
 /**
 
 * Type de compression WBMP
 
 */
 
   public final static String IMAGE_TYPE_WBMP = "WBMP";
 
 
 
 /**
 
 * Type de compression JPEG
 
 */
 
   public final static String IMAGE_TYPE_JPEG = "JPEG";
 
 
 
 /**
 
 * Type de compression PNG
 
 */
 
   public final static String IMAGE_TYPE_PNG = "PNG";
 
 
 
 
 
 /**
 
 * Conversion sans modification de taille d'un fichier
 
 * @param fichierSource File
 
 * @param fichierDestination File
 
 * @param compressionType String
 
 * @throws IOException 
 
 */
  public static String cvtImage(File fichierSource, File fichierDestination, String compressionType)
 
  {
 
 
 
    // buffer
 
    BufferedImage buf = null;
 
    BufferedImage bufFinal = null;
 
 
 
    // récuperation de l''image dans le buffer
 
    try {
 
    	buf = ImageIO.read(fichierSource);
 
      	}
 
    catch (IOException ex)
 
    	{
 
    	ex.printStackTrace();
        return "non ok";
 
    	}
 
 
 
    //calcul de l''echelle
 
    int width=buf.getWidth();
 
    int height=buf.getHeight();
 
    int widthMsq=130;
 
    int heightMsq=170;
 
    double echelle=0;
 
 
 
    double echelleW=(double) widthMsq / width;
 
    double echelleH=(double) heightMsq / height;
 
    if(echelleW<echelleH) echelle=echelleW; else echelle=echelleH;
 
    echelle=(Math.round(echelle*Math.pow(10,2)) )/ (Math.pow(10,2));
 
 
 
    // Crᅵtion du buffer final widthMsq, heightMsq buf.getWidth(), buf.getHeight()
 
    bufFinal = new BufferedImage((int) (buf.getWidth()* echelle), (int)(buf.getHeight()*echelle),buf.getType());//.TYPE_INT_RGB); // masque
 
 
 
    // Redimensionnement de l''image si volonté
 
    Graphics2D g = (Graphics2D) bufFinal.getGraphics();
 
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
 
    g.drawImage(buf, 0,0, (int) (buf.getWidth()* echelle), (int)(buf.getHeight()*echelle), null);
 
 
 
    g.dispose();
 
 
 
    // Ecriture du fichier destination
 
    try {
 
    	ImageIO.write(bufFinal, compressionType,fichierDestination);
 
    	}
 
    catch (IOException e)
 
    	{
 
    	e.printStackTrace();
 
         return "non ok";
 
    	}
 
 
 
 
    return fichierDestination.toString();
 
  }
 
 
 
  public  static String lstImages(String strRepertoire,String destRepertoire)
 
  {     	
 
	 String resultat=""; 
 
	 File repertoire=new File(strRepertoire);
 
	 File[] listFile = repertoire.listFiles();
 
	 if(listFile !=null)
 
	       for ( int i = 0; i < listFile.length; i++) 
 
	       {
 
               File file = listFile[i];
 
               if( file.toString().substring(file.toString().length()-3,file.toString().length()).equals("jpg"))
 
               {     resultat=cvtImage(file,file,IMAGE_TYPE_JPEG);
 
                     File destination = new File(destRepertoire+"/"+file.toString().split(strRepertoire)[1]);
 
                     file.renameTo(destination);
 
 
               }
 
 
 
           } 
 
 
 
 
 
	  return "ok";    
  }  
}
Le code fonctionne correctement, seulement il me fait un java out of memory après avoir redimensionné 700 photos, je supose que le nombre d'objets crées y soit pour quelque chose.

Je cherche donc a optimiser mon code pour ne plus avoir cette erreur.

Quelqu'un a t'il une idée?

Merci