We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
painting on jmf-captured video (Read 1452 times)
painting on jmf-captured video
Nov 28th, 2005, 8:38pm
 
Hello,

I'm trying to add some drawings to a captured webcam-source.
Since I need a resolution of at least 640x480 I decided to use JMF.
Capturing works fine for JMF. You get the video-picture either via "player.getVisualComponent()" which is really fast or via a framegrabber "(FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl")
... fgc.grabFrame() " which is a bit slower, but you get access to a buffered image.

Do you have any suggestions how you can paint with processing's drawing commands onto either the component or the grabbed buffer?


Here is the source code for jmf-video-capturing:

import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import javax.media.*;
import javax.media.control.*;
import javax.media.format.RGBFormat;
import javax.media.protocol.*;
import javax.media.util.BufferToImage;
import processing.core.PApplet;


public class VideoCaptureApplet extends PApplet implements ControllerListener
{
   CaptureDeviceInfo captureDevice = null;
   DataSource src=null;
   Player player = null;
   Processor processor;
   PushBufferStream[] strms;
   Buffer b;
   RGBFormat rgbf;
   BufferToImage conv;
   Image image;
   FrameGrabbingControl fgc;
   FormatControl[] fmtc;

   
   public void initVideo()
   {
       captureDevice = CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture (Win32):0");
       Format[] cfmts = captureDevice.getFormats();
       RGBFormat fmt = null;
       fmt = (RGBFormat)cfmts[5];
       MediaLocator loc = captureDevice.getLocator();

       try{
           src = Manager.createDataSource(loc);
       } catch (Exception e) {System.out.println("fehler bei datasource");}

       fmtc = ((CaptureDevice)src).getFormatControls();
       for (int i = 0; i < fmtc.length; i++) {
           if (fmtc[i].setFormat(fmt) != null)
           break;
       }
       
       try {
           processor = Manager.createProcessor(src);
       } catch (Exception e) {System.out.println("fehler bei processor-realisierug");}    
       processor.realize();
       
       // Must wait here for RealizeCompleteEvent
       try{
           Thread.sleep(1000);
       }catch(Exception e){}
       
       processor.start();

       PushBufferDataSource pbSrc = (PushBufferDataSource)processor.getDataOutput();

       strms = pbSrc.getStreams();
       
       PushBufferStream camStream = strms[0];
       rgbf = (RGBFormat)camStream.getFormat();

       if (captureDevice != null)
       {    
           try {
               player = Manager.createRealizedPlayer(src);
               player.addControllerListener(this);
           } catch (Exception e) {}
       }
   }

   public void setup()
   {
       size(640,480);
       initVideo();
       
       if (player != null)
       {
           if (player.getState() == Player.Realized)
           {
               //add(player.getVisualComponent());
               fgc = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");
               player.start();
           }
       }
   }
   
   public void draw()    {}
   
   public void paint(Graphics g)
   {
       if (player != null && player.getState()==Player.Started)
       {
           b = fgc.grabFrame();
           conv = new BufferToImage(rgbf);
           image = conv.createImage(b);
           g.drawImage(image,0,0,null);
           
           g.drawLine(20,20,100,200);
       }
   }
   
   public void stop()
   {
       player.stop();
       player.deallocate();
       processor.stop();
       processor.deallocate();
       try {src.stop();} catch(IOException e){System.out.println("failure stopping src");}
       src.disconnect();
       src     = null;
       fgc     = null;
       fmtc     = null;
       strms     = null;
   }

   public synchronized void controllerUpdate(ControllerEvent event) {}
}



I would greatly appreciate any help,
Thanks,
.sense
Page Index Toggle Pages: 1