今天就跟大家聊聊有关利用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; 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(); } }
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
软考中级精品资料免费领
- 历年真题答案解析
- 备考技巧名师总结
- 高频考点精准押题
- 资料下载
- 历年真题
193.9 KB下载数265
191.63 KB下载数245
143.91 KB下载数1148
183.71 KB下载数642
644.84 KB下载数2756
相关文章
发现更多好内容猜你喜欢
AI推送时光机利用JavaCV如何实现抓取并保存摄像头的影像
后端开发2023-05-31
Python利用VideoCapture读取视频或摄像头并进行保存
后端开发2024-04-02
C#+EmguCV怎么实现使用摄像头读取、保存视频
后端开发2023-06-29
Qt6如何实现调用摄像头并显示画面
后端开发2023-07-05
Html5中如何调用手机摄像头并实现人脸识别
后端开发2023-06-09
利用Java如何实现爬取网络图片并保存
后端开发2023-05-31
咦!没有更多了?去看看其它编程学习网 内容吧