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

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


实现:

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

}



  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值