Java provide a framework API which enables audio, video and other time-based media to be added to applications and applets built on Java technology. We can use The Java Media Framework API (JMF) to capture video/images from a webcam device.
Above picture explains how JMF works, first we initialize capture device, and then create a datasource object, bind it to player object, and display it to user on JFrame window (the TV). Now I will demonstrate you how to implement it.
Requirements
- Java SE 1.6 SDK
- Java Media Framework
- A Java Editor, we use eclipse
Click read more to see the code!
Enumerate Devices
The first step, we must enumerate capture devices and choose which we want to use. Capture device is including audio and video device. In this example, we just choose the first found video capture device.
CaptureDeviceInfo videoDevice=null;
Vector> vd=CaptureDeviceManager.getDeviceList(null);
//enumerate devices
for(int i=0;i0 && formats[0] instanceof VideoFormat){
videoDevice=di;
System.out.println("Video Device: "+di.getName());
break;
}
}
Prepare Capture Device
Once we get the video device we want, we need to get media locator object and create a data souce.
MediaLocator media=videoDevice.getLocator();
try {
DataSource dataSource=Manager.createDataSource(media);
Player player=Manager.createRealizedPlayer(dataSource);
player.start();
} catch (NoDataSourceException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoPlayerException e) {
e.printStackTrace();
} catch (CannotRealizeException e) {
e.printStackTrace();
}
Display It on JFrame
The last step, after we have the player started, get a visual component (a component that display a video), and add it to JFrame.
MediaLocator media=videoDevice.getLocator();
try {
DataSource dataSource=Manager.createDataSource(media);
Player player=Manager.createRealizedPlayer(dataSource);
player.start();
Component visualcomponent=player.getVisualComponent();
visualcomponent.setBounds(0, 0, 320, 240);
Container cp=this.getContentPane();
cp.setLayout(null);
cp.add(visualcomponent);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Webcam");
this.setSize(330, 280);
this.setResizable(false);
this.setVisible(true);
} catch (NoDataSourceException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoPlayerException e) {
e.printStackTrace();
} catch (CannotRealizeException e) {
e.printStackTrace();
}
Here is full source code we just learned.
package com.lcsoft;
import java.awt.Component;
import java.awt.Container;
import java.io.IOException;
import java.util.Vector;
import javax.media.CannotRealizeException;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Format;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoDataSourceException;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.format.VideoFormat;
import javax.media.protocol.DataSource;
import javax.swing.JFrame;
public class WebcamDemo extends JFrame {
/**
*
*/
private static final long serialVersionUID = -272374147411044339L;
/**
* @param args
*/
public static void main(String[] args) {
new WebcamDemo().startup();
}
public void startup(){
CaptureDeviceInfo videoDevice=null;
Vector> vd=CaptureDeviceManager.getDeviceList(null);
//enumerate devices
for(int i=0;i0 && formats[0] instanceof VideoFormat){
videoDevice=di;
System.out.println("Video Device: "+di.getName());
break;
}
}
// if you already know the device name, just use code below
//String VIDEO_DEVICE_NAME="vfw:Microsoft WDM Image Capture (Win32):0";
//CaptureDeviceInfo videoDevice=CaptureDeviceManager.getDevice(VIDEO_DEVICE_NAME);
MediaLocator media=videoDevice.getLocator();
try {
DataSource dataSource=Manager.createDataSource(media);
Player player=Manager.createRealizedPlayer(dataSource);
player.start();
Component visualcomponent=player.getVisualComponent();
visualcomponent.setBounds(0, 0, 320, 240);
Container cp=this.getContentPane();
cp.setLayout(null);
cp.add(visualcomponent);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Webcam");
this.setSize(330, 280);
this.setResizable(false);
this.setVisible(true);
} catch (NoDataSourceException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoPlayerException e) {
e.printStackTrace();
} catch (CannotRealizeException e) {
e.printStackTrace();
}
}
}
We just learned how to use Java Media Framework to display live video from your webcam into your application. Below is an example to take a snapshot and display it on the left of the video viewport in JFrame.
private void snapshot(Player player){
FrameGrabbingControl ctrl=(FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");
FormatControl fctrl=(FormatControl) player.getControl("javax.media.control.FormatControl");
Buffer b=ctrl.grabFrame();
BufferToImage bti=new BufferToImage((VideoFormat) fctrl.getFormat());
Image img=bti.createImage(b);
JLabel lbSnapshotView=new JLabel();
lbSnapshotView.setLocation(320, 0);
lbSnapshotView.setIcon(new ImageIcon(img));
lbSnapshotView.setSize(320,240);
this.getContentPane().add(lbSnapshotView);
this.repaint();
}
Now we learned all the method we need to capture a video on your Java application. We can extend the application to stream via network connection, and display it everywhere.
Reference:
No related posts.
Tags: capture device, java media framework, jmf, snapshot, webcam
