当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Displaying Current Pictures From A Camera - UPDATED

问题描述:

I would like to develop an application that will receive pictures from a camera and maybe afterwards save it in a database. This is what i want to do:

When the picture is taken it is send to the program immediately (or the program must read the current picture taken) then display it. I will take pictures of many people or things so anytime a picture is taken i want to see the current picture displayed in the program.

I have googled if i can see an example application so that i know that it possible so that i can do mine from scratch. But couldn't find any so i am not sure if it is possible to do it in java.

So guys am asking for guidelines how i can do it in java. I just need the steps then i will program everything myself.

Thanks.

UPDATE

* It is a desktop application(J2SE).

* The camera will be constantly link to the computer on which the application is running whiles the pictures are been taken via cable for the moment(later maybe wireless).

* I will use any one of the ordinary digital cameras around

网友答案:

If JMF (link) runs on your system it is fairly easy to grab photographs from your streaming video. Check this sample code if you run on Windows. (Knowledge blog)

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.media.*;
import javax.media.control.*;
import javax.media.format.*;
import javax.media.util.*;


/**
 * Grabs a frame from a Webcam, overlays the current date and time, and saves the frame as a PNG to c:\webcam.png
 *
 * @author David
 * @version 1.0, 16/01/2004
 */
public class FrameGrab
{
    public static void main(String[] args) throws Exception
    {
        // Create capture device
        CaptureDeviceInfo deviceInfo = CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture (Win32):0");
        Player player = Manager.createRealizedPlayer(deviceInfo.getLocator());
        player.start();

        // Wait a few seconds for camera to initialise (otherwise img==null)
        Thread.sleep(3000);

        // Grab a frame from the capture device
        FrameGrabbingControl frameGrabber = (FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");
        Buffer buf = frameGrabber.grabFrame();

        // Convert frame to an buffered image so it can be processed and saved
        Image img = (new BufferToImage((VideoFormat)buf.getFormat()).createImage(buf));
        BufferedImage buffImg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = buffImg.createGraphics();
        g.drawImage(img, null, null);

        // Overlay curent time on image
        g.setColor(Color.RED);
        g.setFont(new Font("Verdana", Font.BOLD, 16));
        g.drawString((new Date()).toString(), 10, 25);

        // Save image to disk as PNG
        ImageIO.write(buffImg, "png", new File("c:\\webcam.png"));

        // Stop using webcam
        player.close();
        player.deallocate();
        System.exit(0);
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: