詳解使用JavaCV/OpenCV抓取并存儲攝像頭圖像
本程序通過JFrame實(shí)時顯示本機(jī)攝像頭圖像,并將圖像存儲到一個緩沖區(qū),當(dāng)用戶用鼠標(biāo)點(diǎn)擊JFrame中任何區(qū)域時,顯示抓取圖像的簡單動畫,同時保存緩沖區(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring MVC 中攔截器的使用示例詳解"攔截器基本配置"和 &q
Spring MVC 的攔截器作用是在請求到達(dá)控制器之前或之后進(jìn)行攔截,可以對請求和響應(yīng)進(jìn)行一些特定的處理,這篇文章主要介紹了Spring MVC 中的攔截器的使用“攔截器基本配置” 和 “攔截器高級配置”,需要的朋友可以參考下2024-07-07
Java設(shè)計(jì)模式之觀察者模式_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章給大家介紹流量java設(shè)計(jì)模式之觀察者模式,定義對象間一種一對多的依賴關(guān)系,使得當(dāng)每一個對象改變狀態(tài)。下面通過類圖和實(shí)例代碼給大家介紹java設(shè)計(jì)模式之觀察者模式,感興趣的朋友一起看看吧2017-08-08
服務(wù)性能優(yōu)化之mybatis-plus開啟與關(guān)閉SQL日志打印方法
這篇文章主要介紹了在Mybatis-plus中開啟和關(guān)閉控制臺SQL日志打印,在`application.properties`文件中,可以通過配置來實(shí)現(xiàn)SQL日志的開啟和關(guān)閉,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-12-12
Java設(shè)計(jì)模式之Adapter適配器模式
這篇文章主要為大家詳細(xì)介紹了Java設(shè)計(jì)模式之Adapter適配器模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Java中Spring使用Quartz任務(wù)調(diào)度定時器
本篇文章主要介紹了Java中Spring使用Quartz任務(wù)調(diào)度定時器,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-02-02
SWT(JFace) FTP客戶端實(shí)現(xiàn)
SWT(JFace)小制作:FTP客戶端實(shí)現(xiàn)2009-06-06
如何在MyBatis中實(shí)現(xiàn)DataSource
今天給大家整理了如何在MyBatis中實(shí)現(xiàn)DataSource,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下2021-06-06

