Java in the use of JMF to write camera photography program

Source: Internet
Author: User
The program first downloads the latest JMF to Sun and installs it. 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:

Using these three classes to get the camera driver, and to obtain the image stream inside the camera, the image stream is captured by a swing component component class

public static player player = null;
private Capturedeviceinfo di = null;
private Medialocator ml = null;

The driver writing provided in the document, why do I not know:

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)//If the camera button is pressed
{
Framegrabbingcontrol FGC = (Framegrabbingcontrol) player.getcontrol ("Javax.media.control.FrameGrabbingControl");
BUF = Fgc.grabframe (); Get the current Zhen and deposit it into the buffer class
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);/uncompressed image
Encoder.setjpegencodeparam (param);
Try
{
Encoder.encode (BI);
Out.close ();
}
catch (java.io.IOException io)
{
System.out.println ("IOException");
}


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.