2014-12-18 67 views
0

我試圖用openCV連接一個D-Link相機型號DCS-932L。我認爲問題在於URL格式。 IP,端口,密碼和用戶都是正確的。OpenCV Java連接DCS-932L

這是我的代碼:

public static void main(String args[]) throws Exception { 

    String end = "http://USER:[email protected]:80/mjpeg.cgi?user=USERNAME&password=PWD&channel=0&.mjpg"; 


    ContainerImagem toc = new ContainerImagem(); 

    Button button = new Button(); 
    button.addActionListener(null); 

    JFrame frame = new JFrame("Captura de face"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(600, 600); 
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 
    CascadeClassifier faceDetector = new CascadeClassifier("C:\\Users\\leonardo\\Documents\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml"); 

    toc.setSize(2000, 2000); 

    frame.add(toc); 
    frame.setVisible(true); 
    Mat webcam_image = new Mat(); 

    MatToBufImg mat2Buf = new MatToBufImg(); 
    VideoCapture capture = new VideoCapture(); 

    capture.open(end); 

    if (capture.isOpened()) { 
     Thread.sleep(10000); 
     while (true) { 
      capture.read(webcam_image); 
      if (!webcam_image.empty()) { 
       frame.setSize(webcam_image.width(), webcam_image.height()); 
       MatOfRect faceDetections = new MatOfRect(); 
       faceDetector.detectMultiScale(webcam_image, faceDetections); 
       for (Rect rect : faceDetections.toArray()) { 
        Core.rectangle(webcam_image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));// mat2Buf, mat2Buf); 
       } 
       // System.out.println("...............face detected: " + faceDetections.toArray().length); 

       if (faceDetections.toArray().length == 0) { 
        // System.out.println("Sorry Face not detected!"); 
       } 
       mat2Buf.setMatrix(webcam_image, ".jpg"); 
       toc.setImage(mat2Buf.getBufferedImage()); 
       toc.repaint(); 
      } else { 
       System.out.println("problems with webcam image capture"); 
       break; 
      } 
     } 
    } else { 
     JOptionPane.showMessageDialog(null, "O sistema não conseguiu se conectar com esta câmera."); 
    } 
    capture.release(); 
} 

回答

0

我在這個論壇裏面OpenCV的不正確地流MJPG格式的作品中。在本次論壇還,我發現了一個代碼JavaCV使用,而這個工作對我罰款:

public static void main(String[] args) throws Exception { 

OpenCVFrameGrabber grabber = new OpenCVFrameGrabber("http://USER:[email protected]:80/mjpeg.cgi?user=USER&password=PASSWORD&channel=0&.mjpg"); 
grabber.setFormat("mjpeg"); 
grabber.start(); 

IplImage frame = grabber.grab(); 
CanvasFrame canvasFrame = new CanvasFrame("Camera"); 
canvasFrame.setCanvasSize(frame.width(), frame.height()); 
while (canvasFrame.isVisible() && (frame = grabber.grab()) != null) { 
    canvasFrame.showImage(frame); 
} 
grabber.stop(); 
canvasFrame.dispose(); 
System.exit(0); 

}