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

 分类:
图形图像(3) 

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

实现:

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

使用JavaCV/OpenCV抓取并存储摄像头图像的更多相关文章

  1. 摄像头脸部识别 (1)opencv 抓取视频数据并保存

    摄像头脸部识别 (1)opencv 抓取视频数据并保存 基于python 和 opencv 3.4.0 (兼容 opencv 2.X 参考注释),详细如代码 import numpy as np im ...

  2. 在python3下使用OpenCV 抓取摄像头图像并实时显示3色直方图

    以下代码为在Python3环境下利用OpenCV 抓取摄像头的实时图像, 通过OpenCV的 calHist函数计算直方图, 并显示在3个不同窗口中. import cv2 import numpy ...

  3. 【Python入门只需20分钟】从安装到数据抓取、存储原来这么简单

    基于大众对Python的大肆吹捧和赞赏,作为一名Java从业人员,我本着批判与好奇的心态买了本python方面的书<毫无障碍学Python>.仅仅看了书前面一小部分的我......决定做一 ...

  4. 在python3下使用OpenCV 抓取摄像头图像提取蓝色

    工作中需要对摄像头进行调试, Python平台大大提高调试效率. 从网找到段代码, 可以从摄像头图像中抠出蓝色. import cv2 import numpy as np cap  = cv2.Vi ...

  5. OSX下面用ffmpeg抓取桌面以及摄像头推流进行直播

    参考博客 http://blog.chinaunix.net/uid-11344913-id-4665455.html 在osx系统下通过ffmpeg查看设备 ffmpeg -f avfoundati ...

  6. Python 抓取数据存储到Redis中

    redis是一个key-value存储结构.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...

  7. 用ADB 抓取和存储APP日志方法

    前置条件:电脑已经安装好adb (一)  进入adb目录下连接手机,检测出手机 CD 到SDK的platform-tools (二)adb logcat-c  清除日志  (三)adb  logcat ...

  8. Python 抓取数据存储到Mysql中

    # -*- coding: utf-8 -*- import os,sys import requests import bs4 import pymysql#import MySQLdb #连接MY ...

  9. Zeta--S3 Linux抓取一帧YUV图像后使用硬件编码器编码成H.264

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <getopt.h&g ...

随机推荐

  1. 安装vb6 正在更新系统 无响应

    新装的win10系统,安装vb6时,最后一直卡在“正在更新系统”,程序无响应,没办法,kill掉后,貌似不影响软件使用,但是安装vs6sp6B无法成功安装. 解决办法是: 不安装“数据访问”组件. 参 ...

  2. sql语句的使用;

    1.导出数据库的语句: mysqldump -u root -p shop > d:\shop.sql

  3. 网络模块(net, http)小解

    net模块可用来创建TCP服务器来接收网络请求,它提供了创建客户端以及服务端的方法 服务端: 创建服务器: net.createServer([,options], connectionListene ...

  4. c语言结构体指针必须初始化

    先说结论 结构体指针需要初始化 结构体指针的成员指针同样需要初始化 结构体变量定义的时候就已经分配了内存空间,而上面两个确没有 struct test{ int i; struct buf *p;} ...

  5. HDU 5933/思维

    题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=5933]; 题意: 给出n堆物品,问能不能分成K个数量相等的堆,如果能分,则最少次数是多少.分的规则为: ...

  6. ubuntu server 时区设置问题解决

    1.当执行此命令的时候 ntpdate us.pool.ntp.org 出现一下错误提示 name server cannot be used: Temporary failure in name r ...

  7. android 获取系统硬件信息

    一,首先设置权限访问: <uses-permission android:name="android.permission.READ_PHONE_STATE" />  ...

  8. 解决安装WordPress主题及插件需要输入FTP问题

    http://www.zhanghenglei.com/wordpress-ftp-update/ 使用Wordpress程序架构的网站如果需要在网站后台升级.安装主题或者插件的时候,总是会提示需要我 ...

  9. SQLServer 错误: 15404,无法获取有关 Windows NT 组 用户

    因做服务器数据库迁移,造成的一系列问题, 1.重启SQL 代理,操作也不可. 然后又进行操作: SQL---------安全性------登录名 中原来的系统用户名前的计算机名更改为现在所用的计算机名 ...

  10. 调试MVC项目,不关闭 IIS EXPRESS

    在VS主面板打开:工具->选项->调试->编辑继续   取消选中[启用"编辑并继续"] 就OK了 (英文版的请对应相应的操作) 不过这是针对所有的调试,如果你想针 ...