participate


Java Media Framework - Here is the source code to JMF Webcam app + saves jpeg [Locked]
This topic is locked
<<   Back to Forum  |   Give us Feedback
This topic has 52 replies on 4 pages.    1 | 2 | 3 | 4 | Next »
wbracken
Posts:1,381
Registered: 19/02/00
Here is the source code to JMF Webcam app + saves jpeg   
Apr 23, 2002 11:21 AM

 
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.*;
 
public class SwingCapture extends Panel implements ActionListener 
{
  public static 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();
    }
  }
 
 
  
  public static void main(String[] args) 
  {
    Frame f = new Frame("SwingCapture");
    SwingCapture cf = new SwingCapture();
    
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
      playerclose();
      System.exit(0);}});
    
    f.add("Center",cf);
    f.pack();
    f.setSize(new Dimension(320,550));
    f.setVisible(true);
  }
  
  
  public static void playerclose() 
  {
    player.close();
    player.deallocate();
  }
  
 
  public void 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);
    }
    
    public void setImage(Image img) 
    {
      this.myimg = img;
      repaint();
    }
    
    public void paint(Graphics g) 
    {
      if (myimg != null) 
      {
        g.drawImage(myimg, 0, 0, this);
      }
    }
  }
  
 
  public static void 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"); 
    }
  }
  
}


 
szlizs
Posts:9
Registered: 4/28/97
Re: Here is the source code to JMF Webcam app + saves jpeg   
May 7, 2002 6:02 PM (reply 1 of 52)  (In reply to original post )

 
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.

Robert Szlizs
 
szlizs
Posts:9
Registered: 4/28/97
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.

Robert Szlizs
 
jackielee1234
Posts:1
Registered: 5/5/01
Re: Here is the source code to JMF Webcam app + saves jpeg   
Jul 10, 2002 5:55 PM (reply 3 of 52)  (In reply to #2 )

 
I am doing a similar project in school. Thanks for the source code. It helps me a lot.

I also encountered the same runtime problem. But with the suggested solution, the program finally became runnable. Many Thanks too!

I am wondering how to make the program become an executable file or run it as an applet? Any idea!
 
yappi78
Posts:5
Registered: 11/30/01
Re: Here is the source code to JMF Webcam app + saves jpeg   
Jul 18, 2002 10:16 PM (reply 4 of 52)  (In reply to original post )

 
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.
 
raamki
Posts:1
Registered: 4/8/99
Re: Here is the source code to JMF Webcam app + saves jpeg   
Jul 22, 2002 12:31 PM (reply 5 of 52)  (In reply to original post )

 
Hi Bracken

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 ?

Thanks
Ramki.
 
christelou
Posts:50
Registered: 3/5/02
Re: Here is the source code to JMF Webcam app + saves jpeg   
Jul 24, 2002 3:15 AM (reply 6 of 52)  (In reply to original post )

 
hi all,

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)

Please help me ! :-D
Thanks in advance.
Christel
 
yappi78
Posts:5
Registered: 11/30/01
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.
 
phiko
Posts:5
Registered: 9/30/02
Re: Here is the source code to JMF Webcam app + saves jpeg   
Sep 30, 2002 10:08 AM (reply 8 of 52)  (In reply to original post )

 
Hi William,
I've tried this code but I always get an null fgc with the following line.

FrameGrabbingControl fgc = (FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingCotrol");


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.*;
 
public class SwingCapture extends Panel implements
ActionListener 
{
  public static 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();
    }
  }
 
 
  
  public static void main(String[] args) 
  {
    Frame f = new Frame("SwingCapture");
    SwingCapture cf = new SwingCapture();
    
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
      playerclose();
      System.exit(0);}});
    
    f.add("Center",cf);
    f.pack();
    f.setSize(new Dimension(320,550));
    f.setVisible(true);
  }
  
  
  public static void playerclose() 
  {
    player.close();
    player.deallocate();
  }
  
 
  public void 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);
    }
    
    public void setImage(Image img) 
    {
      this.myimg = img;
      repaint();
    }
    
    public void paint(Graphics g) 
    {
      if (myimg != null) 
      {
        g.drawImage(myimg, 0, 0, this);
      }
    }
  }
  
 
  public static void saveJPG(Image img, String s)
  {
BufferedImage bi = new
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 =
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"); 
    }
  }
  
}



 
mpedrats
Posts:4
Registered: 3/10/00
Re: Here is the source code to JMF Webcam app + saves jpeg   
Oct 4, 2002 4:07 PM (reply 9 of 52)  (In reply to original post )

 
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.

Thanks
Markus
 
erikcheng
Posts:1
Registered: 11/10/00
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:

player.start();
try {
Thread.sleep(1000);
} catch (Exception e) {
}

then I can get the image without problem!!
I think the player need some time to initialize and get the first frame.

Hope this is useful to you.
 
davidsk2003
Posts:168
Registered: 1/10/03
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.
 
jaykay3
Posts:1
Registered: 3/1/03
Re: Here is the source code to JMF Webcam app + saves jpeg   
May 17, 2003 10:19 AM (reply 12 of 52)  (In reply to original post )

 
is there any way to change the output size in your code?
Thanks for your posting...
 
davidsk2003
Posts:168
Registered: 1/10/03
Re: Here is the source code to JMF Webcam app + saves jpeg   
May 19, 2003 6:17 AM (reply 13 of 52)  (In reply to #11 )

 
Anyone knows what's wrong with my VRM?

Thanks

David

...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.
 
peraltas
Posts:1
Registered: 5/29/03
Re: Here is the source code to JMF Webcam app + saves jpeg   
May 29, 2003 4:09 AM (reply 14 of 52)  (In reply to original post )

 
can your code be transported to an applet? i have tried it but it seems that the saving part does not work on applet... i need help please...
 
This topic has 52 replies on 4 pages.    1 | 2 | 3 | 4 | Next »
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 28
  • Guests : 129

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums