Sep 2, 2012

Camera Application Using JMF

Java camera JMF
This a simple Java program that opens the system Webcam and presents the visual data of the camera. It used Java Media Framework library, though has not been updated for a very long, but developers still found it very useful for capture, playback, stream, and transcoding multiple media formats. You need to integrate the JMF plugin into your project in order to use the JMF APIs. You can download the JMF library here at Softpedia. 
package codecypherprojects;

/**
 *
 * @author khamis
 */ 

import java.awt.BorderLayout;
import javax.media.*;
import javax.media.protocol.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import javax.media.control.FormatControl;
import javax.media.format.VideoFormat;
import java.io.*;

public class WebCamApp { 
    
    Player web_cam_player;
    
    public static void main(String[] args){
    WebCamApp ourApp = new WebCamApp();
    ourApp.getCameraApp();
   }

   public void getCameraApp(){
   try{

     /**
     * get the system webcam media, the protool "vfw://0" worked well with windows 7
     * but some times take a complete restart to reconnect to native camera source
     **/
     MediaLocator ml = new MediaLocator("vfw://0");
   
     // Create the data source  from which we will create our cam player
     DataSource dataSource = Manager.createDataSource(ml);
    
    
     //Now we create our camera data from the source & start the player
     web_cam_player = Manager.createRealizedPlayer(dataSource);
     web_cam_player.start();
    
     //Lets have a a second delay as we dinamically grab the frames
     Thread.currentThread().sleep(1000);
    
     //Build the webcam visual frame, set the size & release the window on close
     buildCameraFrame();
    
   }catch(Exception e){
       System.out.println("Unable to open the camera source. " + e.getMessage());
       }
   }

    private void buildCameraFrame() {
       JFrame cam_frame=new JFrame("Webcam");
       cam_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       //Do we have the media visual controls?
       if(web_cam_player.getVisualComponent()!=null){
        
       //Add the visual control to the component
       cam_frame.getContentPane().add(web_cam_player.getVisualComponent());
    }
    if(web_cam_player.getControlPanelComponent()!=null){
       cam_frame.getContentPane().add(web_cam_player
         .getControlPanelComponent(), BorderLayout.SOUTH);
       cam_frame.pack();
       cam_frame.setLocationRelativeTo(null);
       cam_frame.setVisible(true);
       cam_frame.setSize(320,240);
    }
}
This is the result of the captured program















Related Posts

Simple JMF Media Player

1 comment:

  1. Mucho mas facil serĂ­a de la siguiente forma(It would be a lot easier this way[for noobs who are just trying the basics with creating a component out of a webcam!)

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package testing;

    import java.awt.*;
    import javax.media.*;
    import javax.swing.JFrame;

    public class Testing extends Frame {

    /**
    * ***********************************************************************
    * MAIN PROGRAM / STATIC METHODS
    * ***********************************************************************
    */
    public static void main(String args[]) throws Exception {

    {
    // Create capture device
    CaptureDeviceInfo deviceInfo = CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture (Win32):0");

    Player player = Manager.createRealizedPlayer(deviceInfo.getLocator());
    player.start();

    // Wait a few seconds for camera to initialise (otherwise img==null)
    Thread.sleep(2500);

    // Creating Layout
    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout());
    frame.setVisible(true);

    //Creating Visual Component
    Component component = player.getVisualComponent();
    frame.add(component);

    // Stop using webcam
    // player.close();
    // player.deallocate();
    // System.exit(0);
    }
    }
    }

    ReplyDelete