详解使用JavaCV/OpenCV抓取并存储摄像头图像
作者:ljsspace 发布时间:2023-05-08 07:23:31
标签: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();
}
}
来源:http://blog.csdn.net/ljsspace/article/details/6702178


猜你喜欢
- 前言emmm… 有个需求,需要根据信息生成svg,因为考虑到样式一致性的问题最终决定有服务端来生成svg。Java提供
- 首先利用IDEA创建Maven工程项目1.选择新建项目2.选中Maven骨架3.填写项目名称和项目位置4.Finsh之后默认打开的是pom.
- 前言java有八个基本数据类型,每个都有对应的一个包装类,比如int对应的Integer。 Integer 是int的包装类型,数据类型是类
- 1.Java 9以前堆栈遍历到目前为止,官方解决方案是获取当前线程并调用其getStackTrace()方法:StackTraceEleme
- 并发编程中的三个概念:1.原子性在Java中,对基本数据类型的变量的读取和赋值操作是原子性操作,即这些操作是不可被中断的,要么执行,要么不执
- 介绍超级管理员:系统管理、用户管理、网点管理、运输点管理、快递员管理、网点申请管理(审核)、报价管理(时效报价)等。普通用户:注册登录、个人
- 线程的作用和意义线程 被定义为程序的执行路径。每个线程都定义了一个独特的控制流。如果您的应用程序涉及到复杂的和耗时的操作,那么设置不同的线程
- 一、前言数据导出为Excel在我们写项目的过程中经常用到需要用到的jar包 poi-3.17.jar 二、具体实现步骤//第一步创建一个we
- Android ToggleButton 详解在Android的开发过程中,对于ToggleButton的使用频率也是相当的高的,下面我就来
- 背景项目中要实现横向列表的无限循环滚动,自然而然想到了RecyclerView,但我们常用的RecyclerView是不支持无限循环滚动的,
- 这篇文章主要介绍了SpringBoot+SpringCloud实现登录用户信息在微服务之间的传递,文中通过示例代码介绍的非常详细,对大家的学
- 一:对象,JavaBean,SpringBean的区别1.什么是JavaBeanjavaBean要求所有属性为私有,该类必须有一个公共无参构
- 引言前面在学习协程启动方式的时候在launch的源码中有一个返回值是Job,async的返回Deferred也是实现了Job,那么而也就是说
- 本文实例讲述了C#多线程处理多个队列数据的方法。分享给大家供大家参考。具体实现方法如下:using System;using System.
- 本文实例讲述了Winform实现调用asp.net数据接口的方法,分享给大家供大家参考。具体实现方法如下:一、问题:最近一个WPF项目需要改
- delphi dll 源码:library dllres; type char1
- 在android提供了一种类型:Parcel。被用作封装数据的容器,封装后的数据可以通过Intent或IPC传递。 除了基本类型以外,只有实
- 对象POJO和JSON互转public class JsonUtil { /** * JSON 转 POJO &n
- QueryWrapper实现MybatisPlus多表关联查询1.dao层接口使用Select注解写SQL重点:@Param("e
- 前言平时做一些统计数据,经常从数据库或者是从接口获取出来的数据,单位是跟业务需求不一致的。比如, 我们拿出来的 分, 实际上要是元又比如,我