如何在java中使用Opencv加载视频文件

h6my8fg2  于 2023-06-28  发布在  Java
关注(0)|答案(2)|浏览(85)

我试图使用opencv来加载一个视频文件在Java中使用netbeans。有人知道怎么做吗?我在网上找不到解决方案。

e4yzc0pl

e4yzc0pl1#

您需要将C:\opencv\build\x86\vc11\bin添加到Path变量...以及写入:

private void myMethod(...) {
    Mat frame = new Mat();
    VideoCapture camera = new VideoCapture("C:/MyName/MyPc/Desktop/TheLordOfTheRings.mp4");
    JFrame jframe = new JFrame("MyTitle");
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel vidpanel = new JLabel();
    jframe.setContentPane(vidpanel);
    jframe.setVisible(true);

    while (true) {
        if (camera.read(frame)) {

            ImageIcon image = new ImageIcon(Mat2bufferedImage(frame));
            vidpanel.setIcon(image);
            vidpanel.repaint();

        }
    }
}
cwxwcias

cwxwcias2#

要在Java中使用OpenCV加载视频文件,您可以按照以下步骤操作:确保在Java项目中正确安装和配置了OpenCV。您可以参考OpenCV文档了解安装说明。

import java.io.File;
    import org.opencv.core.Mat;
    import org.opencv.highgui.HighGui;
    import org.opencv.videoio.VideoCapture;

public class VideoLoaderExample {
    public static void main(String[] args) {
        File lib = null;
        String os = System.getProperty("os.name");
        String bitness = System.getProperty("sun.arch.data.model");

        if (os.toUpperCase().contains("WINDOWS")) {
            if (bitness.endsWith("64")) {
                lib = new File("D:\\opencv\\build\\java\\x64\\" + System.mapLibraryName("opencv_java401"));
            } else {
                lib = new File("D:\\opencv\\build\\java\\x86\\" + System.mapLibraryName("opencv_java401"));
            }
        }

        System.out.println(lib.getAbsolutePath());
        System.load(lib.getAbsolutePath());

        VideoCapture videoCapture = new VideoCapture();
        // Replace with the actual path to your video file
        videoCapture.open("C:\\Users\\devel\\Downloads\\WhatsApp Video.mp4");

        // Check if the video file was successfully opened
        if (!videoCapture.isOpened()) {
            System.out.println("Error opening video file");
            return;
        }

        // Read frames from the video file and display them
        Mat frame = new Mat();
        while (videoCapture.read(frame)) {
            // Display the frame (you can replace this with your own processing logic)
            HighGui.imshow("Video", frame);
            HighGui.waitKey(25); // Delay between frames (adjust as needed)
        }

        // Release resources and close the video file
        videoCapture.release();
        HighGui.destroyAllWindows();
    }
}

相关问题