2016-05-03 76 views
1

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

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

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

+0

是宽度和高度是否正确?你有没有尝试过'Imshow'(在'imwrite'之前),看看那里有什么? –

回答

0

你必须改变你的代码是这样的:

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(); 
    } 
} 
+0

解决黑框问题有什么区别? – nrofis