JMF Example
  1 
  2 package jmfexample;
  3 
  4 import java.io.*;
  5 import java.util.*;
  6 import java.awt.Component;
  7 import javax.swing.JOptionPane;
  8 import javax.media.*;
  9 import javax.media.protocol.*;
 10 import javax.media.format.VideoFormat;
 11 
 12 public class camDataSource {
 13     
 14     private Component parent;
 15     
 16     private DataSource mainCamSource;
 17     private MediaLocator ml;
 18     private Processor processor;
 19     private boolean processing;
 20     
 21     public camDataSource(Component parent) {
 22         this.parent = parent;
 23         setProcessing(false);
 24     }
 25     
 26     public void setMainSource(){
 27         setProcessing(false);
 28         VideoFormat vidformat = new VideoFormat(VideoFormat.YUV);
 29         Vector devices = CaptureDeviceManager.getDeviceList(vidformat);
 30         CaptureDeviceInfo di = null;
 31         
 32         if (devices.size() > 0) di = (CaptureDeviceInfo) devices.elementAt(0);
 33         else {
 34             JOptionPane.showMessageDialog(parent, "Your camera is not connected", "No webcam found", JOptionPane.WARNING_MESSAGE);
 35             return;
 36         }
 37        
 38         try {
 39             ml = di.getLocator();
 40             setMainCamSource(Manager.createDataSource(ml));
 41         } catch (Exception e) {
 42             JOptionPane.showMessageDialog(parent, "Exception locating media: " + e.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
 43             return;
 44         }
 45     }
 46     
 47     public void makeDataSourceCloneable(){
 48         
 49         setMainCamSource(Manager.createCloneableDataSource(getMainCamSource())); // turn our data source to a cloneable data source
 50         
 51     }
 52     
 53     public void startProcessing(){
 54         
 55         try{
 56             processor = Manager.createProcessor(getMainCamSource());
 57         }catch (IOException e) {
 58             JOptionPane.showMessageDialog(parent, "IO Exception creating processor: " + e.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
 59             return;
 60         }catch (NoProcessorException e) {
 61             JOptionPane.showMessageDialog(parent, "Exception creating processor: " + e.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
 62             return;
 63         }
 64         
 65         camStateHelper playhelper = new camStateHelper(processor);
 66         if(!playhelper.configure(10000)){
 67             JOptionPane.showMessageDialog(parent, "cannot configure processor", "Error", JOptionPane.WARNING_MESSAGE);
 68             return;
 69         }
 70         
 71        
 72         processor.setContentDescriptor(null);
 73         
 74         if(!playhelper.realize(10000)){
 75             JOptionPane.showMessageDialog(parent, "cannot realize processor", "Error", JOptionPane.WARNING_MESSAGE);
 76             return;
 77         }
 78        
 79         
 80          
 81         processor.start(); // In order for or your clones to start, you must start the original source
 82        /* try{
 83             Thread.sleep(10000);
 84         }catch(java.lang.InterruptedException e){}
 85         
 86          Control control = processor.getControl("javax.media.control.FrameRateControl");
 87         if ( control != null && control instanceof javax.media.control.FrameRateControl ){
 88             ((javax.media.control.FrameRateControl)control).setFrameRate(0.2f);
 89         } else{
 90             System.out.println("no frame control");
 91         }*/
 92         
 93         setProcessing(true);
 94     }
 95     
 96     public DataSource cloneCamSource(){
 97         if(!getProcessing()) setMainSource();
 98         return ((SourceCloneable)getMainCamSource()).createClone();
 99                // return processor.getDataOutput();
100     }
101     
102     public DataSource getMainCamSource(){
103         return mainCamSource;
104     }
105     
106     public void setMainCamSource(DataSource mainCamSource){
107         this.mainCamSource = mainCamSource;
108     }
109     
110     public void setMl(MediaLocator ml){
111         this.ml = ml;
112     }
113     
114     public MediaLocator getMl(){
115         return ml;
116     }
117     
118     public boolean getProcessing(){
119         return processing;
120     }
121     
122     public void setProcessing(boolean processing){
123         this.processing = processing;
124         
125     }
126     
127     public void setParent(Component parent){
128         this.parent = parent;
129     }
130     
131     public Component getParent(){
132         return parent;
133     }
134 }
135 
136