详解使用JavaCV/OpenCV抓取并存储摄像头图像
作者:ljsspace 时间:2023-05-08 07:23:31
本程序通过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
标签:javacv,opencv
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#实现的字符串转MD5码函数实例
2023-03-02 15:34:43
简单了解redis常见客户端及Sharding机制原理
2022-03-28 16:49:09
C#基于UDP进行异步通信的方法
2022-03-20 18:23:55
redisson特性及优雅实现示例
2022-02-13 19:51:25
Andriod studio 打包aar 的方法
2023-12-13 19:32:36
C语言连续生成多个随机数实现可限制范围
2023-08-24 08:35:06
![](https://img.aspxhome.com/file/2023/3/127553_0s.png)
使用vscode搭建javaweb项目的详细步骤
2022-08-05 01:12:43
![](https://img.aspxhome.com/file/2023/1/119101_0s.png)
Java毕业设计实战之在线高中考试系统的实现
2021-07-04 06:15:20
![](https://img.aspxhome.com/file/2023/3/71813_0s.jpg)
java扩展Hibernate注解支持java8新时间类型
2021-09-12 07:43:38
C#实现自定义打印文字和图片的示例代码
2021-05-30 02:35:20
![](https://img.aspxhome.com/file/2023/2/110172_0s.png)
OpenFeign设置header的三种方式总结
2023-06-25 19:03:46
![](https://img.aspxhome.com/file/2023/8/79338_0s.jpg)
Java数据结构之链表实现(单向、双向链表及链表反转)
2021-10-17 18:04:25
![](https://img.aspxhome.com/file/2023/9/128849_0s.png)
insert语句太长用StringBuilder优化一下
2023-03-29 17:56:44
java 同步、异步、阻塞和非阻塞分析
2022-08-09 03:02:42
![](https://img.aspxhome.com/file/2023/1/70321_0s.png)
浅谈图片上传利用request.getInputStream()获取文件流时遇到的问题
2023-10-18 10:36:43
![](https://img.aspxhome.com/file/2023/9/64029_0s.jpg)
SpringBoot+SpringSession+Redis实现session共享及唯一登录示例
2023-10-07 07:56:17
![](https://img.aspxhome.com/file/2023/4/71244_0s.png)
mybatis使用foreach查询不出结果也不报错的问题
2023-11-24 22:36:17
![](https://img.aspxhome.com/file/2023/6/60116_0s.png)
C#实现猜数字游戏
2021-11-16 07:29:58
![](https://img.aspxhome.com/file/2023/4/91374_0s.jpg)
SpringBoot+JPA 分页查询指定列并返回指定实体方式
2021-08-26 11:54:57
Android实现系统日历同步日程
2022-02-13 09:57:44
![](https://img.aspxhome.com/file/2023/2/90122_0s.jpg)