質問編集履歴

1

補足説明 テストプログラムを作成して分かったこと

2020/04/16 03:16

投稿

yk3125
yk3125

スコア91

test CHANGED
File without changes
test CHANGED
@@ -143,3 +143,389 @@
143
143
  }
144
144
 
145
145
  }
146
+
147
+
148
+
149
+
150
+
151
+ 上記のPGでは分かりにくいかと思い、下記のようなテストプログラムを作りました。
152
+
153
+ "切替"でカメラ1,2が切り替わるのですが、
154
+
155
+ 最初にVideoCapture cap = new VideoCapture(4); を表示してから
156
+
157
+ VideoCapture cap = new VideoCapture(0); を実行させないとカメラ2台の画像取得ができません。
158
+
159
+ 0と4を逆にするとダメでした。一応、不安定ですが動きました。
160
+
161
+ raspberry pi4 の能力不足なのか動きはカクカクです。timeoutもでますが、しばらくすると表示もされます。
162
+
163
+ やはりraspberry pi4でUSBカメラ2台はむりなのでしょうか。
164
+
165
+
166
+
167
+ import java.awt.BorderLayout;
168
+
169
+ import java.awt.EventQueue;
170
+
171
+ import java.awt.Image;
172
+
173
+ import java.awt.*;
174
+
175
+
176
+
177
+ import javax.swing.JLabel;
178
+
179
+ import javax.swing.JTabbedPane;
180
+
181
+ import javax.imageio.ImageIO;
182
+
183
+ import javax.swing.JFrame;
184
+
185
+ import javax.swing.JPanel;
186
+
187
+ import javax.swing.border.EmptyBorder;
188
+
189
+ import javax.swing.JButton;
190
+
191
+ import javax.swing.AbstractAction;
192
+
193
+ import java.awt.event.ActionEvent;
194
+
195
+ import java.io.ByteArrayInputStream;
196
+
197
+ import java.text.SimpleDateFormat;
198
+
199
+ import java.util.Date;
200
+
201
+ import java.util.TimerTask;
202
+
203
+ import java.util.Timer;
204
+
205
+
206
+
207
+ import javax.swing.Action;
208
+
209
+
210
+
211
+ import org.opencv.core.Mat;
212
+
213
+ import org.opencv.core.MatOfByte;
214
+
215
+ import org.opencv.highgui.Highgui;
216
+
217
+ import org.opencv.highgui.VideoCapture;
218
+
219
+
220
+
221
+ public class usbCam01 extends JFrame {
222
+
223
+ private static JPanel contentPane;
224
+
225
+ private static ImageViewer picmain;
226
+
227
+ private final Action btnSetuzoku_click = new SwingAction();
228
+
229
+ private final Action btnTojiru_click = new SwingAction_1();
230
+
231
+ private static int pcnt = 0;
232
+
233
+ private static int mpgend = 0;
234
+
235
+ private static CamThread1 camth1;
236
+
237
+ private static CamThread2 camth2;
238
+
239
+
240
+
241
+ /**
242
+
243
+ * Launch the application.
244
+
245
+ */
246
+
247
+ public static void main(String[] args) {
248
+
249
+ // DB Sakusei
250
+
251
+ int rc = 0;
252
+
253
+
254
+
255
+ String s1 = System.getProperty("java.library.path");
256
+
257
+ System.out.println("+++" + s1);
258
+
259
+ s1 = System.getProperty("java.class.path");
260
+
261
+ System.out.println("***" + s1);
262
+
263
+
264
+
265
+ System.loadLibrary("opencv_java2413");
266
+
267
+ EventQueue.invokeLater(new Runnable() {
268
+
269
+ public void run() {
270
+
271
+ try {
272
+
273
+ usbCam01 frame = new usbCam01();
274
+
275
+ frame.setVisible(true);
276
+
277
+ } catch (Exception e) {
278
+
279
+ e.printStackTrace();
280
+
281
+ }
282
+
283
+ }
284
+
285
+ });
286
+
287
+ }
288
+
289
+
290
+
291
+ /**
292
+
293
+ * Create the frame.
294
+
295
+ */
296
+
297
+ public usbCam01() {
298
+
299
+ setTitle("USBCAMERA");
300
+
301
+ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
302
+
303
+ setBounds(100, 100, 682, 580);
304
+
305
+ contentPane = new JPanel();
306
+
307
+ contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
308
+
309
+ setContentPane(contentPane);
310
+
311
+ contentPane.setLayout(null);
312
+
313
+
314
+
315
+ JButton btnSetuzoku = new JButton("Setuzoku");
316
+
317
+ btnSetuzoku.setAction(btnSetuzoku_click);
318
+
319
+ btnSetuzoku.setBounds(22, 12, 117, 25);
320
+
321
+ contentPane.add(btnSetuzoku);
322
+
323
+
324
+
325
+ JButton btnTojiru = new JButton("切替");
326
+
327
+ btnTojiru.setAction(btnTojiru_click);
328
+
329
+ btnTojiru.setBounds(151, 12, 117, 25);
330
+
331
+ contentPane.add(btnTojiru);
332
+
333
+
334
+
335
+ picmain = new ImageViewer();
336
+
337
+ picmain.setBounds(22, 49, 640, 480);
338
+
339
+ contentPane.add(picmain);
340
+
341
+ picmain.setVisible(true);
342
+
343
+
344
+
345
+ }
346
+
347
+
348
+
349
+ private class SwingAction extends AbstractAction {
350
+
351
+ public SwingAction() {
352
+
353
+ putValue(NAME, "Setuzoku");
354
+
355
+ putValue(SHORT_DESCRIPTION, "Some short description");
356
+
357
+ }
358
+
359
+ public void actionPerformed(ActionEvent e) {
360
+
361
+ // USB CAMERA START
362
+
363
+ try {
364
+
365
+ // 1s Timer Event
366
+
367
+ mpgend=1;
368
+
369
+ camth1 = new CamThread1();
370
+
371
+ camth1.start();
372
+
373
+ }catch(Exception ex){
374
+
375
+ ex.printStackTrace();
376
+
377
+ }
378
+
379
+ }
380
+
381
+ }
382
+
383
+ private class SwingAction_1 extends AbstractAction {
384
+
385
+ // USB CAMERA END
386
+
387
+ public SwingAction_1() {
388
+
389
+ putValue(NAME, "切替");
390
+
391
+ putValue(SHORT_DESCRIPTION, "Some short description");
392
+
393
+ }
394
+
395
+ public void actionPerformed(ActionEvent e) {
396
+
397
+ if(mpgend == 1) { mpgend = 2; } else { mpgend = 1; }
398
+
399
+ }
400
+
401
+ }
402
+
403
+
404
+
405
+
406
+
407
+ private static class CamThread1 extends Thread {
408
+
409
+ public void run() {
410
+
411
+ try {
412
+
413
+ VideoCapture cap = new VideoCapture(4);
414
+
415
+ Thread.sleep(2000);
416
+
417
+ if (!cap.isOpened()) {
418
+
419
+ System.out.println("Error Initializing USB Camera");
420
+
421
+ } else {
422
+
423
+ System.out.println("USB1 Camera OK");
424
+
425
+
426
+
427
+ Mat frame = new Mat();
428
+
429
+ MatOfByte mem = new MatOfByte();
430
+
431
+ while(cap.read(frame)){
432
+
433
+ //cap.retrieve(frame);
434
+
435
+ Highgui.imencode(".png", frame, mem);
436
+
437
+ byte pic[] = mem.toArray();
438
+
439
+ Image im = ImageIO.read(new ByteArrayInputStream(pic));
440
+
441
+ if(pcnt == 5){
442
+
443
+ camth2 = new CamThread2();
444
+
445
+ camth2.start();
446
+
447
+ mpgend = 1;
448
+
449
+ }
450
+
451
+ pcnt++;
452
+
453
+ if(mpgend == 1) picmain.updateImage(im);
454
+
455
+ }
456
+
457
+ System.exit(0);
458
+
459
+ }
460
+
461
+ } catch (Exception ex) {
462
+
463
+ // TODO Auto-generated catch block
464
+
465
+ ex.printStackTrace();
466
+
467
+ }
468
+
469
+ }
470
+
471
+ }
472
+
473
+
474
+
475
+ private static class CamThread2 extends Thread {
476
+
477
+ public void run() {
478
+
479
+ try {
480
+
481
+ VideoCapture cap = new VideoCapture(0);
482
+
483
+ Thread.sleep(2000);
484
+
485
+ if (!cap.isOpened()) {
486
+
487
+ System.out.println("Error Initializing USB Camera");
488
+
489
+ } else {
490
+
491
+ System.out.println("USB2 Camera OK");
492
+
493
+
494
+
495
+ Mat frame = new Mat();
496
+
497
+ MatOfByte mem = new MatOfByte();
498
+
499
+ while(cap.read(frame)){
500
+
501
+ //cap.retrieve(frame);
502
+
503
+ Highgui.imencode(".png", frame, mem);
504
+
505
+ byte pic[] = mem.toArray();
506
+
507
+ Image im = ImageIO.read(new ByteArrayInputStream(pic));
508
+
509
+ if(mpgend == 2) picmain.updateImage(im);
510
+
511
+ }
512
+
513
+ System.exit(0);
514
+
515
+ }
516
+
517
+ } catch (Exception ex) {
518
+
519
+ // TODO Auto-generated catch block
520
+
521
+ ex.printStackTrace();
522
+
523
+ }
524
+
525
+ }
526
+
527
+ }
528
+
529
+
530
+
531
+ }