栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

详解使用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; 
 
 
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(); 
  } 
 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/146879.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号