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

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

 分类:
 

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

实现:

[java] view plain copy
 
  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.   
  10. import javax.imageio.ImageIO;  
  11. import javax.swing.Timer;  
  12.   
  13. import com.googlecode.javacv.CanvasFrame;  
  14. import com.googlecode.javacv.OpenCVFrameGrabber;  
  15. import com.googlecode.javacv.cpp.opencv_core.IplImage;  
  16. import static com.googlecode.javacv.cpp.opencv_core.cvReleaseImage;  
  17.   
  18. /** 
  19.  *  
  20.  * Use JavaCV/OpenCV to capture camera images 
  21.  *  
  22.  * There are two functions in this demo: 
  23.  * 1) show real-time camera images  
  24.  * 2) capture camera images by mouse-clicking anywhere in the JFrame,  
  25.  * the jpg file is saved in a hard-coded path.  
  26.  *  
  27.  * @author ljs 
  28.  * 2011-08-19 
  29.  * 
  30.  */  
  31. public class CameraCapture {  
  32.     public static String savedImageFile = "c:\tmp\my.jpg";  
  33.       
  34.     //timer for image capture animation  
  35.     static class TimerAction implements ActionListener {  
  36.         private Graphics2D g;  
  37.         private CanvasFrame canvasFrame;  
  38.         private int width,height;  
  39.           
  40.         private int delta=10;  
  41.         private int count = 0;  
  42.           
  43.         private Timer timer;  
  44.         public void setTimer(Timer timer){  
  45.             this.timer = timer;  
  46.         }  
  47.            
  48.         public TimerAction(CanvasFrame canvasFrame){  
  49.             this.g = (Graphics2D)canvasFrame.getCanvas().getGraphics();   
  50.             this.canvasFrame = canvasFrame;  
  51.             this.width = canvasFrame.getCanvas().getWidth();  
  52.             this.height = canvasFrame.getCanvas().getHeight();  
  53.         }  
  54.         public void actionPerformed(ActionEvent e) {  
  55.             int offset = delta*count;  
  56.             if(width-offset>=offset && height-offset >= offset) {          
  57.                 g.drawRect(offset, offset, width-2*offset, height-2*offset);  
  58.                 canvasFrame.repaint();  
  59.                 count++;  
  60.             }else{  
  61.                 //when animation is done, reset count and stop timer.  
  62.                 timer.stop();  
  63.                 count = 0;  
  64.             }              
  65.         }  
  66.     }  
  67.   
  68.     public static void main(String[] args) throws Exception {  
  69.         //open camera source  
  70.         OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);  
  71.         grabber.start();  
  72.           
  73.         //create a frame for real-time image display  
  74.         CanvasFrame canvasFrame = new CanvasFrame("Camera");  
  75.         IplImage image = grabber.grab();  
  76.         int width = image.width();  
  77.         int height = image.height();  
  78.         canvasFrame.setCanvasSize(width, height);  
  79.           
  80.         //onscreen buffer for image capture  
  81.         final BufferedImage bImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
  82.         Graphics2D bGraphics = bImage.createGraphics();       
  83.           
  84.         //animation timer  
  85.         TimerAction timerAction = new TimerAction(canvasFrame);  
  86.         final Timer timer=new Timer(10, timerAction);  
  87.         timerAction.setTimer(timer);  
  88.            
  89.         //click the frame to capture an image  
  90.         canvasFrame.getCanvas().addMouseListener(new MouseAdapter(){  
  91.             public void mouseClicked(MouseEvent e){       
  92.                 timer.start(); //start animation  
  93.                 try {  
  94.                     ImageIO.write(bImage, "jpg", new File(savedImageFile));  
  95.                 } catch (IOException e1) {  
  96.                     e1.printStackTrace();  
  97.                 }                     
  98.             }                  
  99.         });  
  100.           
  101.         //real-time image display  
  102.         while(canvasFrame.isVisible() && (image=grabber.grab()) != null){  
  103.             if(!timer.isRunning()) { //when animation is on, pause real-time display  
  104.                 canvasFrame.showImage(image);     
  105.                 //draw the onscreen image simutaneously  
  106.                 bGraphics.drawImage(image.getBufferedImage(),null,0,0);    
  107.             }  
  108.         }  
  109.           
  110.         //release resources  
  111.         cvReleaseImage(image);     
  112.         grabber.stop();  
  113.         canvasFrame.dispose();  
  114.     }  
  115.   
  116. }  
原文地址:https://www.cnblogs.com/donaldlee2008/p/5362053.html