I am having problem to capture the video feed from the camera mentioned above. The code works fine when I'm using my internal webcamera. The code is grabbed from here. The error messages that I receive are the same as posted in that link as well.
Update
When I copy the link http://usrname:pwd@ipaddress/video.cgi?.mjpg into my browser, it works fine and the stream is displayed. Although, I cannot access the camera using the same URL in Java.
What I have done
I have tried to follow this LINK @ StackOverflow, but no success there. I thought it might help since the camera is similar (I mean that I alter the URL according to the example).
I also tried this, where the camera seems to be the same as the one I have. This does not work either.
My code is the following:
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();
}
}
}
The username is replaced with the actual username and the password is replaced with the actual password, and so is the IP-address.
The other class used is as follows.
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;
}
}
I don't know if it matters, but I cannot get it to work with my webcamera if I remove
if (camera.read(mat)) {
System.out.print("IMAGE");
}
from the VideoCamera class. It seems to me that the program should run even if I remove a print out. Nevertheless, my problem is that I want it to run with my IP-camera; I don't really care about my other cameras.
I am greatful for all help.