D-Link DCS-930LB的OpenCV视频捕捉

时间:2016-09-07 02:50:15

标签: java opencv video-capture ip-camera

我在从上述相机捕捉视频输入时遇到问题。当我使用我的内部webcamera时,代码工作正常。该代码来自here。我收到的错误消息与该链接中发布的错误消息相同。

更新

当我将链接http://usrname:pwd@ipaddress/video.cgi?.mjpg复制到浏览器中时,它可以正常工作并显示流。虽然,我无法使用Java中的相同URL访问相机。

我做了什么

我试图遵循这个LINK @ StackOverflow,但没有成功。我认为它可能会有所帮助,因为相机是相似的(我的意思是我根据示例改变了URL)。

我也尝试了this,其中相机似乎与我的相机相同。这也不起作用。

我的代码如下:

import java.io.IOException;
import javax.swing.JFrame;
import org.opencv.core.Core;
import org.opencv.videoio.VideoCapture;

public class OpenCVTest {

    public OpenCVTest() {

    // TODO Auto-generated constructor stub
    }

/**
 * @param args
 * @throws IOException
 */
public static void main(String[] args) throws IOException {

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    VideoCapture camera = new VideoCapture(
            "http://username:password@IP-address:8080/video.cgi?.mjpg");
    // VideoCapture camera = new VideoCapture(0);
    if (camera.isOpened()) {
        System.out.println("Video is captured");
    } else {
        System.out.println("");
    }
    VideoCamera cam = new VideoCamera(camera);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(cam);
    frame.setSize(800, 800);
    frame.setVisible(true);

    while (camera.isOpened()) {
        cam.repaint();

    }

   }

}

用户名替换为实际用户名,密码替换为实际密码,IP地址也是如此。

使用的另一个类如下。

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import javax.swing.JPanel;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;

@SuppressWarnings("serial")
public class VideoCamera extends JPanel {
    VideoCapture camera;

    public VideoCamera(VideoCapture cam) {

    camera = cam;

}

/**
 * @param args
 */
public static void main(String[] args) {

    // TODO Auto-generated method stub

}

public BufferedImage Mat2BufferedImage(Mat m) {

    int type = BufferedImage.TYPE_BYTE_GRAY;
    if (m.channels() > 1) {
        type = BufferedImage.TYPE_3BYTE_BGR;
    }
    int bufferSize = m.channels() * m.cols() * m.rows();
    byte[] b = new byte[bufferSize];
    m.get(0, 0, b); // get all the pixels
    BufferedImage img = new BufferedImage(m.cols(), m.rows(), type);
    final byte[] targetPixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
    System.arraycopy(b, 0, targetPixels, 0, b.length);
    return img;

}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Mat mat = new Mat();

    if (camera.read(mat)) {
        System.out.print("IMAGE");

    }

    BufferedImage image = Mat2BufferedImage(mat);
    // Mat gray = turnGray(mat);
    // MatOfRect objects = new MatOfRect();
    // CascadeClassifier cas = new CascadeClassifier();
    // cas.detectMultiScale(gray,objects);
    // Mat thresh = threash( gray);

    // BufferedImage image = Mat2BufferedImage(thresh);
    g.drawImage(image, 10, 10, image.getWidth(), image.getHeight(), null);

}

public Mat turnGray(Mat img)

{
    Mat mat1 = new Mat();
    Imgproc.cvtColor(img, mat1, Imgproc.COLOR_RGB2GRAY);
    return mat1;
}

public Mat threash(Mat img) {
    Mat threshed = new Mat();
    int SENSITIVITY_VALUE = 100;
    Imgproc.threshold(img, threshed, SENSITIVITY_VALUE, 255, Imgproc.THRESH_BINARY);
    return threshed;
   }

}

我不知道这是否重要,但如果删除我无法使用我的webcamera

if (camera.read(mat)) {
    System.out.print("IMAGE");
}
来自VideoCamera类的

。在我看来,即使我删除了打印输出,程序也应该运行。不过,我的问题是我希望它能用我的IP摄像机运行;我不太关心我的其他相机。

我很感激所有的帮助。

0 个答案:

没有答案