相机拍摄的图像在OpenCV中是全黑的

时间:2016-05-03 04:11:10

标签: java opencv

我是OpenCV的初学者 - Java并试图通过从笔记本电脑中的相机捕获图像来学习基础知识。我在eclipse中运行以下代码,我可以看到相机指示灯闪烁一秒钟,表明它确实启动了。但存储的图像是全黑的。

import org.opencv.core.*;
import org.opencv.videoio.VideoCapture;
import org.opencv.imgcodecs.*;

public class VideoCap {


    public static void main(String[] args){

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        VideoCapture camera = new VideoCapture(0);
        if(!camera.isOpened()) {
                    System.out.println("Erro in opening camera");
        }

        else {
            Mat frame = new Mat();
            while(true) {
                if(camera.read(frame)) {
                    System.out.println("Camera obtained");
                    System.out.println("Captured frame width" + frame.width()
                    + " catured frame height " + frame.height() );
                    Imgcodecs.imwrite("cam.jpg", frame);
                    break;                  
                }
            }
        }
        camera.release();
    }
}

控制台没有错误,可能出了什么问题?

1 个答案:

答案 0 :(得分:-1)

您必须更改以下代码:

import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;        
import org.opencv.videoio.VideoCapture;        

public class VideoCap {
    public static void main (String args[]){
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        VideoCapture camera = new VideoCapture(0);

        System.out.println("Welcome to OpenCV " + Core.VERSION);
        if(!camera.isOpened()){
            System.out.println("Error");
        }
        else {
            Mat frame = new Mat();
            camera.read(frame); 
            while(true){
                if (camera.read(frame)){
                    System.out.println("Frame Obtained");
                    System.out.println("Captured Frame Width " + 
                    frame.width() + " Height " + frame.height());
                    Imgcodecs.imwrite("c://capture/camera.jpg", frame);
                    System.out.println("OK");
                    break;
                }
            }   
        }
        camera.release();
    }
}