Which Technology you used?

Wednesday 14 November 2012

MY COMPANY SITE

 

Hello friends, We are going to launch a new website of our company shortly. and we are going to provide the best IT solution in software, web and mobile application development. also we provide the best training in JAVA, ANDROID, PHP, ASP.NET and I-PHONE which is based on SDLC process. and on our site you can also use forum facility to ask the questions to our experts related to technology. We have also provided which course is covered in our training period. so kindly check it out.

Tuesday 20 December 2011

capture image using webcam in java


Capturing an image

First you need to get a list of devices with the help of CaptureDeviceManager and filter out the first element of that list. Then you can get the visual component of it and use it as the video screen.
Then you have to place all those player control panel and video screen on a frame. After that you allow the program to capture image after a certain time.
After that as in many cases Java has to treat the buffered image. You can save the image in any format you wish, with the help of ImageIO.


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.media.*;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.RGBFormat;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;

/**
 *
 * @author BUDDHIMA
 */
public class WebCam {

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

    public static void main(String args[]) {
        new WebCam();// create a new instance of WebCam in main function
    }

    WebCam() {
        try {
//gets a list of devices how support the given videoformat
            Vector deviceList = CaptureDeviceManager.getDeviceList(new RGBFormat());
            System.out.println(deviceList.toString());

//gets the first device in deviceList
            device = (CaptureDeviceInfo) deviceList.firstElement();

            ml = device.getLocator();

            player = Manager.createRealizedPlayer(ml);

            player.start();

            videoScreen = player.getVisualComponent();
            Frame frm = new Frame();
            frm.setBounds(10, 10, 900, 700);//sets the size of the screen

// setting close operation to the frame
            frm.addWindowListener(new WindowAdapter() {

                public void windowClosing(WindowEvent we) {
                    System.exit(0);
                }
            });

//place player and video screen on the frame
            frm.add(videoScreen, BorderLayout.CENTER);
            frm.add(player.getControlPanelComponent(), BorderLayout.SOUTH);
            frm.setVisible(true);

//capture image
            Thread.sleep(10000);//wait 10 seconds before capturing photo

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

            saveImagetoFile(img, "MyPhoto.jpg");//save the captured image as MyPhoto.jpg

        } catch (Exception e) {
            System.out.println(e);
        }
    }

    private void saveImagetoFile(Image img, String string) {
        try {
            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);

            ImageIO.write(bi, fileType, new File(string));

        } catch (Exception e) {
        }
    }
}

Friday 14 October 2011