最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解使用JavaCV/OpenCV抓取并存儲(chǔ)攝像頭圖像

 更新時(shí)間:2017年04月10日 09:19:00   作者:ljsspace  
本篇文章主要介紹了使用JavaCV/OpenCV抓取并存儲(chǔ)攝像頭圖像,實(shí)例分析了使用JavaCV/OpenCV抓取并存儲(chǔ)攝像頭圖像的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本程序通過JFrame實(shí)時(shí)顯示本機(jī)攝像頭圖像,并將圖像存儲(chǔ)到一個(gè)緩沖區(qū),當(dāng)用戶用鼠標(biāo)點(diǎn)擊JFrame中任何區(qū)域時(shí),顯示抓取圖像的簡(jiǎn)單動(dòng)畫,同時(shí)保存緩沖區(qū)的圖像到磁盤文件中。點(diǎn)擊JFrame關(guān)閉按鈕可以退出程序。

實(shí)現(xiàn):

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(); 
  } 
 
} 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

荔浦县| 江孜县| 建始县| 安新县| 报价| 托克托县| 蒙山县| 延津县| 桃园县| 泰顺县| 陆丰市| 武宁县| 视频| 蒙阴县| 金堂县| 阳西县| 永仁县| 镇原县| 牟定县| 西安市| 潮安县| 济阳县| 洪洞县| 天门市| 罗源县| 喀喇| 岑溪市| 泊头市| 蒲城县| 镇赉县| 峨边| 郧西县| 云林县| 巴中市| 宿松县| 沙坪坝区| 土默特左旗| 务川| 涞源县| 河南省| 桂阳县|