Use JMF to take pictures :)

xiaoxiao2021-03-06  70

I divide the program into two kinds, interesting and boring, recently made a few interesting projects, one of which should, even if the camera is photographed :), used for the scene, generate photos, mainly to use Java Media Framework (JMF).

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

Then, talk about the needs

1. Camera photo

2. Enter a file name in the text box

3. Press the camera button to get the image in the camera

4. There is a red frame to intercept the fixed size photo on the photo taken.

5. Saved as the local image as JPG format, must not compress the picture quality

Technical key, I believe that it is also the most interesting part of everyone to make a camera work and take a photo.

Using JMF, the code is simple:

// Using these three classes to get the camera driver, and get the image stream in the camera, the obtained image stream is a Swing Component component class.

Public static player player = null; private capTureDeviceInfo di = null; private mediaocator ml = null;

// Drive Writing provided in the document, why don't you know this?

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 on the camera to get the current image in 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

JComponent c = (JComponent) e.getSource (); if (c == capture) // if the photographing button is pressed {FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl ( "javax.media.control.FrameGrabbingControl"); BUF = fgc.grabframe (); // Get the current congenial BTOI = New BuffertoImage ((VideoFormat) buf.getformat ()); img = btoi.createImage (buf); // show the image imgpanel.setimage (IMG);

There is not much to save the image, 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

Allf

, false); // does not compress image encoder.setjpegencodepEParam (param); try {encoder.encode (bi); out.close ();} catch (java.io iexception IO) {system.out.println ("IOEXCEPTION) ");

I have applied to establish JWEBCAM into an open source project, put it in GRO, let's play your own imagination to join your code, such as shooting video, add image processing, and so on. If there is a change in Cool, remember to send it to me :)

转载请注明原文地址:https://www.9cbs.com/read-88137.html

New Post(0)