Capture video from d-link DCS-930LB

asked 2016-09-07 10:51:48 -0500

SniFFzoR gravatar image

Hi,

I have a problem when I'm trying to capture video from my IP-camera, mentioned in the topic.

I have tried to solve it using the tips and examples displayed here, as well as the advice given here, but nothing seems to work.

My code, originally taken from this example, looks as follows.

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 ip_camera = new VideoCapture("http://username:password@ip-address/video.cgi?x.mjpeg");
    //ip_camera.open("http://username:password@ip-address/video.cgi?x.mjpeg");

    // VideoCapture camera = new VideoCapture(0);
    if (ip_camera.isOpened()) {
        System.out.println("Video is captured");
    } else {
        System.out.println("");
    }
    VideoCamera cam = new VideoCamera(ip_camera);

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

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

    }

}

}

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

}

It is worth mentioning that when I try the URL passed to the VideoCapture object in my browser, I get access to the camera feed.

My error message looks like this.

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Width (0) and height (0) must be > 0 at java.awt.image.SampleModel.<init>(SampleModel.java:126) at java.awt.image.ComponentSampleModel.<init>(ComponentSampleModel.java:146) at java.awt.image.PixelInterleavedSampleModel.<init>(PixelInterleavedSampleModel.java:87) at java.awt.image.Raster.createInterleavedRaster(Raster.java:641) at java.awt.image.Raster.createInterleavedRaster(Raster.java:278) at java.awt.image.Raster.createInterleavedRaster(Raster.java:212) at java.awt.image.ComponentColorModel.createCompatibleWritableRaster(ComponentColorModel.java:2825) at java.awt.image ... (more)

edit retag flag offensive close merge delete

Comments

I think that the problem is within my internet settings, maybe the router. Does anybody else think that the settings could matter, making it impossible to see the feed?

SniFFzoR gravatar imageSniFFzoR ( 2016-09-30 06:57:17 -0500 )edit