met volgende code kan ik gemakkelijk in java mijn webcam inlezen:
Echter ik kan niet de juiste 4 megapixels selecteren hoe zou dit komen? dit kan wel in het origineel programma.
Iemand enig idee? Groeten.
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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
| import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import javax.media.*;
import javax.media.datasink.*;
import javax.media.format.*;
import javax.media.protocol.*;
import javax.media.util.*;
import javax.media.control.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import com.sun.image.codec.jpeg.*;
// import com.sun.media.vfw.VFWCapture; // JMF 2.1.1c version
import com.sun.media.protocol.vfw.VFWCapture; // JMF 2.1.1e version
public class JWebCam extends JFrame
implements WindowListener, ComponentListener
{
protected final static int MIN_WIDTH = 320;
protected final static int MIN_HEIGHT = 240;
protected static int shotCounter = 1;
protected JLabel statusBar = null;
protected JPanel visualContainer = null;
protected Component visualComponent = null;
protected JToolBar toolbar = null;
protected MyToolBarAction formatButton = null;
protected MyToolBarAction captureButton = null;
protected Player player = null;
protected CaptureDeviceInfo webCamDeviceInfo = null;
protected MediaLocator ml = null;
protected Dimension imageSize = null;
protected FormatControl formatControl = null;
protected VideoFormat currentFormat = null;
protected Format[] videoFormats = null;
protected MyVideoFormat[] myFormatList = null;
protected boolean initialised = false;
/* --------------------------------------------------------------
* Constructor
* -------------------------------------------------------------- */
public JWebCam ( String frameTitle )
{
super ( frameTitle );
try
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}
catch ( Exception cnfe )
{
System.out.println ("Note : Cannot load look and feel settings");
}
setSize ( 320, 260 ); // default size...
addWindowListener ( this );
addComponentListener ( this );
getContentPane().setLayout ( new BorderLayout() );
visualContainer = new JPanel();
visualContainer.setLayout ( new BorderLayout() );
getContentPane().add ( visualContainer, BorderLayout.CENTER );
statusBar = new JLabel ("");
statusBar.setBorder ( new EtchedBorder() );
getContentPane().add ( statusBar, BorderLayout.SOUTH );
}
/* --------------------------------------------------------------
* Initialise
*
* @returns true if web cam is detected
* -------------------------------------------------------------- */
public boolean initialise ( )
throws Exception
{
return ( initialise ( autoDetect() ) );
}
/* -------------------------------------------------------------------
* Initialise
*
* @params _deviceInfo, specific web cam device if not autodetected
* @returns true if web cam is detected
* ------------------------------------------------------------------- */
public boolean initialise ( CaptureDeviceInfo _deviceInfo )
throws Exception
{
statusBar.setText ( "Initialising...");
webCamDeviceInfo = _deviceInfo;
if ( webCamDeviceInfo != null )
{
statusBar.setText ( "Connecting to : " + webCamDeviceInfo.getName() );
try
{
setUpToolBar();
getContentPane().add ( toolbar, BorderLayout.NORTH );
ml = webCamDeviceInfo.getLocator();
if ( ml != null )
{
player = Manager.createRealizedPlayer ( ml );
if ( player != null )
{
player.start();
formatControl = (FormatControl)player.getControl ( "javax.media.control.FormatControl" );
videoFormats = webCamDeviceInfo.getFormats();
visualComponent = player.getVisualComponent();
if ( visualComponent != null )
{
visualContainer.add ( visualComponent, BorderLayout.CENTER );
myFormatList = new MyVideoFormat[videoFormats.length];
for ( int i=0; i<videoFormats.length; i++ )
{
myFormatList[i] = new MyVideoFormat ( (VideoFormat)videoFormats[i] );
}
Format currFormat = formatControl.getFormat();
if ( currFormat instanceof VideoFormat )
{
currentFormat = (VideoFormat)currFormat;
imageSize = currentFormat.getSize();
visualContainer.setPreferredSize ( imageSize );
setSize ( imageSize.width, imageSize.height + statusBar.getHeight() + toolbar.getHeight() );
}
else
{
System.err.println ("Error : Cannot get current video format");
}
invalidate();
pack();
return ( true );
}
else
{
System.err.println ("Error : Could not get visual component");
return ( false );
}
}
else
{
System.err.println ("Error : Cannot create player");
statusBar.setText ( "Cannot create player" );
return ( false );
}
}
else
{
System.err.println ("Error : No MediaLocator for " + webCamDeviceInfo.getName() );
statusBar.setText ( "No Media Locator for : " + webCamDeviceInfo.getName() );
return ( false );
}
}
catch ( IOException ioEx )
{
statusBar.setText ( "Connecting to : " + webCamDeviceInfo.getName() );
return ( false );
}
catch ( NoPlayerException npex )
{
statusBar.setText ("Cannot create player");
return ( false );
}
catch ( CannotRealizeException nre )
{
statusBar.setText ( "Cannot realize player");
return ( false );
}
}
else
{
return ( false );
}
}
/* -------------------------------------------------------------------
* Dynamically create menu items
*
* @returns the device info object if found, null otherwise
* ------------------------------------------------------------------- */
public void setFormat ( VideoFormat selectedFormat )
{
if ( formatControl != null )
{
player.stop();
imageSize = selectedFormat.getSize();
formatControl.setFormat ( selectedFormat );
player.start();
statusBar.setText ( "Format :" + selectedFormat );
currentFormat = selectedFormat;
visualContainer.setPreferredSize ( currentFormat.getSize() );
setSize ( imageSize.width, imageSize.height + statusBar.getHeight() + toolbar.getHeight() );
}
else
{
System.out.println ("Visual component not an instance of FormatControl");
statusBar.setText ( "Visual component cannot change format" );
}
}
public VideoFormat getFormat ( )
{
return ( currentFormat );
}
protected void setUpToolBar ( )
{
toolbar = new JToolBar();
// Note : If you supply the 16 x 16 bitmaps then you can replace
// the commented line in the MyToolBarAction constructor
formatButton = new MyToolBarAction ( "Resolution", "BtnFormat.jpg" );
captureButton = new MyToolBarAction ( "Capture", "BtnCapture.jpg" );
toolbar.add ( formatButton);
toolbar.add ( captureButton );
getContentPane().add ( toolbar, BorderLayout.NORTH );
}
protected void toolbarHandler ( MyToolBarAction actionBtn )
{
if ( actionBtn == formatButton )
{
Object selected = JOptionPane.showInputDialog (this,
"Select Video format",
"Capture format selection",
JOptionPane.INFORMATION_MESSAGE,
null, // Icon icon,
myFormatList, // videoFormats,
currentFormat );
if ( selected != null )
{
setFormat ( ((MyVideoFormat)selected).format );
}
}
else if ( actionBtn == captureButton )
{
Image photo = grabFrameImage ( );
if ( photo != null )
{
MySnapshot snapshot = new MySnapshot ( photo, new Dimension ( imageSize ) );
}
else
{
System.err.println ("Error : Could not grab frame");
}
}
}
/* -------------------------------------------------------------------
* autoDetects the first web camera in the system
* searches for video for windows ( vfw ) capture devices
*
* @returns the device info object if found, null otherwise
* ------------------------------------------------------------------- */
public CaptureDeviceInfo autoDetect ( )
{
Vector list = CaptureDeviceManager.getDeviceList (null );
CaptureDeviceInfo devInfo = null;
if ( list != null )
{
String name;
for ( int i=0; i<list.size(); i++ )
{
devInfo = (CaptureDeviceInfo)list.elementAt ( i );
name = devInfo.getName();
if ( name.startsWith ("vfw:") )
{
break;
}
}
if ( devInfo != null && devInfo.getName().startsWith("vfw:") )
{
return ( devInfo );
}
else
{
for ( int i = 0; i < 10; i++ )
{
try
{
name = VFWCapture.capGetDriverDescriptionName ( i );
if (name != null && name.length() > 1)
{
devInfo = com.sun.media.protocol.vfw.VFWSourceStream.autoDetect ( i );
if ( devInfo != null )
{
return ( devInfo );
}
}
}
catch ( Exception ioEx )
{
// ignore errors detecting device
statusBar.setText ( "AutoDetect failed : " + ioEx.getMessage() );
}
}
return ( null );
}
}
else
{
return ( null );
}
}
/* -------------------------------------------------------------------
* deviceInfo
*
* @note outputs text information
* ------------------------------------------------------------------- */
public void deviceInfo ( )
{
if ( webCamDeviceInfo != null )
{
Format[] formats = webCamDeviceInfo.getFormats();
if ( ( formats != null ) && ( formats.length > 0 ) )
{
}
for ( int i=0; i<formats.length; i++ )
{
Format aFormat = formats[i];
if ( aFormat instanceof VideoFormat )
{
Dimension dim = ((VideoFormat)aFormat).getSize();
// System.out.println ("Video Format " + i + " : " + formats[i].getEncoding() + ", " + dim.width + " x " + dim.height );
}
}
}
else
{
System.out.println ("Error : No web cam detected");
}
}
/* -------------------------------------------------------------------
* grabs a frame's buffer from the web cam / device
*
* @returns A frames buffer
* ------------------------------------------------------------------- */
public Buffer grabFrameBuffer ( )
{
if ( player != null )
{
FrameGrabbingControl fgc = (FrameGrabbingControl)player.getControl ( "javax.media.control.FrameGrabbingControl" );
if ( fgc != null )
{
return ( fgc.grabFrame() );
}
else
{
System.err.println ("Error : FrameGrabbingControl is null");
return ( null );
}
}
else
{
System.err.println ("Error : Player is null");
return ( null );
}
}
/* -------------------------------------------------------------------
* grabs a frame's buffer, as an image, from the web cam / device
*
* @returns A frames buffer as an image
* ------------------------------------------------------------------- */
public Image grabFrameImage ( )
{
Buffer buffer = grabFrameBuffer();
if ( buffer != null )
{
// Convert it to an image
BufferToImage btoi = new BufferToImage ( (VideoFormat)buffer.getFormat() );
if ( btoi != null )
{
Image image = btoi.createImage ( buffer );
if ( image != null )
{
return ( image );
}
else
{
System.err.println ("Error : BufferToImage cannot convert buffer");
return ( null );
}
}
else
{
System.err.println ("Error : cannot create BufferToImage instance");
return ( null );
}
}
else
{
System.out.println ("Error : Buffer grabbed is null");
return ( null );
}
}
/* -------------------------------------------------------------------
* Closes and cleans up the player
*
* ------------------------------------------------------------------- */
public void playerClose ( )
{
if ( player != null )
{
player.close();
player.deallocate();
player = null;
}
}
public void windowClosing ( WindowEvent e )
{
playerClose();
System.exit ( 1 );
}
public void componentResized ( ComponentEvent e )
{
Dimension dim = getSize();
boolean mustResize = false;
if ( dim.width < MIN_WIDTH )
{
dim.width = MIN_WIDTH;
mustResize = true;
}
if ( dim.height < MIN_HEIGHT )
{
dim.height = MIN_HEIGHT;
mustResize = true;
}
if ( mustResize )
setSize ( dim );
}
public void windowActivated ( WindowEvent e ) { }
public void windowClosed ( WindowEvent e ) { }
public void windowDeactivated ( WindowEvent e ) { }
public void windowDeiconified ( WindowEvent e ) { }
public void windowIconified ( WindowEvent e ) { }
public void windowOpened ( WindowEvent e ) { }
public void componentHidden(ComponentEvent e) { }
public void componentMoved(ComponentEvent e) { }
public void componentShown(ComponentEvent e) { }
protected void finalize ( ) throws Throwable
{
playerClose();
super.finalize();
}
class MyToolBarAction extends AbstractAction
{
public MyToolBarAction ( String name, String imagefile )
{
// Note : Use version this if you supply your own toolbar icons
// super ( name, new ImageIcon ( imagefile ) );
super ( name );
}
public void actionPerformed ( ActionEvent event )
{
toolbarHandler ( this );
}
};
class MyVideoFormat
{
public VideoFormat format;
public MyVideoFormat ( VideoFormat _format )
{
format = _format;
}
public String toString ( )
{
Dimension dim = format.getSize();
return ( format.getEncoding() + " [ " + dim.width + " x " + dim.height + " ]" );
}
};
class MySnapshot extends JFrame
{
protected Image photo = null;
protected int shotNumber;
public MySnapshot ( Image grabbedFrame, Dimension imageSize )
{
super ( );
shotNumber = shotCounter++;
setTitle ( "Photo" + shotNumber );
photo = grabbedFrame;
setDefaultCloseOperation ( WindowConstants.DISPOSE_ON_CLOSE );
int imageHeight = photo.getWidth ( this );
int imageWidth = photo.getHeight ( this );
setSize ( imageSize.width, imageSize.height );
final FileDialog saveDialog = new FileDialog ( this, "Save JPEG", FileDialog.SAVE );
final JFrame thisCopy = this;
saveDialog.setFile ( "Photo" + shotNumber );
addWindowListener ( new WindowAdapter()
{
public void windowClosing ( WindowEvent e )
{
saveDialog.show();
String filename = saveDialog.getFile();
if ( filename != null )
{
if ( saveJPEG ( filename ) )
{
JOptionPane.showMessageDialog ( thisCopy, "Saved " + filename );
setVisible ( false );
dispose();
}
else
{
JOptionPane.showMessageDialog ( thisCopy, "Error saving " + filename );
}
}
else
{
setVisible ( false );
dispose();
}
}
}
);
setVisible ( true );
}
public void paint ( Graphics g )
{
g.drawImage ( photo, 0, 0, getWidth(), getHeight(), this );
}
/* -------------------------------------------------------------------
* Saves an image as a JPEG
*
* @params the image to save
* @params the filename to save the image as
* ------------------------------------------------------------------- */
public boolean saveJPEG ( String filename )
{
boolean saved = false;
BufferedImage bi = new BufferedImage ( photo.getWidth(null),
photo.getHeight(null),
BufferedImage.TYPE_INT_RGB );
Graphics2D g2 = bi.createGraphics();
g2.drawImage ( photo, null, null );
FileOutputStream out = null;
try
{
out = new FileOutputStream ( filename );
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder ( out );
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam ( bi );
param.setQuality ( 1.0f, false ); // 100% high quality setting, no compression
encoder.setJPEGEncodeParam ( param );
encoder.encode ( bi );
out.close();
saved = true;
}
catch ( Exception ex )
{
System.out.println ("Error saving JPEG : " + ex.getMessage() );
}
return ( saved );
}
} // of MySnapshot
public static void main (String[] args )
{
try
{
JWebCam myWebCam = new JWebCam ( "TimeSlice Web Cam Capture" );
myWebCam.setVisible ( true );
if ( !myWebCam.initialise() )
{
System.out.println ("Web Cam not detected / initialised");
}
}
catch ( Exception ex )
{
ex.printStackTrace();
}
}
} |
Echter ik kan niet de juiste 4 megapixels selecteren hoe zou dit komen? dit kan wel in het origineel programma.
Iemand enig idee? Groeten.