详解使用JavaCV/OpenCV抓取并存储摄像头图像
本程序通过JFrame实时显示本机摄像头图像,并将图像存储到一个缓冲区,当用户用鼠标点击JFrame中任何区域时,显示抓取图像的简单动画,同时保存缓冲区的图像到磁盘文件中。点击JFrame关闭按钮可以退出程序。
实现:
importjava.awt.Graphics2D; importjava.awt.event.ActionEvent; importjava.awt.event.ActionListener; importjava.awt.event.MouseAdapter; importjava.awt.event.MouseEvent; importjava.awt.image.BufferedImage; importjava.io.File; importjava.io.IOException; importjavax.imageio.ImageIO; importjavax.swing.Timer; importcom.googlecode.javacv.CanvasFrame; importcom.googlecode.javacv.OpenCVFrameGrabber; importcom.googlecode.javacv.cpp.opencv_core.IplImage; importstaticcom.googlecode.javacv.cpp.opencv_core.cvReleaseImage; /** * *UseJavaCV/OpenCVtocapturecameraimages * *Therearetwofunctionsinthisdemo: *1)showreal-timecameraimages *2)capturecameraimagesbymouse-clickinganywhereintheJFrame, *thejpgfileissavedinahard-codedpath. * *@authorljs *2011-08-19 * */ publicclassCameraCapture{ publicstaticStringsavedImageFile="c:\\tmp\\my.jpg"; //timerforimagecaptureanimation staticclassTimerActionimplementsActionListener{ privateGraphics2Dg; privateCanvasFramecanvasFrame; privateintwidth,height; privateintdelta=10; privateintcount=0; privateTimertimer; publicvoidsetTimer(Timertimer){ this.timer=timer; } publicTimerAction(CanvasFramecanvasFrame){ this.g=(Graphics2D)canvasFrame.getCanvas().getGraphics(); this.canvasFrame=canvasFrame; this.width=canvasFrame.getCanvas().getWidth(); this.height=canvasFrame.getCanvas().getHeight(); } publicvoidactionPerformed(ActionEvente){ intoffset=delta*count; if(width-offset>=offset&&height-offset>=offset){ g.drawRect(offset,offset,width-2*offset,height-2*offset); canvasFrame.repaint(); count++; }else{ //whenanimationisdone,resetcountandstoptimer. timer.stop(); count=0; } } } publicstaticvoidmain(String[]args)throwsException{ //opencamerasource OpenCVFrameGrabbergrabber=newOpenCVFrameGrabber(0); grabber.start(); //createaframeforreal-timeimagedisplay CanvasFramecanvasFrame=newCanvasFrame("Camera"); IplImageimage=grabber.grab(); intwidth=image.width(); intheight=image.height(); canvasFrame.setCanvasSize(width,height); //onscreenbufferforimagecapture finalBufferedImagebImage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics2DbGraphics=bImage.createGraphics(); //animationtimer TimerActiontimerAction=newTimerAction(canvasFrame); finalTimertimer=newTimer(10,timerAction); timerAction.setTimer(timer); //clicktheframetocaptureanimage canvasFrame.getCanvas().addMouseListener(newMouseAdapter(){ publicvoidmouseClicked(MouseEvente){ timer.start();//startanimation try{ ImageIO.write(bImage,"jpg",newFile(savedImageFile)); }catch(IOExceptione1){ e1.printStackTrace(); } } }); //real-timeimagedisplay while(canvasFrame.isVisible()&&(image=grabber.grab())!=null){ if(!timer.isRunning()){//whenanimationison,pausereal-timedisplay canvasFrame.showImage(image); //drawtheonscreenimagesimutaneously bGraphics.drawImage(image.getBufferedImage(),null,0,0); } } //releaseresources cvReleaseImage(image); grabber.stop(); canvasFrame.dispose(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。