使用JavaCV/OpenCV抓取并存储摄像头图像

时间:2023-03-10 02:18:41
使用JavaCV/OpenCV抓取并存储摄像头图像

http://blog.****.net/ljsspace/article/details/6702178

使用JavaCV/OpenCV抓取并存储摄像头图像 分类:
图形图像(3) 使用JavaCV/OpenCV抓取并存储摄像头图像

版权声明:本文为博主原创文章,未经博主允许不得转载。

本程序通过JFrame实时显示本机摄像头图像,并将图像存储到一个缓冲区,当用户用鼠标点击JFrame中任何区域时,显示抓取图像的简单动画,同时保存缓冲区的图像到磁盘文件中。点击JFrame关闭按钮可以退出程序。

实现:

  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 are two 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(10, 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. }