Java in the use of JMF to write camera photography program

Source: Internet
Author: User

I split the program into two, interesting and uninteresting, and recently made a couple of interesting projects, one of which should be even a camera-taking program. Used for scene photography, photo generation, and primarily for the Java Media Framework (JMF).

First go to Sun to download the latest JMF, and then install. http://java.sun.com/products/java-media/jmf/index.jsp

Then, say the need

1. Take a picture with a camera

2. Enter a file name in the text box

3. Press the photo button to get the image inside the camera

4. There is a red box on the photo that captures the fixed size of the picture.

5. Save as a local image in JPG format, no compression

The key to the technology, I believe the most interesting part is how to get a camera to work, and take a picture.

With JMF, the code is simple:

//利用这三个类分别获取摄像头驱动,和获取摄像头内的图像流,获取到的图像流是一个Swing的Component组件类
public static Player player = null;
private CaptureDeviceInfo di = null;
private MediaLocator ml = null;
//文档中提供的驱动写法,为何这么写我也不知:)
String str1 = "vfw:Logitech USB Video Camera:0";
String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
di = CaptureDeviceManager.getDevice(str2);
ml = di.getLocator();
try
{
 player = Manager.createRealizedPlayer(ml);
 player.start();
 Component comp;
 if ((comp = player.getVisualComponent()) != null)
 {
  add(comp, BorderLayout.NORTH);
 }
}
catch (Exception e)
{
 e.printStackTrace();
}

The next step is to click the photo to get the current image inside the camera.

The code is also very simple:

private JButton capture;
private Buffer buf = null;
private BufferToImage btoi = null;
private ImagePanel imgpanel = null;
private Image img = null;
private ImagePanel imgpanel = null;
JComponent c = (JComponent) e.getSource();
if (c == capture)//如果按下的是拍照按钮
{
 FrameGrabbingControl fgc =(FrameGrabbingControl)  player.getControl("javax.media.control.FrameGrabbingControl");
 buf = fgc.grabFrame(); // 获取当前祯并存入Buffer类
 btoi = new BufferToImage((VideoFormat) buf.getFormat());
 img = btoi.createImage(buf); // show the image
 imgpanel.setImage(img);
}

Save the image is not much to say, the following is the sample code

BufferedImage bi = (BufferedImage) createImage(imgWidth, imgHeight);
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(1f, false);//不压缩图像
encoder.setJPEGEncodeParam(param);
try
{
 encoder.encode(bi);
 out.close();
}
catch (java.io.IOException io)
{
 System.out.println("IOException");
}

Have applied to build Jwebcam as an open source project, put it into Gro, let's use your imagination to add your own code, such as shooting video, adding image processing functions, and so on.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.