1 Reply Latest reply: May 7, 2012 6:21 AM by 935476 RSS

    JMF code samples?

    928831
      Hi,

      I am have just installed JMF and have it working and up and running. I haven't used JMF before and want to learn how program using it! What i want to do is to take a picture from my webcam and save it.

      Are there any library's of code samples or tutorials that anyone can think of that could help me out? Or even books I can read?

      Many Thanks
        • 1. Re: JMF code samples?
          935476
          I have written a small app that can maybe get you started, its a little rough around the edges but it works :-)
          Code below (BasicWebCam.java):

          If you want the Eclispe project, let me know and I will send it to you.

          import java.awt.Component;
          import java.awt.EventQueue;
          import java.awt.Graphics2D;
          import java.awt.Image;
          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;
          import java.awt.image.BufferedImage;
          import java.io.FileOutputStream;
          import java.text.DateFormat;
          import java.text.SimpleDateFormat;
          import java.util.Calendar;

          import javax.media.Buffer;
          import javax.media.CaptureDeviceInfo;
          import javax.media.CaptureDeviceManager;
          import javax.media.Manager;
          import javax.media.MediaLocator;
          import javax.media.Player;
          import javax.media.control.FrameGrabbingControl;
          import javax.media.format.VideoFormat;
          import javax.media.util.BufferToImage;
          import javax.swing.JFrame;
          import javax.swing.JPanel;
          import javax.swing.border.EmptyBorder;
          import javax.swing.JButton;

          import com.sun.image.codec.jpeg.JPEGCodec;
          import com.sun.image.codec.jpeg.JPEGEncodeParam;
          import com.sun.image.codec.jpeg.JPEGImageEncoder;

          public class BasicWebCam extends JFrame implements ActionListener {

          private JPanel contentPane;
          private JPanel pnlCam = new JPanel();
          public CaptureDeviceInfo captureDeviceInfo;
          public MediaLocator mediaLocator;
          public Player player;
          private Component comp;
          public javax.swing.Timer timer = new javax.swing.Timer(250, (ActionListener) this);
          public Buffer BUF;
          public Image img;
          public BufferToImage BtoI;

          /**
          * Launch the application.
          */
          public static void main(String[] args) {
               EventQueue.invokeLater(new Runnable() {
               public void run() {
                    try {
                    BasicWebCam frame = new BasicWebCam();
                    frame.setVisible(true);
                    } catch (Exception e) {
                    e.printStackTrace();
                    }
               }
               });
          }

          /**
          * Create the frame.
          */
          public BasicWebCam() {
               setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               setBounds(100, 100, 353, 327);
               contentPane = new JPanel();
               contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
               setContentPane(contentPane);
               contentPane.setLayout(null);

               pnlCam.setBounds(10, 11, 320, 240);
               contentPane.add(pnlCam);

               JButton btnCapture = new JButton("Capture");
               btnCapture.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                    actionCapture();
                    }
               });
               btnCapture.setBounds(124, 262, 89, 23);
               contentPane.add(btnCapture);

               init();
          }

          public void init() {
               String strDevice = "vfw:Microsoft WDM Image Capture (Win32):0";
               mediaLocator = new MediaLocator("vfw://0");
               // FOR LINUX
               // String strDevice = "v4l:Laptop Integrated Webcam:0";
               // mediaLocator = new MediaLocator("v4l://0");
               captureDeviceInfo = CaptureDeviceManager.getDevice(strDevice);
               showVid();
          }

          private void showVid() {
               try {
               player = Manager.createRealizedPlayer(mediaLocator);
               player.start();
               if ((comp = player.getVisualComponent()) != null) {
                    comp.setSize(pnlCam.getWidth(), pnlCam.getHeight());
                    // comp.setPreferredSize(new Dimension(pnlCapture.getWidth(),
                    // pnlCapture.getHeight()));
                    pnlCam.add(comp);
               }
               Thread.sleep(5000);
               timer.start(); // start timer
               } catch (Exception e) {
               e.printStackTrace();
               }
          }

          public void actionCapture() {
               // Grab a frame
               // comp.setSize(pnlCapture.getWidth(), pnlCapture.getHeight());
               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);

               // resize
               img = resizeImage(img, pnlCam.getWidth(), pnlCam.getHeight());

               // show the image
               // pnlImage.setImage(img);
               img.getGraphics().drawString(getDateTime("yyyy/MM/dd HH:mm:ss") + " - ColinCoderator", 50, 235);

               // save image
               String fileName = getDateTime("yyyy-MM-dd_HH-mm-ss") + ".jpg";
               String path = "c:/projects/BasicWebCam/" + fileName;
               System.out.println(path);
               // save image
               saveJPG(img, path);
          }

          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");
               }
          }

          public Image resizeImage(Image img, int width, int height) {
               BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
               Graphics2D g = resizedImage.createGraphics();
               g.drawImage(img, 0, 0, width, height, null);
               g.dispose();
               return resizedImage;
          }

          public static String getDateTime(String format) {
               DateFormat dateFormat = new SimpleDateFormat(format);
               Calendar cal = Calendar.getInstance();
               return (dateFormat.format(cal.getTime()));
          }

          @Override
          public void actionPerformed(ActionEvent arg0) {
               // TODO Auto-generated method stub

          }
          }