詳解使用JavaCV/OpenCV抓取并存儲(chǔ)攝像頭圖像
本程序通過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();
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Java數(shù)組的一維和二維講解和內(nèi)存顯示圖
這篇文章主要介紹了Java數(shù)組的一維和二維講解和內(nèi)存顯示圖,數(shù)組就相當(dāng)于一個(gè)容器,存放相同類型數(shù)據(jù)的容器。而數(shù)組的本質(zhì)上就是讓我們能 "批量" 創(chuàng)建相同類型的變量,需要的朋友可以參考下2023-05-05
詳解如何利用jasypt實(shí)現(xiàn)配置文件加密
Jasypt?(Java?Simplified?Encryption)?是一個(gè)?java?庫,它允許開發(fā)人員以最小的成本將基本的加密功能添加到項(xiàng)目中,而無需深入了解密碼學(xué)的工作原理。本文將利用jasypt實(shí)現(xiàn)配置文件加密,感興趣的可以學(xué)習(xí)一下2022-07-07
NoHttpResponseException問題排查解決記錄分析
這篇文章主要為大家介紹了NoHttpResponseException問題排查解決記錄分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
java SelectableChannel的使實(shí)例用法講解
在本篇文章里小編給大家整理的是一篇關(guān)于java SelectableChannel的使實(shí)例用法講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-03-03
使用GenericObjectPool避免泄漏設(shè)置方法
這篇文章主要為大家介紹了使用GenericObjectPool避免泄漏的設(shè)置方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
SpringBoot實(shí)現(xiàn)事件監(jiān)聽(異步執(zhí)行)的示例代碼
事件監(jiān)聽是一種機(jī)制,可以定義和觸發(fā)自定義的事件,以及在應(yīng)用程序中注冊(cè)監(jiān)聽器來響應(yīng)這些事件,本文主要介紹了SpringBoot實(shí)現(xiàn)事件監(jiān)聽(異步執(zhí)行)的示例代碼,感興趣的可以了解一下2024-08-08
ArrayList源碼探秘之Java動(dòng)態(tài)數(shù)組的實(shí)現(xiàn)
這篇文章將帶大家從ArrayList源碼來探秘一下Java動(dòng)態(tài)數(shù)組的實(shí)現(xiàn),文中的示例代碼講解詳細(xì),對(duì)我們深入了解JavaScript有一定的幫助,需要的可以參考一下2023-08-08

