使用JavaCV/OpenCV抓取并存储摄像头图像
http://blog.csdn.net/ljsspace/article/details/6702178


版权声明:本文为博主原创文章,未经博主允许不得转载。
本程序通过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();
- }
- }
使用JavaCV/OpenCV抓取并存储摄像头图像的更多相关文章
- 摄像头脸部识别 (1)opencv 抓取视频数据并保存
摄像头脸部识别 (1)opencv 抓取视频数据并保存 基于python 和 opencv 3.4.0 (兼容 opencv 2.X 参考注释),详细如代码 import numpy as np im ...
- 在python3下使用OpenCV 抓取摄像头图像并实时显示3色直方图
以下代码为在Python3环境下利用OpenCV 抓取摄像头的实时图像, 通过OpenCV的 calHist函数计算直方图, 并显示在3个不同窗口中. import cv2 import numpy ...
- 【Python入门只需20分钟】从安装到数据抓取、存储原来这么简单
基于大众对Python的大肆吹捧和赞赏,作为一名Java从业人员,我本着批判与好奇的心态买了本python方面的书<毫无障碍学Python>.仅仅看了书前面一小部分的我......决定做一 ...
- 在python3下使用OpenCV 抓取摄像头图像提取蓝色
工作中需要对摄像头进行调试, Python平台大大提高调试效率. 从网找到段代码, 可以从摄像头图像中抠出蓝色. import cv2 import numpy as np cap = cv2.Vi ...
- OSX下面用ffmpeg抓取桌面以及摄像头推流进行直播
参考博客 http://blog.chinaunix.net/uid-11344913-id-4665455.html 在osx系统下通过ffmpeg查看设备 ffmpeg -f avfoundati ...
- Python 抓取数据存储到Redis中
redis是一个key-value存储结构.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...
- 用ADB 抓取和存储APP日志方法
前置条件:电脑已经安装好adb (一) 进入adb目录下连接手机,检测出手机 CD 到SDK的platform-tools (二)adb logcat-c 清除日志 (三)adb logcat ...
- Python 抓取数据存储到Mysql中
# -*- coding: utf-8 -*- import os,sys import requests import bs4 import pymysql#import MySQLdb #连接MY ...
- Zeta--S3 Linux抓取一帧YUV图像后使用硬件编码器编码成H.264
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <getopt.h&g ...
随机推荐
- CSV文件解析工具
package com.common.util; import java.io.BufferedReader; import java.io.FileInputStream; import java. ...
- hosts文件失效,配置的域名不能成功访问
在windows7系统 hosts文件中配置域名 出现hosts文件失效 并不能正常访问域名 可能并不适用所有的这样的情况 但是我自己修改了dns的服务器的状态 就可以正常访问 如下图所示
- install font
哪些字体包含国际音标呢? 在微软的Windows与Office的2000或以上版本中分别带有Lucida Sans Unicode和Arial Unicode MS两种字体(以下分别简称LSU和AUM ...
- TextView赋值int型,并显示
textview赋值int型采用text.setText(FPS+""); FPS为int型变量 或者在thread线程需要在主Activity中显示文字,可以调用: runOnU ...
- redis2--redis安装
(1)安装Redis Redis目前只支持Linux系统,因为开发此软件的创始者认为,Redis是为后台数据服务的,所以认为该软件使用在纯净的服务环境下,而不是应用型操作系统下,而Linux作为服务器 ...
- bootstrap开始咯
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- [WPF] 浏览百度地图并获取经纬度地址信息
项目中需要利用登记的区域和地址在百度地图上定位,并获取该地址的经纬度. 本次功能对我来说主要难点如下:1.百度地图API的基本使用方法,请首选使用百度地图的JavaScript大众版(PS:之前使用W ...
- Orace内置函数大全[转:http://www.cnblogs.com/lfx0692/articles/2395950.html]
NewProgramer Oracle SQL 内置函数大全(转) SQL中的单记录函数 1.ASCII 返回与指定的字符对应的十进制数;SQL> select ascii('A') A,a ...
- delete、truncate与drop的区别
转自:SQL truncate .delete与drop区别 相同点: 1.truncate和不带where子句的delete.以及drop都会删除表内的数据. 2.drop.truncate都是DD ...
- Android Studio 如何将包名按层级展示
在project视图右上角有个“设置”的按钮,点开,然后将上图所圈部分去勾选就可以了.