用java来调用OpenCV (二)

来源:互联网 发布:大数据行业工资 编辑:程序博客网 时间:2024/06/17 21:26

下面这个程序是实现每播放一秒视频就截取一张图片保存到本地的操作。

import org.opencv.core.Core;import org.opencv.core.Mat;import org.opencv.core.MatOfRect;import org.opencv.core.Point;import org.opencv.core.Rect;import org.opencv.core.Scalar;import org.opencv.highgui.Highgui;import org.opencv.highgui.VideoCapture;import org.opencv.objdetect.CascadeClassifier;import com.googlecode.javacv.cpp.opencv_highgui;//这个对象就只有在javacv里才有的


public static void main(String[] args) {// System.out.println("Welcome to OpenCV " + Core.VERSION);// System.loadLibrary(Core.NATIVE_LIBRARY_NAME);// Mat m = Mat.eye(3, 3, CvType.CV_8UC1);// System.out.println("m = " + m.dump());//加载本地的OpenCV库,这样就可以用它来调用Java APISystem.loadLibrary(Core.NATIVE_LIBRARY_NAME);  Test t = new Test();t.run2();}

public void run2(){//读取视频文件VideoCapture cap = new VideoCapture("D:/testFile.mp4");System.out.println(cap.isOpened());//判断视频是否打开if(cap.isOpened()){//总帧数double frameCount = cap.get(opencv_highgui.CV_CAP_PROP_FRAME_COUNT);System.out.println(frameCount);//帧率double fps = cap.get(opencv_highgui.CV_CAP_PROP_FPS);System.out.println(fps);//时间长度double len = frameCount / fps; System.out.println(len);Double d_s=new Double(len);System.out.println(d_s.intValue());Mat frame = new Mat();for(int i=0;i<d_s.intValue();i++){//设置视频的位置(单位:毫秒)cap.set(opencv_highgui.CV_CAP_PROP_POS_MSEC ,i*1000);//读取下一帧画面if(cap.read(frame)){//保存画面到本地目录Highgui.imwrite("d:/img/"+i+".jpg", frame);}}//关闭视频文件cap.release();}}


2 0
原创粉丝点击