Capture and store camera images using JAVACV/OPENCV

Source: Internet
Author: User

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
  1. Import Java.awt.Graphics2D;
  2. Import java.awt.event.ActionEvent;
  3. Import Java.awt.event.ActionListener;
  4. Import Java.awt.event.MouseAdapter;
  5. Import java.awt.event.MouseEvent;
  6. Import Java.awt.image.BufferedImage;
  7. Import Java.io.File;
  8. Import java.io.IOException;
  9. Import Javax.imageio.ImageIO;
  10. Import Javax.swing.Timer;
  11. Import Com.googlecode.javacv.CanvasFrame;
  12. Import Com.googlecode.javacv.OpenCVFrameGrabber;
  13. Import Com.googlecode.javacv.cpp.opencv_core. Iplimage;
  14. Import static com.googlecode.javacv.cpp.opencv_core.cvReleaseImage;
  15. /**
  16. *
  17. * Use JAVACV/OPENCV to capture camera images
  18. *
  19. * There is functions in this demo:
  20. * 1) Show real-time camera images
  21. * 2) Capture camera images by mouse-clicking anywhere in the JFrame,
  22. * The JPG file is saved in a hard-coded path.
  23. *
  24. * @author Ljs
  25. * 2011-08-19
  26. *
  27. */
  28. Public class Cameracapture {
  29. public static String savedimagefile = "C:\\tmp\\my.jpg";
  30. //timer for image capture animation
  31. static class Timeraction implements ActionListener {
  32. Private graphics2d G;
  33. private Canvasframe Canvasframe;
  34. private int width,height;
  35. private int delta=10;
  36. private int count = 0;
  37. Private timer timer;
  38. public void SetTimer (Timer timer) {
  39. This.timer = timer;
  40. }
  41. Public timeraction (Canvasframe canvasframe) {
  42. THIS.G = (graphics2d) Canvasframe.getcanvas (). Getgraphics ();
  43. this.canvasframe = canvasframe;
  44. this.width = Canvasframe.getcanvas (). GetWidth ();
  45. this.height = Canvasframe.getcanvas (). GetHeight ();
  46. }
  47. public void actionperformed (ActionEvent e) {
  48. int offset = Delta*count;
  49. if (width-offset>=offset && height-offset >= offset) {
  50. G.drawrect (offset, offset, width-2*offset, height-2*offset);
  51. Canvasframe.repaint ();
  52. count++;
  53. }else{
  54. //when animation is done, reset count and stop timer.
  55. Timer.stop ();
  56. Count = 0;
  57. }
  58. }
  59. }
  60. public static void Main (string[] args) throws Exception {
  61. //open Camera Source
  62. Opencvframegrabber grabber = new Opencvframegrabber (0);
  63. Grabber.start ();
  64. //create a frame for real-time image display
  65. Canvasframe canvasframe = new Canvasframe ("Camera");
  66. Iplimage image = Grabber.grab ();
  67. int width = image.width ();
  68. int height = image.height ();
  69. Canvasframe.setcanvassize (width, height);
  70. //onscreen buffer for image capture
  71. final BufferedImage bimage = new BufferedImage (width, height, bufferedimage.type_int_rgb);
  72. Graphics2D bgraphics = Bimage.creategraphics ();
  73. //animation Timer
  74. Timeraction timeraction = new Timeraction (Canvasframe);
  75. Final Timer timer=new timer (ten, timeraction);
  76. Timeraction.settimer (timer);
  77. //click the frame to capture an image
  78. Canvasframe.getcanvas (). Addmouselistener (new Mouseadapter () {
  79. public void mouseclicked (MouseEvent e) {
  80. Timer.start (); //start Animation
  81. try {
  82. Imageio.write (bimage, "jpg", new File (Savedimagefile));
  83. } catch (IOException E1) {
  84. E1.printstacktrace ();
  85. }
  86. }
  87. });
  88. //real-time Image Display
  89. While (Canvasframe.isvisible () && (Image=grabber.grab ()) = null) {
  90. if (!timer.isrunning ()) { //when animation is on, pause real-time display
  91. Canvasframe.showimage (image);
  92. //draw The onscreen image simutaneously
  93. Bgraphics.drawimage (Image.getbufferedimage (),null,0,0);
  94. }
  95. }
  96. //release Resources
  97. Cvreleaseimage (image);
  98. Grabber.stop ();
  99. Canvasframe.dispose ();
  100. }
  101. }

Capture and store camera images using JAVACV/OPENCV

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.