I divide the program into two kinds, interesting and boring, and have recently made a few interesting projects, one of which, even if the camera is photographed. Used for on-site photography, generating photos, mainly used in 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 capteviceInfo di = null; private Medialocator ml = null ////-document, the driver writing, why do I don't know why:) 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 = null; JComponent c = (JComponent) e.getSource (); if (c = = capture) // If you press a camera button {FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl ( "javax.media.control.FrameGrabbingControl"); buf = fgc.grabFrame (); // get the current and stored Chen Buffer class btoi = new buffertoImage ((VideoFormat) buf.getformat ()); img = btoi.createImage (buf); // show the image imgpanel.setimage (});} If you save the image, you are following it. 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 ); // does not compress image encoder.setjpegencodepAram (param); try {encoder.encode (bi); out.close ();} catch (java.io iexception IO) {system.out.println ("oException") }
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.