Java Web Cam Example using JMF

After long time i have decide to post again about Java web cam which I have  explained before .

Last time i have used video4linux with linux platform but this time I’m going to use Java Media Framework (JMF) with with windows platform.

First you have to instal the JMF into windows you can download it from Oracle website.Next you have to import JMF plugins into your project .

In here I have used netbeans IDE there for you can see some methods which are not useful for jmf but IDE has includes for its source code by itself.

ex

In hare i have created JFrame and load jmf components into JPanel .

Code Example :

This is the code should be in your jframe in here i have not include the codes regarding for creating of jframe.

public class SimpleWebCam extends javax.swing.JFrame {

    CaptureDeviceInfo device;
    MediaLocator ml;
    Player player;
    Component videoScreen;

    public SimpleWebCam() {
        initComponents();
        // path to webcam in windows web cam device is located in here
        String mediaFile = "vfw:Micrsoft WDM Image Capture (Win32):0";

            ml = new MediaLocator(mediaFile);
        try {
            player = Manager.createRealizedPlayer(ml);

            player.start();
            // create video screen to display webcam preview
            videoScreen = player.getVisualComponent();
            videoScreen.setSize(p1.getSize());
            p1.removeAll();
            p1.add(videoScreen, BorderLayout.CENTER);
            p1.repaint();
            p1.revalidate();

            p2.removeAll();
            p2.add(player.getControlPanelComponent());
            p2.repaint();
            p2.revalidate();

        } catch (IOException ex) {
            Logger.getLogger(SimpleWebCam.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoPlayerException ex) {
            Logger.getLogger(SimpleWebCam.class.getName()).log(Level.SEVERE, null, ex);
        } catch (CannotRealizeException ex) {
            Logger.getLogger(SimpleWebCam.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

This is the code part which is should be in your ” Capture ” Button event .


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        try {

            Thread.sleep(100);//wait 10 seconds before capturing photo
        } catch (InterruptedException ex) {
            Logger.getLogger(SimpleWebCam.class.getName()).log(Level.SEVERE, null, ex);
        }

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

            Buffer buf = fgc.grabFrame();//grab the current frame on video screen

            BufferToImage btoi = new BufferToImage((VideoFormat) buf.getFormat());

            Image img = btoi.createImage(buf);
            // save image to file
             saveImagetoFile(img, "MyPhoto.jpg");

             new campic(img).setVisible(true);
    }

Method for save captured image in the hard disk (this method calls inside the capture button action performed event )

 private void saveImagetoFile(Image img, String string) {

            int w = img.getWidth(null);
            int h = img.getHeight(null);
            BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = bi.createGraphics();

            g2.drawImage(img, 0, 0, null);

            g2.dispose();

            String fileType = string.substring(string.indexOf('.') + 1);
        try {
            ImageIO.write(bi, fileType, new File(string));
        } catch (IOException ex) {
            Logger.getLogger(SimpleWebCam.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

I hope you will understand the example you can download complete project file from here
Download Project