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.BufferedImage.<init>(BufferedImage.java:428) at VideoCamera.Mat2BufferedImage(VideoCamera.java:37) at VideoCamera.paintComponent(VideoCamera.java:53) at javax.swing.JComponent.paint(JComponent.java:1053) at javax.swing.JComponent.paintChildren(JComponent.java:886) at javax.swing.JComponent.paint(JComponent.java:1062) at javax.swing.JComponent.paintChildren(JComponent.java:886) at javax.swing.JComponent.paint(JComponent.java:1062) at javax.swing.JLayeredPane.paint(JLayeredPane.java:586) at javax.swing.JComponent.paintChildren(JComponent.java:886) at javax.swing.JComponent.paintToOffscreen(JComponent.java:5230) at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1572) at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1495) at javax.swing.RepaintManager.paint(RepaintManager.java:1265) at javax.swing.JComponent.paint(JComponent.java:1039) at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39) at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:79) at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:116) at java.awt.Container.paint(Container.java:1973) at java.awt.Window.paint(Window.java:3901) at javax.swing.RepaintManager$4.run(RepaintManager.java:835) at javax.swing.RepaintManager$4.run(RepaintManager.java:807) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:807) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:782) at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:731) at javax.swing.RepaintManager.access$1300(RepaintManager.java:64) at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1720) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744) at java.awt.EventQueue.access$400(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:697) at java.awt.EventQueue$3.run(EventQueue.java:691) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:714) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
I am sorry the post got so long, but I figured it is better to as meticulous as possible.
/S