Since so many people ask for this code, but never get it, I figured I'd repost it. I didn't write it, but I added the jpeg saving part, and modified it for my device, which you will have to do to get it to work. Just go into JMFRegistry and get the device name from your webcam and then edit the code. Also, keep in mind that some webcams don't work with JMF. You have to have a webcam that supports VFW or WDM interface.
And here's the code:
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.util.*;
import javax.media.control.*;
import javax.media.protocol.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import com.sun.image.codec.jpeg.*;
publicclass SwingCapture extends Panel implements ActionListener
{
publicstatic Player player = null;
public CaptureDeviceInfo di = null;
public MediaLocator ml = null;
public JButton capture = null;
public Buffer buf = null;
public Image img = null;
public VideoFormat vf = null;
public BufferToImage btoi = null;
public ImagePanel imgpanel = null;
public SwingCapture()
{
setLayout(new BorderLayout());
setSize(320,550);
imgpanel = new ImagePanel();
capture = new JButton("Capture");
capture.addActionListener(this);
String str1 = "vfw:Logitech USB Video Camera:0";
String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
di = CaptureDeviceManager.getDevice(str2);
ml = di.getLocator();
try
{
player = Manager.createRealizedPlayer(ml);
player.start();
Component comp;
if ((comp = player.getVisualComponent()) != null)
{
add(comp,BorderLayout.NORTH);
}
add(capture,BorderLayout.CENTER);
add(imgpanel,BorderLayout.SOUTH);
}
catch (Exception e)
{
e.printStackTrace();
}
}
publicstaticvoid main(String[] args)
{
Frame f = new Frame("SwingCapture");
SwingCapture cf = new SwingCapture();
f.addWindowListener(new WindowAdapter() {
publicvoid windowClosing(WindowEvent e) {
playerclose();
System.exit(0);}});
f.add("Center",cf);
f.pack();
f.setSize(new Dimension(320,550));
f.setVisible(true);
}
publicstaticvoid playerclose()
{
player.close();
player.deallocate();
}
publicvoid actionPerformed(ActionEvent e)
{
JComponent c = (JComponent) e.getSource();
if (c == capture)
{
// Grab a frame
FrameGrabbingControl fgc = (FrameGrabbingControl)
player.getControl("javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame();
// Convert it to an image
btoi = new BufferToImage((VideoFormat)buf.getFormat());
img = btoi.createImage(buf);
// show the image
imgpanel.setImage(img);
// save image
saveJPG(img,"c:\\test.jpg");
}
}
class ImagePanel extends Panel
{
public Image myimg = null;
public ImagePanel()
{
setLayout(null);
setSize(320,240);
}
publicvoid setImage(Image img)
{
this.myimg = img;
repaint();
}
publicvoid paint(Graphics g)
{
if (myimg != null)
{
g.drawImage(myimg, 0, 0, this);
}
}
}
publicstaticvoid saveJPG(Image img, String s)
{
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(img, null, null);
FileOutputStream out = null;
try
{
out = new FileOutputStream(s);
}
catch (java.io.FileNotFoundException io)
{
System.out.println("File Not Found");
}
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(0.5f,false);
encoder.setJPEGEncodeParam(param);
try
{
encoder.encode(bi);
out.close();
}
catch (java.io.IOException io)
{
System.out.println("IOException");
}
}
}
Thanks, nice code, but doesn't work with str1, that is my device name from JMFRegistry. NullPointerException on line: ml = di.getLocator(); Something is missing.
Re: Here is the source code to JMF Webcam app + saves jpeg
May 7, 2002 6:37 PM
(reply 2
of 52) (In reply to
#1 )
...So, instead of ml = di.getLocator(); use rather ml = new MediaLocator("vfw://0"); where the argument is from the Location field of the JMFRegistry for the given device. Now it works.
when i run this source code, no image is show on the window.
java ide shows cannot connect to capture devices. what should i do? i already install jmf2.1.1a.
Have a question. I have a very similar application that does a screen capture from a webcam (Panasonic Mini DV) using JMF. The program works well with regular webcams. But when I use this particular miniDV webcam I am getting the video in a greenish shade. Can you please suggest as to what I can do to get around this problem ?
I have also the same project but i want it works in an applet and without JMF installed on client side.
How could i detect the webcam without the JMFRegistry (which get the device name from the webcam)?
Is it possible to do it ? (i have thought about signed applet to solve my problem)
Re: Here is the source code to JMF Webcam app + saves jpeg
Jul 24, 2002 7:01 PM
(reply 7
of 52) (In reply to
#6 )
hai, christelou
i also develop same task like u, but i also facing the same problem.can we exchange information if we have fing the solutions.
good luck.
So the player returns always null. The other thigns seem to be fine. I can view the image in the application but when I click capture it throws a Null Pointer Exception because fgc is null.
I am using Logitech QuickCam, USB, Win2k, jdk 1.3.1, JMF 2.1.1
Can you help me?
thanx,
Philip
Since so many people ask for this code, but never get
it, I figured I'd repost it. I didn't write it, but I
added the jpeg saving part, and modified it for my
device, which you will have to do to get it to work.
Just go into JMFRegistry and get the device name from
your webcam and then edit the code. Also, keep in mind
that some webcams don't work with JMF. You have to
have a webcam that supports VFW or WDM interface.
And here's the code:
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.util.*;
import javax.media.control.*;
import javax.media.protocol.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import com.sun.image.codec.jpeg.*;
publicclass SwingCapture extends Panel implements
ActionListener
{
publicstatic Player player = null;
public CaptureDeviceInfo di = null;
public MediaLocator ml = null;
public JButton capture = null;
public Buffer buf = null;
public Image img = null;
public VideoFormat vf = null;
public BufferToImage btoi = null;
public ImagePanel imgpanel = null;
public SwingCapture()
{
setLayout(new BorderLayout());
setSize(320,550);
imgpanel = new ImagePanel();
capture = new JButton("Capture");
capture.addActionListener(this);
String str1 = "vfw:Logitech USB Video Camera:0";
String str2 = "vfw:Microsoft WDM Image Capture
ure (Win32):0";
di = CaptureDeviceManager.getDevice(str2);
ml = di.getLocator();
try
{
player = Manager.createRealizedPlayer(ml);
player.start();
Component comp;
if ((comp = player.getVisualComponent()) !=
)) != null)
{
add(comp,BorderLayout.NORTH);
}
add(capture,BorderLayout.CENTER);
add(imgpanel,BorderLayout.SOUTH);
}
catch (Exception e)
{
e.printStackTrace();
}
}
publicstaticvoid main(String[] args)
{
Frame f = new Frame("SwingCapture");
SwingCapture cf = new SwingCapture();
f.addWindowListener(new WindowAdapter() {
publicvoid windowClosing(WindowEvent e) {
playerclose();
System.exit(0);}});
f.add("Center",cf);
f.pack();
f.setSize(new Dimension(320,550));
f.setVisible(true);
}
publicstaticvoid playerclose()
{
player.close();
player.deallocate();
}
publicvoid actionPerformed(ActionEvent e)
{
JComponent c = (JComponent) e.getSource();
if (c == capture)
{
// Grab a frame
FrameGrabbingControl fgc =
fgc = (FrameGrabbingControl)
player.getControl("javax.media.control.FrameGrabbingCo
trol");
buf = fgc.grabFrame();
// Convert it to an image
btoi = new
= new BufferToImage((VideoFormat)buf.getFormat());
img = btoi.createImage(buf);
// show the image
imgpanel.setImage(img);
// save image
saveJPG(img,"c:\\test.jpg");
}
}
class ImagePanel extends Panel
{
public Image myimg = null;
public ImagePanel()
{
setLayout(null);
setSize(320,240);
}
publicvoid setImage(Image img)
{
this.myimg = img;
repaint();
}
publicvoid paint(Graphics g)
{
if (myimg != null)
{
g.drawImage(myimg, 0, 0, this);
}
}
}
publicstaticvoid saveJPG(Image img, String s)
{
BufferedImage bi = newnew BufferedImage(img.getWidth(null),
img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(img, null, null);
FileOutputStream out = null;
try
{
out = new FileOutputStream(s);
}
catch (java.io.FileNotFoundException io)
{
System.out.println("File Not Found");
}
JPEGImageEncoder encoder =
r = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param =
m = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(0.5f,false);
encoder.setJPEGEncodeParam(param);
try
{
encoder.encode(bi);
out.close();
}
catch (java.io.IOException io)
{
System.out.println("IOException");
}
}
}
Thanks for the code.
I can run it, the window comes up but as soon as the window comes up I'm getting the following:
java.lang.UnsatisfiedLinkError: getWindowHandle
at com.sun.media.util.WindowUtil.getWindowHandle(Native Method)
at com.sun.media.util.WindowUtil.getWindowHandle(WindowUtil.java:63)
at com.sun.media.renderer.video.GDIRenderer.doProcess(GDIRenderer.java:255)
at com.sun.media.renderer.video.GDIRenderer.doProcess(GDIRenderer.java:248)
at com.sun.media.renderer.video.BasicVideoRenderer.process(BasicVideoRenderer.java:118)
at com.sun.media.BasicRendererModule.processBuffer(BasicRendererModule.java:727)
at com.sun.media.BasicRendererModule.scheduleBuffer(BasicRendererModule.java:499)
at com.sun.media.BasicRendererModule.doProcess(BasicRendererModule.java:400)
at com.sun.media.RenderThread.process(BasicRendererModule.java:1114)
at com.sun.media.util.LoopThread.run(LoopThread.java:135)
I'm fairly new to JMF. Has anyone had the same problem?
Help is very much appreciated.
Re: Here is the source code to JMF Webcam app + saves jpeg
Apr 15, 2003 6:06 AM
(reply 10
of 52) (In reply to
#9 )
Thanks for the source code. I got a lot of information from this forum. Now, I want to share with you some of my findings.
I have tried to modify the code to a run once program for just capture the photo only without trigger on the panel. At first, I always got null image. But after some investigation. I found that if I insert some delay right after player.start() like:
Re: Here is the source code to JMF Webcam app + saves jpeg
May 9, 2003 11:30 AM
(reply 11
of 52) (In reply to
#2 )
...So, instead of ml = di.getLocator(); use rather ml
= new MediaLocator("vfw://0"); where the argument is
from the Location field of the JMFRegistry for the
given device. Now it works.
When I compile it, it always tells me the error:
SwingCapture.java:43: incompatible types
found : javax.media.Player
required: Player
player = Manager.createRealizedPlayer(ml);
Even though I tried your suggestion, I still get the same error.