JMF - Captura de Video

Olá pessoal,

Já procurei, mas nao encontrei nenhum exemplo ou tutorial de como capturar, num applet, as imagens de minha web cam… alguem tem alguma luz???

Obrigado… :lol:

Allan.

ve ae…

import java.awt.Dimension;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Properties;
import javax.media.*;
import javax.media.control.FormatControl;
import javax.media.format.RGBFormat;
import javax.media.protocol.*;
import javax.media.util.BufferToImage;

/**
 * Faz captura de imagens em webcams. Adaptada por Diego R. Drumond.
 *
 * @author S.Ritter
 * @version 1.0 24/01/2002
 */
public class CapturaImagem extends Thread implements ControllerListener
{
	/*  */
    private Properties videoProperties;
    /*  */
    private Object stateLock;
    /*  */
    private Object runLock;
    /*  */
    private Processor deviceProc;
    /*  */
    private PushBufferStream camStream;
    /*  */
    private PushBufferDataSource source;
    /*  */
    private BufferToImage converter;
    /*  */
    private Image currentImage;
    /*  */
    private boolean threadRunning;
	/**
	 * 
	 */
    public CapturaImagem( ) throws CapturaImagemException
    {
        stateLock     = new Object( );
        runLock       = new Object( );
        deviceProc    = null;
        source        = null;
        threadRunning = false;
        String videoPropFile = System.getProperty( "video.properties", "video.properties" );
        setup( videoPropFile );
    }
	/**
	 * 
	 */
    public CapturaImagem( String videoPropFile ) throws CapturaImagemException
    {
        stateLock     = new Object( );
        runLock       = new Object( );
        deviceProc    = null;
        source        = null;
        threadRunning = false;
        setup( videoPropFile );
    }
	/**
	 * 
	 */
    private void setup( String videoPropFile ) throws CapturaImagemException
    {
        videoProperties = new Properties( );
        if( videoPropFile != null )
        {
            try
            {
                FileInputStream fis = new FileInputStream( new File( videoPropFile ) );
                videoProperties.load( fis );
            }
            catch( IOException ioe )
            {
                System.out.println( "Unable to access video properties" );
                System.out.println( ioe.getMessage( ) );
            }
        }
        Dimension viewSize  = null;
        int viewDepth       = 0;
        String cameraDevice = videoProperties.getProperty( "device-name", "vfw:Logitech USB Video Camera:0" );
        try
        {
            String pValue = videoProperties.getProperty( "resolution-x", "160" );
            int xRes      = Integer.parseInt( pValue );
            pValue        = videoProperties.getProperty( "resolution-y", "120" );
            int yRes      = Integer.parseInt( pValue );
            viewSize      = new Dimension( xRes, yRes );
            pValue        = videoProperties.getProperty( "colour-depth", "24" );
            viewDepth     = Integer.parseInt( pValue );
        }
        catch( NumberFormatException nfe )
        {
            System.out.println( "Bad numeric value in video properties file" );
            System.exit( 1 );
        }
        CaptureDeviceInfo device = CaptureDeviceManager.getDevice( cameraDevice );
        if( device == null )
        {
            throw new CapturaImagemException( "No device found [ " + cameraDevice + "]" );
        }
        RGBFormat userFormat       = null;
        javax.media.Format cfmt[ ] = device.getFormats( );
        for( int i = 0; i < cfmt.length; i++ )
        {
            if( !( cfmt[ i ] instanceof RGBFormat ) )
                continue;

            userFormat       = ( RGBFormat )cfmt[ i ];
            Dimension d      = userFormat.getSize( );
            int bitsPerPixel = userFormat.getBitsPerPixel( );
            if( viewSize.equals( d ) && bitsPerPixel == viewDepth )
                break;

            userFormat = null;
        }

        if( userFormat == null )
        {
            throw new CapturaImagemException( "Requested format not supported" );
        }
        javax.media.MediaLocator loc = device.getLocator( );
        if( loc == null )
        {
            throw new CapturaImagemException( "Unable to get MediaLocator for device" );
        }
        DataSource formattedSource = null;
        try
        {
            formattedSource = Manager.createDataSource( loc );
        }
        catch( IOException ioe )
        {
            throw new CapturaImagemException( "IO Error creating dataSource" );
        }
        catch( NoDataSourceException ndse )
        {
            throw new CapturaImagemException( "Unable to create dataSource" );
        }
        if( !( formattedSource instanceof CaptureDevice ) )
        {
            throw new CapturaImagemException( "DataSource not a CaptureDevice" );
        }
        FormatControl fmtControls[ ] = ( ( CaptureDevice )formattedSource ).getFormatControls( );
        if( fmtControls == null || fmtControls.length == 0 )
        {
            throw new CapturaImagemException( "No FormatControl available" );
        }
        javax.media.Format setFormat = null;
        for( int i = 0; i < fmtControls.length; i++ )
        {
            if( fmtControls[ i ] != null && ( setFormat = fmtControls[ i ].setFormat( userFormat ) ) != null )
                break;
        }

        if( setFormat == null )
        {
            throw new CapturaImagemException( "Failed to set camera format" );
        }
        try
        {
            formattedSource.connect( );
        }
        catch( IOException ioe )
        {
            throw new CapturaImagemException( "Unable to connect to DataSource" );
        }
        try
        {
            deviceProc = Manager.createProcessor( formattedSource );
        }
        catch( IOException ioe )
        {
            throw new CapturaImagemException( "Unable to get Processor for device: " + ioe.getMessage( ) );
        }
        catch( NoProcessorException npe )
        {
            throw new CapturaImagemException( "Unable to get Processor for device: " + npe.getMessage( )  );
        }
        deviceProc.addControllerListener( this );
        deviceProc.realize( );
        while( deviceProc.getState( ) != 300 ) 
        {
            synchronized( stateLock )
            {
                try
                {
                    stateLock.wait( );
                }
                catch( InterruptedException ie )
                {
                    throw new CapturaImagemException( "Failed to get to realized state" );
                }
            }
        }
        deviceProc.start( );
        try
        {
            source = ( PushBufferDataSource )deviceProc.getDataOutput( );
        }
        catch( NotRealizedError nre )
        {
            throw new CapturaImagemException( "Processor not realized" );
        }
        PushBufferStream streams[ ] = source.getStreams( );
        camStream                   = null;
        for( int i = 0; i < streams.length; i++ )
        {
            if( !( streams[ i ].getFormat( ) instanceof RGBFormat ) )
            {
                continue;
            }
            camStream      = streams[ i ];
            RGBFormat rgbf = ( RGBFormat )streams[ i ].getFormat( );
            converter      = new BufferToImage( rgbf );
            break;
        }
        System.out.println( "Capture device ready" );
    }
	/**
	 * 
	 */
    public Image getImage( )
    {
        while( !threadRunning ) 
        {
            synchronized( runLock )
            {
                try
                {
                    runLock.wait( );
                }
                catch( InterruptedException ie ) { }
            }
        }
        return accessInternalImage( null );
    }
	/**
	 * 
	 */
    public BufferedImage getBufferedImage( )
    {
        return ( BufferedImage )getImage( );
    }
	/**
	 * 
	 */
    public void run( )
    {
        System.out.println( "Capture thread starting..." );
        Buffer b = new Buffer( );
        do
        {
            do
            {
                try
                {
                    camStream.read( b );
                }
                catch( Exception e ) { }
                Image i = converter.createImage( b );
                accessInternalImage( i );
            }
            while( threadRunning );

            threadRunning = true;

            synchronized( runLock )
            {
                runLock.notifyAll( );
            }
        }
        while( true );
    }
	/**
	 * 
	 */
    public void controllerUpdate( ControllerEvent ce )
    {
        if( ce instanceof RealizeCompleteEvent )
        {
            synchronized( stateLock )
            {
                stateLock.notifyAll( );
            }
        }
    }
	/**
	 * 
	 */
    private synchronized Image accessInternalImage( Image image )
    {
        if( image == null )
        {
            return currentImage;
        }
        else
        {
            currentImage = image;
            return null;
        }
    }
}

video.properties

device-name=vfw:Microsoft WDM Image Capture (Win32):1
resolution-x=320
resolution-y=240
colour-depth=24