Hope someone with experience in this area can share their experiences.
I. jmf Development Environment Settings
Install
Set environment variable path
Set JMFHOME = C: \ JMF2.1.1
Set CLASSPATH = % JMFHOME % \ lib \ jmf. jar; % JMFHOME % \ lib \ sound. jar;.; % CLASSPATH %
Ii. Example code of the player (copy to Eclipse, export package (runable)
Package jmf. test;
Import java. awt. BorderLayout;
Import java. awt. Component;
Import java. awt. Graphics;
Import java. awt. GraphicsConfiguration;
Import java. awt. GraphicsDevice;
Import java. awt. GraphicsEnvironment;
Import java. awt. HeadlessException;
Import java. awt. Image;
Import java. awt. Transparency;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;
Import java. awt. event. WindowAdapter;
Import java. awt. event. javaswevent;
Import java. awt. image. BufferedImage;
Import java. awt. image. ColorModel;
Import java. awt. image. PixelGrabber;
Import java. io. File;
Import java. io. IOException;
Import java. util. Vector;
Import javax. imageio. ImageIO;
Import javax. media. Buffer;
Import javax. media. CaptureDeviceInfo;
Import javax. media. CaptureDeviceManager;
Import javax. media. Format;
Import javax. media. MediaLocator;
Import javax. media. NoPlayerException;
Import javax. media. control. FrameGrabbingControl;
Import javax. media. format. RGBFormat;
Import javax. media. format. VideoFormat;
Import javax. media. protocol. DataSource;
Import javax. media. protocol. PushBufferDataSource;
Import javax. media. util. BufferToImage;
Import javax. swing. ImageIcon;
Import javax. swing. JButton;
Import javax. swing. JPanel;
Import jmapps. jmstudio. CaptureDialog;
Import jmapps. ui. PlayerFrame;
Import jmapps. util. CDSWrapper;
Import jmapps. util. JMFUtils;
Public class JMFDemo extends PlayerFrame {
Public JMFDemo (){
Super (null, "video capture window ");
}
DataSource;
Private CaptureDeviceInfo infor;
Private MediaLocator mediaLocator;
String str1 = "vfw: Logitech USB Video Camera: 0 ";
String str2 = "vfw: Microsoft WDM Image Capture (Win32): 0 ";
Private String url = "vfw: Microsoft WDM Image Capture (Win32): 0 ";
Private Component com;
Private JPanel;
Private int captureCount = 0;
FrameGrabbingControl controlGrabber;
Public void play (){
If (mediaPlayerCurrent. getState ()! = MediaPlayerCurrent. Started ){
MediaPlayerCurrent. start ();
}
}
Private void init () throws NoPlayerException, IOException {
// Setdefaclocloseoperation (JFrame. DO_NOTHING_ON_CLOSE );
String nameCaptureDeviceAudio = null;
String nameCaptureDeviceVideo = null;
CaptureDialog dialogCapture = new CaptureDialog (this, null );
DialogCapture. show ();
If (dialogCapture. getAction () = CaptureDialog. ACTION_CANCEL)
Return;
CaptureDeviceInfo cdi = dialogCapture. getAudioDevice ();
If (cdi! = Null & dialogCapture. isAudioDeviceUsed ())
NameCaptureDeviceAudio = cdi. getName ();
Cdi = dialogCapture. getVideoDevice ();
If (cdi! = Null & dialogCapture. isVideoDeviceUsed ())
NameCaptureDeviceVideo = cdi. getName ();
DataSource = JMFUtils. createCaptureDataSource (nameCaptureDeviceAudio,
DialogCapture. getAudioFormat (), nameCaptureDeviceVideo,
DialogCapture. getVideoFormat ());
DataSource cdswrapper = new CDSWrapper (
(PushBufferDataSource) dataSource );
DataSource = cdswrapper;
DataSource. connect ();
Open (dataSource );
JPanel controlPanel = new JPanel ();
ControlPanel. setName ("controlPnael is here ");
Add (BorderLayout. SOUTH, controlPanel );
JButton capture = new JButton ("Capture Image ");
Capture. addActionListener (new ActionListener (){
Public void actionreceivmed (ActionEvent arg0 ){
MediaPlayerCurrent. stop ();
Buffer bufferFrame;
BufferToImage bufferToImage;
Image image;
BufferedImage bi;
ControlGrabber = (FrameGrabbingControl) mediaPlayerCurrent
. GetControl ("javax. media. control. FrameGrabbingControl ");
BufferFrame = controlGrabber. grabFrame ();
BufferToImage = new BufferToImage (VideoFormat) bufferFrame
. GetFormat ());
Image = bufferToImage. createImage (bufferFrame );
File out = new File ("capture" + (++ captureCount) + ". png ");
Try {
Bi = toBufferedImage (image );
ImageIO. write (bi, "png", out );
} Catch (IOException e1 ){
E1.printStackTrace ();
}
MediaPlayerCurrent. start ();
}
});
ControlPanel. add (BorderLayout. CENTER, capture );
JButton playStop = new JButton ("stop ");
// Add (BorderLayout. SOUTH, playControl );
PlayStop. addActionListener (new ActionListener (){
// @ Override
Public void actionreceivmed (ActionEvent arg0 ){
MediaPlayerCurrent. stop ();
}
});
ControlPanel. add (BorderLayout. EAST, playStop );
JButton playStart = new JButton ("start ");
// Add (BorderLayout. SOUTH, playControl );
PlayStart. addActionListener (new ActionListener (){
// @ Override
Public void actionreceivmed (ActionEvent arg0 ){
// MediaPlayerCurrent. stop ();
If (mediaPlayerCurrent. getState ()! = MediaPlayerCurrent. Started ){
MediaPlayerCurrent. start ();
}
}
});
ControlPanel. add (BorderLayout. WEST, playStart );
AddWindowListener (new WindowAdapter (){
// @ Override
Public void windowClosing (WindowEvent e ){
MediaPlayerCurrent. close ();
DataSource. disconnect ();
System. out. println ("exit .....");
System. exit (0 );
}
});
}
Public static boolean hasAlpha (Image image ){
// If buffered image, the color model is readily available
If (image instanceof BufferedImage ){
BufferedImage bimage = (BufferedImage) image;
Return bimage. getColorModel (). hasAlpha ();
}
// Use a pixel grabber to retrieve the image's color model;
// Grabbing a single pixel is usually sufficient
PixelGrabber pg = new PixelGrabber (image, 0, 0, 1, 1, false );
Try {
Pg. grabPixels ();
} Catch (InterruptedException e ){
}
// Get the image's color model
ColorModel cm = pg. getColorModel ();
Return cm. hasAlpha ();
}
Public static BufferedImage toBufferedImage (Image image ){
If (image instanceof BufferedImage ){
Return (BufferedImage) image;
}
// This code ensures that all the pixels in the image are loaded
Image = new ImageIcon (image). getImage ();
// Determine if the image has transparent pixels; for this method's
// Implementation, see e661 Determining If an Image Has Transparent
// Pixels
Boolean hasAlpha = hasAlpha (image );
// Create a buffered image with a format that's compatible with
// Screen
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment
. GetLocalGraphicsEnvironment ();
Try {
// Determine the type of transparency of the new buffered image
Int transparency = Transparency. OPAQUE;
If (hasAlpha ){
Transparency = Transparency. BITMASK;
}
// Create the buffered image
GraphicsDevice gs = ge. getdefascrescreendevice ();
GraphicsConfiguration gc = gs. getDefaultConfiguration ();
Bimage = gc. createCompatibleImage (image. getWidth (null), image
. GetHeight (null), transparency );
} Catch (HeadlessException e ){
// The system does not have a screen
System. err. println ("The system does not have a screen! ");
System. exit (-1 );
}
If (bimage = null ){
// Create a buffered image using the default color model
Int type = BufferedImage. TYPE_INT_RGB;
If (hasAlpha ){
Type = BufferedImage. TYPE_INT_ARGB;
}
Bimage = new BufferedImage (image. getWidth (null), image
. GetHeight (null), type );
}
// Copy image to buffered image
Graphics g = bimage. createGraphics ();
// Paint the image onto the buffered image
G. drawImage (image, 0, 0, null );
G. dispose ();
Return bimage;
}
Private MediaLocator autoDetect () {// Automatic Identification Function
MediaLocator ml = null; // MediaLocator corresponding to the video collection device
VideoFormat currentFormat = null; // custom formats supported by Video Capture Devices
Format setFormat = null; // custom video capture device output Format
Format [] videoFormats = null; // All formats supported by Video Capture Devices
System. out. println ("AutoDetect for VFW"); // VFW: Microsoft Video for Windows
// Obtain the list of all current devices
Vector deviceList = CaptureDeviceManager. getDeviceList (null );
CaptureDeviceInfo device = CaptureDeviceManager. getDevice (url );
If (deviceList! = Null ){
// Find the available device name based on the device list
For (int I = 0; I <deviceList. size (); I ++ ){
Try {
CaptureDeviceInfo di = (CaptureDeviceInfo) deviceList
. ElementAt (I );
// If the device name starts with vfw
If (di. getName (). startsWith ("vfw :")){
// Obtain all supported RGB formats
VideoFormats = di. getFormats ();
For (int j = 0; j <videoFormats. length; j ++ ){
// We only need the first RGB format
If (videoFormats [j] instanceof RGBFormat ){
CurrentFormat = (RGBFormat) videoFormats [I];
Break;
}
}
If (currentFormat = null ){
System. err. println ("Search For RGBFormat Failed ");
System. exit (-1 );
}
// Obtain MediaLocator through the device. This is very important.
Ml = di. getLocator ();
}
} Catch (Exception npe ){
System. err. println ("Unable to get Processor for device ");
System. exit (-1 );
}
}
} Else {
System. err. println ("No Capture Device OK ");
System. exit (-1 );
}
MediaLocator = ml;
Return ml; // return the available device medialocator
}
Public static void main (String [] args) throws NoPlayerException,
IOException {
JMFDemo demo = new JMFDemo ();
Demo. setSize (100,100 );
Demo. autoDetect ();
Demo. init ();
Demo. play ();
Demo. setVisible (true );
}
}