http://blog.csdn.net/ljsspace/article/details/6702178
Classification:graphic Images (3)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
This program through JFrame real-time display of the camera image, and the image stored in a buffer, when the user click on any area of jframe with the mouse, display the simple animation of the captured image, while saving the buffer image to the disk file. Click the JFrame Close button to exit the program.
Realize:
[Java]View PlainCopy
- Import Java.awt.Graphics2D;
- Import java.awt.event.ActionEvent;
- Import Java.awt.event.ActionListener;
- Import Java.awt.event.MouseAdapter;
- Import java.awt.event.MouseEvent;
- Import Java.awt.image.BufferedImage;
- Import Java.io.File;
- Import java.io.IOException;
- Import Javax.imageio.ImageIO;
- Import Javax.swing.Timer;
- Import Com.googlecode.javacv.CanvasFrame;
- Import Com.googlecode.javacv.OpenCVFrameGrabber;
- Import Com.googlecode.javacv.cpp.opencv_core. Iplimage;
- Import static com.googlecode.javacv.cpp.opencv_core.cvReleaseImage;
- /**
- *
- * Use JAVACV/OPENCV to capture camera images
- *
- * There is functions in this demo:
- * 1) Show real-time camera images
- * 2) Capture camera images by mouse-clicking anywhere in the JFrame,
- * The JPG file is saved in a hard-coded path.
- *
- * @author Ljs
- * 2011-08-19
- *
- */
- Public class Cameracapture {
- public static String savedimagefile = "C:\\tmp\\my.jpg";
- //timer for image capture animation
- static class Timeraction implements ActionListener {
- Private graphics2d G;
- private Canvasframe Canvasframe;
- private int width,height;
- private int delta=10;
- private int count = 0;
- Private timer timer;
- public void SetTimer (Timer timer) {
- This.timer = timer;
- }
- Public timeraction (Canvasframe canvasframe) {
- THIS.G = (graphics2d) Canvasframe.getcanvas (). Getgraphics ();
- this.canvasframe = canvasframe;
- this.width = Canvasframe.getcanvas (). GetWidth ();
- this.height = Canvasframe.getcanvas (). GetHeight ();
- }
- public void actionperformed (ActionEvent e) {
- int offset = Delta*count;
- if (width-offset>=offset && height-offset >= offset) {
- G.drawrect (offset, offset, width-2*offset, height-2*offset);
- Canvasframe.repaint ();
- count++;
- }else{
- //when animation is done, reset count and stop timer.
- Timer.stop ();
- Count = 0;
- }
- }
- }
- public static void Main (string[] args) throws Exception {
- //open Camera Source
- Opencvframegrabber grabber = new Opencvframegrabber (0);
- Grabber.start ();
- //create a frame for real-time image display
- Canvasframe canvasframe = new Canvasframe ("Camera");
- Iplimage image = Grabber.grab ();
- int width = image.width ();
- int height = image.height ();
- Canvasframe.setcanvassize (width, height);
- //onscreen buffer for image capture
- final BufferedImage bimage = new BufferedImage (width, height, bufferedimage.type_int_rgb);
- Graphics2D bgraphics = Bimage.creategraphics ();
- //animation Timer
- Timeraction timeraction = new Timeraction (Canvasframe);
- Final Timer timer=new timer (ten, timeraction);
- Timeraction.settimer (timer);
- //click the frame to capture an image
- Canvasframe.getcanvas (). Addmouselistener (new Mouseadapter () {
- public void mouseclicked (MouseEvent e) {
- Timer.start (); //start Animation
- try {
- Imageio.write (bimage, "jpg", new File (Savedimagefile));
- } catch (IOException E1) {
- E1.printstacktrace ();
- }
- }
- });
- //real-time Image Display
- While (Canvasframe.isvisible () && (Image=grabber.grab ()) = null) {
- if (!timer.isrunning ()) { //when animation is on, pause real-time display
- Canvasframe.showimage (image);
- //draw The onscreen image simutaneously
- Bgraphics.drawimage (Image.getbufferedimage (),null,0,0);
- }
- }
- //release Resources
- Cvreleaseimage (image);
- Grabber.stop ();
- Canvasframe.dispose ();
- }
- }
Capture and store camera images using JAVACV/OPENCV