2016-05-03 112 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