在 java 中捕获视频第一帧文件

Capture video first frame file in java

我正在尝试获取视频文件的第一帧。 我的代码基于我在网上找到的一些示例,但我不明白编码中有什么问题。

非常感谢您的帮助。

JAVA Class:

import org.bytedeco.javacv.*;
import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.indexer.*;
import org.bytedeco.opencv.opencv_core.*;
import org.bytedeco.opencv.opencv_videoio.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.videoio.Videoio;

import static org.bytedeco.opencv.global.opencv_core.*;
import static org.bytedeco.opencv.global.opencv_imgproc.*;
import static org.bytedeco.opencv.global.opencv_videoio.*;
import static org.bytedeco.opencv.global.opencv_imgcodecs.*;


public class VideoFrameCapture {

    public static void main(String[] args) {

        int initFrames;
        Mat frame = new Mat();          

        VideoCapture capture = new VideoCapture("C:\TMP\video\pub3.mp4");

        capture.set(Videoio.CAP_PROP_FRAME_WIDTH,320);
        capture.set(Videoio.CAP_PROP_FRAME_HEIGHT,240);

        initFrames = (int) capture.get(1);

        if (capture.isOpened()) {
            capture.read(frame);
        }

        if (frame != null) {
            Imgcodecs.imwrite("C:\TMP\video\cover.jpg", frame, CV_16U);
        }
    } 
}

错误信息

The method imwrite(String, Mat, MatOfInt) in the type Imgcodecs is not applicable for the arguments (String, Mat, int)

我已经像这样更正了导入和代码,并且效果很好:

import org.bytedeco.opencv.opencv_core.*;
import org.bytedeco.opencv.opencv_videoio.*;

import static org.bytedeco.opencv.global.opencv_core.*;
import static org.bytedeco.opencv.global.opencv_imgcodecs.*;


public class VideoFrameCapture {

    public static void main(String[] args) {
        int height = 240;
        int width = 320;

        Mat frame = new Mat(height, width, CV_8UC1);

        VideoCapture capture = new VideoCapture("C:\TMP\video\pub3.mp4");

        if (capture.isOpened()) {
            capture.read(frame);
        }

        if (frame != null) {
            imwrite("C:\TMP\video\cover.jpg", frame);
        }
    }
}