Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: JMF Webcam app + saving jpeg  (Read 1625 times)
0 Members and 1 Guest are viewing this topic.
G. Ritardo
Member
*
*

Reputation: 0
Offline Offline
Posts: 53
Referrals: 0

Awards
« on: May 18, 2009, 03:48:17 PM »

Not written by me and forgot what website i found this (probably the sun site) but still wanted to share the code for those that want to use java in combination with a webcam + saving the image to jpeg. I haven't tested the code myself though because i don't have the JMF package installed but if you have any trouble running the code let me know and i'll try to help you out.

Code
GeSHi (java):
  1. import javax.swing.*;
  2. import javax.swing.event.*;
  3. import java.io.*;
  4. import javax.media.*;
  5. import javax.media.format.*;
  6. import javax.media.util.*;
  7. import javax.media.control.*;
  8. import javax.media.protocol.*;
  9. import java.util.*;
  10. import java.awt.*;
  11. import java.awt.image.*;
  12. import java.awt.event.*;
  13. import com.sun.image.codec.jpeg.*;
  14.  
  15. public class SwingCapture extends Panel implements ActionListener
  16. {
  17.  public static Player player = null;
  18.  public CaptureDeviceInfo di = null;
  19.  public MediaLocator ml = null;
  20.  public JButton capture = null;
  21.  public Buffer buf = null;
  22.  public Image img = null;
  23.  public VideoFormat vf = null;
  24.  public BufferToImage btoi = null;
  25.  public ImagePanel imgpanel = null;
  26.  
  27.  public SwingCapture()
  28.  {
  29.    setLayout(new BorderLayout());
  30.    setSize(320,550);
  31.  
  32.    imgpanel = new ImagePanel();
  33.    capture = new JButton("Capture");
  34.    capture.addActionListener(this);
  35.  
  36.    String str1 = "vfw:Logitech USB Video Camera:0";
  37.    String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
  38.    di = CaptureDeviceManager.getDevice(str2);
  39.    ml = di.getLocator();
  40.  
  41.    try
  42.    {
  43.      player = Manager.createRealizedPlayer(ml);
  44.      player.start();
  45.      Component comp;
  46.  
  47.      if ((comp = player.getVisualComponent()) != null)
  48.      {
  49.        add(comp,BorderLayout.NORTH);
  50.      }
  51.      add(capture,BorderLayout.CENTER);
  52.      add(imgpanel,BorderLayout.SOUTH);
  53.    }
  54.    catch (Exception e)
  55.    {
  56.      e.printStackTrace();
  57.    }
  58.  }
  59.  
  60.  
  61.  
  62.  public static void main(String[] args)
  63.  {
  64.    Frame f = new Frame("SwingCapture");
  65.    SwingCapture cf = new SwingCapture();
  66.  
  67.    f.addWindowListener(new WindowAdapter() {
  68.      public void windowClosing(WindowEvent e) {
  69.      playerclose();
  70.      System.exit(0);}});
  71.  
  72.    f.add("Center",cf);
  73.    f.pack();
  74.    f.setSize(new Dimension(320,550));
  75.    f.setVisible(true);
  76.  }
  77.  
  78.  
  79.  public static void playerclose()
  80.  {
  81.    player.close();
  82.    player.deallocate();
  83.  }
  84.  
  85.  
  86.  public void actionPerformed(ActionEvent e)
  87.  {
  88.    JComponent c = (JComponent) e.getSource();
  89.  
  90.    if (c == capture)
  91.    {
  92.      // Grab a frame
  93.      FrameGrabbingControl fgc = (FrameGrabbingControl)
  94.      player.getControl("javax.media.control.FrameGrabbingControl");
  95.      buf = fgc.grabFrame();
  96.  
  97.      // Convert it to an image
  98.      btoi = new BufferToImage((VideoFormat)buf.getFormat());
  99.      img = btoi.createImage(buf);
  100.  
  101.      // show the image
  102.      imgpanel.setImage(img);
  103.  
  104.      // save image
  105.      saveJPG(img,"c:\\test.jpg");
  106.    }
  107.  }
  108.  
  109.  class ImagePanel extends Panel
  110.  {
  111.    public Image myimg = null;
  112.  
  113.    public ImagePanel()
  114.    {
  115.      setLayout(null);
  116.      setSize(320,240);
  117.    }
  118.  
  119.    public void setImage(Image img)
  120.    {
  121.      this.myimg = img;
  122.      repaint();
  123.    }
  124.  
  125.    public void paint(Graphics g)
  126.    {
  127.      if (myimg != null)
  128.      {
  129.        g.drawImage(myimg, 0, 0, this);
  130.      }
  131.    }
  132.  }
  133.  
  134.  
  135.  public static void saveJPG(Image img, String s)
  136.  {
  137.    BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
  138.    Graphics2D g2 = bi.createGraphics();
  139.    g2.drawImage(img, null, null);
  140.  
  141.    FileOutputStream out = null;
  142.    try
  143.    {
  144.      out = new FileOutputStream(s);
  145.    }
  146.    catch (java.io.FileNotFoundException io)
  147.    {
  148.      System.out.println("File Not Found");
  149.    }
  150.  
  151.    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  152.    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
  153.    param.setQuality(0.5f,false);
  154.    encoder.setJPEGEncodeParam(param);
  155.  
  156.    try
  157.    {
  158.      encoder.encode(bi);
  159.      out.close();
  160.    }
  161.    catch (java.io.IOException io)
  162.    {
  163.      System.out.println("IOException");
  164.    }
  165.  }
  166.  
  167. }
  168.  
Created by GeSHI 1.0.7.20


Logitech QuickCam.
Code
GeSHi (java):
  1. public static boolean PassportPhotoOpen = false;
  2. public static JInternalFrame photoFrameReference;
  3.  
  4. ...
  5.  
  6.    // program sample menu item creation
  7.    mItem = new JMenuItem("Capture Photo");
  8.    mItem.addActionListener(new ActionListener() {
  9.      public void actionPerformed(ActionEvent e) {
  10.        String empID = nText.employeeID; // derived from tree node
  11.        String empName = nText.employeeName;
  12.        createPhotoFrame(empID,empName);
  13.      }
  14.    });
  15.    popup.add(mItem);
  16.  
  17. ...
  18.  
  19. // routine that is executed above that creates the photo capture internal frame
  20. protected void createPhotoFrame(String empID, String empName) {
  21.  // kill photo capture frame if it exists
  22.  if (PassportPhotoOpen){
  23.    PassportPhoto.playerclose(); // rather use the reference?
  24.    photoFrameReference.dispose();
  25.    PassportPhotoOpen = false;
  26.  }
  27.  // create and display frame
  28.  PassportPhoto frame = new PassportPhoto(empID, empName);
  29.  frame.setVisible(true);
  30.  ImageIcon frameIcon = new ImageIcon(Passport.class.getClassLoader().getResource("images/SmallWIM.gif"));
  31.  frame.setFrameIcon(frameIcon);
  32.  PassportPhotoOpen = true;
  33.  photoFrameReference = frame;
  34.  frame.setRequestFocusEnabled(true);
  35.  try {frame.setSelected(true);}
  36.  catch (java.beans.PropertyVetoException e) {} // ignore as obvious if no frame appears
  37.  frame.toFront();
  38. }
  39.  
  40. ...
  41.  
  42. // modified photo capture code
  43.  
  44. import javax.swing.*;
  45. import javax.swing.event.*;
  46. import java.io.*;
  47. import javax.media.*;
  48. import javax.media.format.*;
  49. import javax.media.util.*;
  50. import javax.media.control.*;
  51. import javax.media.protocol.*;
  52. import java.util.*;
  53. import java.awt.*;
  54. import java.awt.image.*;
  55. import java.awt.event.*;
  56. import com.sun.image.codec.jpeg.*;
  57.  
  58. public class PassportPhoto extends JInternalFrame implements ActionListener {
  59.  public static Player player = null;
  60.  public CaptureDeviceInfo di = null;
  61.  public MediaLocator ml = null;
  62.  public JButton capture = null;
  63.  public Buffer buf = null;
  64.  public Image img = null;
  65.  public VideoFormat vf = null;
  66.  public BufferToImage btoi = null;
  67.  public ImagePanel imgpanel = null;
  68.  
  69.  public PassportPhoto(String empID, String empName) {
  70.    super("Photo Capture " +empName, false, true, false, true);
  71.    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); // exit control
  72.    addInternalFrameListener(new InternalFrameAdapter() {
  73.      public void internalFrameActivated(InternalFrameEvent e){}
  74.      public void internalFrameClosed(InternalFrameEvent e){}
  75.      public void internalFrameClosing(InternalFrameEvent e){
  76.        EmployeeTree.PassportPhotoOpen = false;
  77.        playerclose();
  78.        dispose();
  79.      }
  80.      public void internalFrameDeactivated(InternalFrameEvent e){}
  81.      public void internalFrameDeiconified(InternalFrameEvent e){}
  82.      public void internalFrameIconified(InternalFrameEvent e){}
  83.      public void internalFrameOpened(InternalFrameEvent e){}
  84.    });
  85.    JPanel videoPanel = new JPanel();
  86.    capture = new JButton("Capture");
  87.    capture.setSize(new Dimension(79, 26));
  88.    capture.setMinimumSize(new Dimension(79, 26));
  89.    capture.setMaximumSize(new Dimension(79, 26));
  90.    capture.addActionListener(this);
  91.    imgpanel = new ImagePanel();
  92.    //String str1 = "vfw:Logitech USB Video Camera:0";
  93.    //String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
  94.    //di = CaptureDeviceManager.getDevice(str1);
  95.    //ml = di.getLocator();
  96.    // remove above and substitute below
  97.    ml = new MediaLocator("vfw://0");
  98.    try {
  99.      Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, new Boolean(true));
  100.      player = Manager.createRealizedPlayer(ml);
  101.      player.start();
  102.      Component comp;
  103.      if ((comp = player.getVisualComponent()) != null) {
  104.        videoPanel.add(comp, BorderLayout.CENTER);
  105.        videoPanel.setSize(new Dimension(320, 240));
  106.        videoPanel.setMinimumSize(new Dimension(320, 240));
  107.        videoPanel.setMaximumSize(new Dimension(320, 240));
  108.      }
  109.      else {
  110.        // actually will warn user, log error and exit here
  111.        System.out.println("null");
  112.      }
  113.    }
  114.    catch (Exception e) {
  115.      // actually will warn user, log error and exit here
  116.      e.printStackTrace();
  117.    }
  118.    /******************************/
  119.    /* assemble the final screen  */
  120.    /******************************/
  121.    Container contentPane = getContentPane();
  122.    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
  123.    contentPane.add(videoPanel, BorderLayout.WEST);
  124.    contentPane.add(capture,BorderLayout.CENTER);
  125.    contentPane.add(imgpanel,BorderLayout.EAST);
  126.    setSize(730, 275); // window size
  127.    setMinimumSize(new Dimension(730, 275));
  128.    setMaximumSize(new Dimension(730, 275));
  129.    setLocation(30, 30); // window location
  130.  }
  131.  public static void playerclose() {
  132.    player.close();
  133.    player.deallocate();
  134.  }
  135.  public void actionPerformed(ActionEvent e) {
  136.    JComponent c = (JComponent) e.getSource();
  137.    if (c == capture) {
  138.      // Grab the frame
  139.      FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");
  140.      buf = fgc.grabFrame();
  141.      btoi = new BufferToImage((VideoFormat)buf.getFormat()); // Convert to image
  142.      img = btoi.createImage(buf);
  143.      imgpanel.setImage(img); // display the image
  144.      // actually will have a separate save routine
  145.      saveJPG(img,"c:\\test.jpg"); // save image
  146.    }
  147.  }
  148.  class ImagePanel extends JPanel {
  149.    public Image myimg = null;
  150.    public ImagePanel() {
  151.      setSize(320,240);
  152.      setMinimumSize(new Dimension(320, 240));
  153.      setMaximumSize(new Dimension(320, 240));
  154.    }
  155.    public void setImage(Image img) {
  156.      this.myimg = img;
  157.      repaint();
  158.    }
  159.    public void paint(Graphics g) {if (myimg != null) {g.drawImage(myimg, 0, 0, this);}}
  160.  }
  161.  public static void saveJPG(Image img, String s) {
  162.    BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
  163.    Graphics2D g2 = bi.createGraphics();
  164.    g2.drawImage(img, null, null);
  165.    FileOutputStream out = null;
  166.    try{out = new FileOutputStream(s);}
  167.    catch (java.io.FileNotFoundException io) {
  168.      // again actually will warn user, log error and exit here
  169.      System.out.println("File Not Found");
  170.    }
  171.    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  172.    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
  173.    param.setQuality(0.5f,false);
  174.    encoder.setJPEGEncodeParam(param);
  175.    try {
  176.      encoder.encode(bi);
  177.      out.close();
  178.    }
  179.    catch (java.io.IOException io) {
  180.      // again actually will warn user, log error and exit here
  181.      System.out.println("IOException");
  182.    }
  183.  }
  184. }
Created by GeSHI 1.0.7.20

Logged

Founder of www.Retardation-Foundation.com :: A community for retarded people!? Powered by admin Gaylord Ritardo.
Zeus
Member
*

Reputation: 0
Offline Offline
Posts: 11
Referrals: 0

Awards
« Reply #1 on: May 18, 2009, 07:30:59 PM »

thx
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here