Search code examples
javajavacv

Capture video first frame file in java


I'm trying to get the first frame of video file. I've base my code on some example I found on the web and I don't understand what is wrong in the coding.

Thanks very much for your helps.

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);
        }
    } 
}

ERROR Message

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


Solution

  • I've corrected the import and the code like this, and it works well:

    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);
            }
        }
    }