亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

利用JavaCV如何實現抓取并保存攝像頭的影像

發布時間:2020-11-16 16:25:33 來源:億速云 閱讀:504 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關利用JavaCV如何實現抓取并保存攝像頭的影像,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

實現:

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如何實現抓取并保存攝像頭的影像有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

会宁县| 南充市| 黄平县| 鄯善县| 桃园市| 临颍县| 宕昌县| 沛县| 堆龙德庆县| 利津县| 壤塘县| 临猗县| 旺苍县| 阆中市| 比如县| 九龙县| 彰化县| 伊宁市| 襄汾县| 永年县| 五常市| 望奎县| 本溪市| 宁河县| 新丰县| 大宁县| 若羌县| 雷山县| 德清县| 襄垣县| 弥渡县| 兴安盟| 剑阁县| 朝阳区| 宜章县| 阿荣旗| 阳江市| 迁安市| 缙云县| 武夷山市| 墨脱县|