Bonjour ,

Je suis en train de programmer en Java avec Processing, une application pour un Banc de vérification des composants sur cartes électronique (Stage) par traitement d'image en utilisant openCv et controlP5 pour l'interface graphique.

J'utilise de simple Jframe pour qu'à chaque appui sur un bouton: mon programme ouvre ainsi une nouvelle fenêtre avec notamment mon premier bouton qui ouvre la webcam dans une nouvelle fenêtre afin permettre à l'utilisateur d'ajuster la position de celle-ci avec les glissières verticales et horizontal du banc, et ensuite il suffit tout simplement d'appuyer sur une touche pour qu'il ferme la fenêtre.

Seulement voilà, je ne peut le faire qu'une fois ! Je ne comprend pas pourquoi je n peut pas ré-ouvrir la fenêtre, j'ai l'impression qu'il na pas tué le processus et que je ne peut le ré-ouvrir, ou que l'objet cam n'est plus disponible pour cette fenêtre.

Si vous pouvez m'aider, je vous en remercie d'avance !

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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
 
 
//utilisation des bibliothèques opencv/controlP5
import gab.opencv.*;
import processing.video.*;
import java.awt.*;
import controlP5.*;
import javax.swing.JFrame;
import java.awt.event.WindowAdapter;
import javax.swing.*;
 
// déclaration des frames
PFrame configurePositionWebcam = null;
ConfigurePositionWebcam s;
 
PFrame2 TakeACalibrationPicture = null;
TakeACalibrationPicture s1;
 
PFrame3 CheckYourCard = null;
CheckYourCard s2;
 
PFrame4 FindYourMissingComponent = null;
FindYourMissingComponent s3;
 
 
// déclaration des objets
 
Capture cam;  // représentation de la cam
OpenCV opencv;  // objet openCV
ControlP5 cp5; // objet d'IHM
String filename; // Dossier
int compt=0;  // Variable d'incrémentation du nom de fichier
PImage imgSrc; // Variable d'enregistrement pour fichier Image
PImage photoInit, ChekYourCard, findYourMissingComponent;
PImage logo;
 
// differents colors
int jaune=color(211,195,0);
int vert=color(103,215,52);
int rouge=color(246,51,51);
int bleu=color(59,50,253);
int noir=color(0,0,0);
int blanc=color(255,255,255);
int bleuclair=color(0,255,255); 
int violet=color(255,0,255); 
int orange=color(245,142,0);
 
//Frame for a configuration the position of the webcam
public class PFrame extends JFrame {
  public PFrame() {
 
    setBounds(0, 0, 660, 520);
    s = new ConfigurePositionWebcam();
    add(s);
    s.init();
    println("Configure the position of the webcam");
    show();     
    // Fermeture de la fenêtre
       /*addWindowListener(new WindowAdapter()
       {
          public void windowClosing(WindowEvent e) {  System.exit(0); }
       });*/
 
    /*if(key=='x'){
        System.exit(0);
    }*/
  }
}
 
//Frame for take a calibration picture
public class PFrame2 extends JFrame {
   public  PFrame2() {
 
    setBounds(0, 0, 660, 520); 
    s1= new TakeACalibrationPicture();
    add(s1);
    s1.init();
    println("Take a calibration picture");
    show();
  }
}
 
//Frame for Check your card
public class PFrame3 extends JFrame {
   public PFrame3() {
    setBounds(0, 0, 660, 520);
    s2= new CheckYourCard();
    add(s2);
    s2.init();
    println("Check your card");
    show();
  }
}
 
//Frame for Check your card
public class PFrame4 extends JFrame {
   public PFrame4() {
    setBounds(0, 0, 660, 520);
    s3= new FindYourMissingComponent();
    add(s3);
    s3.init();
    println("Find your missing component");
    show();
  }
}
 
 
 
 
//setup for the main window
 
 void setup(){
 size(400,400); // taille de la fenêtre
 cp5 = new ControlP5(this); // objet d'interface graphique
 
 // create a new button with name 'adjust the webcam'
 cp5.addButton("Adjust the webcam")
    .setValue(0)
    .setPosition(100,50)
    .setSize(200,19)
    //.setColor(noir);
    .setColorBackground(orange)
 
    .addCallback(
    new CallbackListener() {
    public void controlEvent(CallbackEvent theEvent) {
      switch(theEvent.getAction()) {
        case(ControlP5.ACTION_PRESSED):
        println("Adjust the webcam is pressed");
         if(configurePositionWebcam==null){
                         configurePositionWebcam = new PFrame();
                         configurePositionWebcam.setVisible(true);
                         configurePositionWebcam.setTitle("Adjust the webcam");
                         configurePositionWebcam.setLocationRelativeTo(null);
                         configurePositionWebcam.setDefaultCloseOperation(PFrame.DISPOSE_ON_CLOSE);                  // C'EST ICI QUE JE DISPOSE LA FENETRE
 
                         /*try{
                              Thread.sleep(1000); 
                         }  catch(InterruptedException e) {
                             e.printStackTrace();
                            }*/
                       }
 
        break;
 
        case(ControlP5.ACTION_RELEASED):
        case(ControlP5.ACTION_RELEASEDOUTSIDE):
        println("send MIDI on release, note off.");
        break;
      }
    } // end of controlEvent
  } // end of CallBackListener
    ); // end of addCallback
    ;  // end of addButton ' adjustTheWebcam'
 
  // create a new button with name 'take a calibration picture'
  cp5.addButton("Take a calibration picture")
     .setValue(100)
     .setPosition(100,100)
     .setSize(200,19)
     .setColorBackground(rouge)
     .addCallback(
    new CallbackListener() {
    public void controlEvent(CallbackEvent theEvent) {
      switch(theEvent.getAction()) {
        case(ControlP5.ACTION_PRESSED):
        println("Take a calibration picture is pressed");
         if(TakeACalibrationPicture==null){
                         TakeACalibrationPicture = new PFrame2();
                         TakeACalibrationPicture.setTitle("Take a calibration picture");
                         TakeACalibrationPicture.setLocationRelativeTo(null);
                         TakeACalibrationPicture.setDefaultCloseOperation(PFrame2.DISPOSE_ON_CLOSE);
                         TakeACalibrationPicture.setVisible(true);
                       }
 
        break;
 
        case(ControlP5.ACTION_RELEASED):
        case(ControlP5.ACTION_RELEASEDOUTSIDE):
        println("send MIDI on release, note off.");
        break;
      }
    } // end of controlEvent
  } // end of CallBackListener
    ); // end of addCallback
     ;
 
// create a new button with name 'take a picture of your card'
  cp5.addButton("Take a picture of your card")
     .setValue(100)
     .setPosition(100,150)
     .setSize(200,19)
     .setColorBackground(bleu)
     .addCallback(
    new CallbackListener() {
    public void controlEvent(CallbackEvent theEvent) {
      switch(theEvent.getAction()) {
        case(ControlP5.ACTION_PRESSED):
        println("Take a calibration picture is pressed");
         if(CheckYourCard==null){
                         CheckYourCard = new PFrame3();
                         CheckYourCard.setTitle("Take a picture of your card");
                         CheckYourCard.setLocationRelativeTo(null);
                         CheckYourCard.setDefaultCloseOperation(PFrame3.DISPOSE_ON_CLOSE);
                         CheckYourCard.setVisible(true);
 
                       }
 
        break;
 
        case(ControlP5.ACTION_RELEASED):
        case(ControlP5.ACTION_RELEASEDOUTSIDE):
        println("send MIDI on release, note off.");
        break;
      }
    } // end of controlEvent
  } // end of CallBackListener
    ); // end of addCallback
     ;
  // create a new button with name 'check your electronic card'   
  cp5.addButton("Check your electronic card")
     .setPosition(100,200)
     .setSize(200,19)
     .setValue(0)
     .setColorBackground(jaune)
     .addCallback(
    new CallbackListener() {
    public void controlEvent(CallbackEvent theEvent) {
      switch(theEvent.getAction()) {
        case(ControlP5.ACTION_PRESSED):
        println("Take a calibration picture is pressed");
         if(FindYourMissingComponent==null){
                         FindYourMissingComponent = new PFrame4();
                         FindYourMissingComponent.setTitle("Find your missing component");
                         FindYourMissingComponent.setLocationRelativeTo(null);
                         FindYourMissingComponent.setDefaultCloseOperation(PFrame3.DISPOSE_ON_CLOSE);
                         FindYourMissingComponent.setVisible(true);
                       }
 
        break;
 
        case(ControlP5.ACTION_RELEASED):
        case(ControlP5.ACTION_RELEASEDOUTSIDE):
        println("send MIDI on release, note off.");
        break;
      }
    } // end of controlEvent
  } // end of CallBackListener
    ); // end of addCallback
     ;
 
 
}
 
// Draw for the main frame
void draw() {
  scale(1); // echelle de la webcam
  background(vert);  // fond de l'interface
   logo =loadImage("logo.jpg");
  image(logo,100,300);
  }
 
 
// configure position webcam
 
public class ConfigurePositionWebcam extends PApplet {
 
 void setup() {
  size(640, 480);
  cam = new Capture(this, 640/2, 480/2);
  opencv = new OpenCV(this, 640/2, 480/2);
 cam.start(); 
}
void draw() {
  scale(2);
  opencv.loadImage(cam);
  image(cam, 0, 0 );
  noFill();
  stroke(0, 255, 0);
  strokeWeight(3);
}
 
 
void keyPressed() { 
 
                                         // C'EST ICI QUE JE VEUT FERMER MA FENETRE SI J'APPUI SUR UNE TOUCHE             
 
 
     // configurePositionWebcam.setDefaultCloseOperation(PFrame.DISPOSE_ON_CLOSE);
      cam.stop();   // if you press an touch on the keyboard you close the webcam because you have finish to put the webcam in a correctly position
      dispose();
     // frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
 
 
}
void captureEvent(Capture c) {
  c.read();
}
 
}
 
// Take a Calibration picture
 
public class TakeACalibrationPicture extends PApplet {
 
void setup() {
  size(640, 480);
  cam = new Capture(this, 640/2, 480/2);
  opencv = new OpenCV(this, 640/2, 480/2);
  cam.start(); 
  imgSrc = createImage(600,600,RGB);
}
void draw() {
  scale(2);
  opencv.loadImage(cam);
  image(cam, 0, 0 );
  noFill();
  stroke(0, 255, 0);
  strokeWeight(3);
}
void keyPressed() { 
  imgSrc=cam.get();
  //imgSrc.save("Photo"+str(compt));
  //compt=compt+1;
  imgSrc.save("PhotoInit");
  delay(1000);
   cam.stop();
   dispose();
}
void captureEvent(Capture c) {
  c.read();
}
 
}
 
 
//Check your card
 
public class CheckYourCard extends PApplet {
 
void setup() {
  imgSrc = createImage(600,600,RGB);
  size(640, 480);
  cam = new Capture(this, 640/2, 480/2);
  opencv = new OpenCV(this, 640/2, 480/2);
  cam.start(); 
}
void draw() {
  scale(2);
  opencv.loadImage(cam);
  image(cam, 0, 0 );
  noFill();
  stroke(0, 255, 0);
  strokeWeight(3);
}
void keyPressed() { 
  if(key=='t') {      // 't' for take
    //imgSrc.save("PhotoInit");
    imgSrc=cam.get();
    imgSrc.save("Photo"+str(compt));
    compt=compt+1;
  }
 
  if(key=='e'){   // 'e' for exit
 
  delay(1000);
   cam.stop();
   dispose();
  }
}
void captureEvent(Capture c) {
  c.read();
}
 
}
 
//Find your missing component
 
public class FindYourMissingComponent extends PApplet {
 
void setup() {
  photoInit = loadImage("PhotoInit.jpg");
  ChekYourCard = loadImage("Photo0.jpg");
  delay(1000);
  size(photoInit.width, photoInit.height);
 
  opencv = new OpenCV(this, photoInit);
  opencv.diff(ChekYourCard);
  findYourMissingComponent = opencv.getSnapshot();
 
// opencv.useColor();
// opencv.loadImage(ChekYourCard);
// opencv.diff(ChekYourCard);
// colorDiff = opencv.getSnapshot();
 
}
 
void draw() {
  pushMatrix();
  scale(0.5);
  image(photoInit, 0, 0);
  image(ChekYourCard, photoInit.width, 0);
// image(colorDiff, 0, photoInit.height);
  image(findYourMissingComponent, photoInit.width, photoInit.height);
  popMatrix();
 
  fill(255);
  text("photoInit", 10, 20);
  text("ChekYourCard", photoInit.width/2 +10, 20);
  text("findYourMissingComponent", photoInit.width/2 + 10, photoInit.height/2+ 20);
 
// text("color diff", 10, photoInit.height/2+ 20);
}
 
}
 
 
//Fin du programme on stop tous
public void stop(){
   super.stop();  
}