i have a code for capturing device (the webcam)and take snapshot from camera but it take snapshot from desktop not just a camera region.
the code is:
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.media.renderer.video.AWTRenderer;
/**
*
* @author mohamed khalaf
*/import javax.media.*;
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import javax.swing.event.*;
import javax.swing.filechooser.FileFilter;
import java.io.File;
import java.io.FileOutputStream;
public class JMFtest extends JFrame {
boolean saved=false;
File file=null;
public Container c=getContentPane();
public Player _player=null;
public Image img=null;
public Buffer buffer=null;
public BufferToImage btoi=null;
public ImagePanel imagepanel=null;
class ImagePanel extends JFrame{
public Image myimg;
public ImagePanel() {
setLayout(new FlowLayout());
setSize(300,300);
setVisible(true);
}
public void setmage(Image img){
this.myimg=img;
repaint();
}
public void paint(Graphics g){
if(myimg!=null){
g.drawImage(myimg,0,0, this);
}
}
}
JMFtest() {
addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
_player.stop();
_player.deallocate();
_player.close();
System.exit( 0 );
}
});
JPanel panel = (JPanel)getContentPane();
panel.setLayout( new FlowLayout() );
JButton b=new JButton("Start");
b.setMnemonic('S');
b.setCursor(new Cursor(Cursor.HAND_CURSOR));
panel.add(b);
JButton s=new JButton("Stop");
s.setMnemonic('S');
s.setCursor(new Cursor(Cursor.WAIT_CURSOR));
panel.add(s);
JButton c=new JButton("Take photo");
c.setMnemonic('T');
c.setCursor(new Cursor(Cursor.HAND_CURSOR));
panel.add(c);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
_player.start();
}
});
s.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
_player.stop();
}
});
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String mlcoation="vfw:Micrsoft WDM Image Capture (Win32):0";
FrameGrabbingControl fgc = (FrameGrabbingControl)_player.getControl("javax.media.control.FrameGrabbingControl");
buffer=fgc.grabFrame();
btoi=new BufferToImage((VideoFormat)buffer.getFormat());
img=btoi.createImage(buffer);
ImagePanel imagepanel=new ImagePanel();
}
});
String mediaFile = "vfw:Micrsoft WDM Image Capture (Win32):0";
try {
MediaLocator mlr = new MediaLocator( mediaFile );
_player =Manager.createRealizedPlayer(mlr);
if (_player.getVisualComponent() != null){
setSize(200,200);
panel.add("South", _player.getVisualComponent());
}
}
catch (Exception e) {
System.err.println( "Got exception " + e );
}
}
public static void main(String[] args) {
JMFtest jmfTest = new JMFtest();
jmfTest.setSize(800,800);
jmfTest.show();
}
}
the code is:
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.media.renderer.video.AWTRenderer;
/**
*
* @author mohamed khalaf
*/import javax.media.*;
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import javax.swing.event.*;
import javax.swing.filechooser.FileFilter;
import java.io.File;
import java.io.FileOutputStream;
public class JMFtest extends JFrame {
boolean saved=false;
File file=null;
public Container c=getContentPane();
public Player _player=null;
public Image img=null;
public Buffer buffer=null;
public BufferToImage btoi=null;
public ImagePanel imagepanel=null;
class ImagePanel extends JFrame{
public Image myimg;
public ImagePanel() {
setLayout(new FlowLayout());
setSize(300,300);
setVisible(true);
}
public void setmage(Image img){
this.myimg=img;
repaint();
}
public void paint(Graphics g){
if(myimg!=null){
g.drawImage(myimg,0,0, this);
}
}
}
JMFtest() {
addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
_player.stop();
_player.deallocate();
_player.close();
System.exit( 0 );
}
});
JPanel panel = (JPanel)getContentPane();
panel.setLayout( new FlowLayout() );
JButton b=new JButton("Start");
b.setMnemonic('S');
b.setCursor(new Cursor(Cursor.HAND_CURSOR));
panel.add(b);
JButton s=new JButton("Stop");
s.setMnemonic('S');
s.setCursor(new Cursor(Cursor.WAIT_CURSOR));
panel.add(s);
JButton c=new JButton("Take photo");
c.setMnemonic('T');
c.setCursor(new Cursor(Cursor.HAND_CURSOR));
panel.add(c);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
_player.start();
}
});
s.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
_player.stop();
}
});
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String mlcoation="vfw:Micrsoft WDM Image Capture (Win32):0";
FrameGrabbingControl fgc = (FrameGrabbingControl)_player.getControl("javax.media.control.FrameGrabbingControl");
buffer=fgc.grabFrame();
btoi=new BufferToImage((VideoFormat)buffer.getFormat());
img=btoi.createImage(buffer);
ImagePanel imagepanel=new ImagePanel();
}
});
String mediaFile = "vfw:Micrsoft WDM Image Capture (Win32):0";
try {
MediaLocator mlr = new MediaLocator( mediaFile );
_player =Manager.createRealizedPlayer(mlr);
if (_player.getVisualComponent() != null){
setSize(200,200);
panel.add("South", _player.getVisualComponent());
}
}
catch (Exception e) {
System.err.println( "Got exception " + e );
}
}
public static void main(String[] args) {
JMFtest jmfTest = new JMFtest();
jmfTest.setSize(800,800);
jmfTest.show();
}
}