簡體   English   中英

Java 中帶有 OpenCv 的 IP 攝像機

[英]IP camera with OpenCv in Java

我正在嘗試從 IP 攝像機獲取並顯示視頻流。 我在這里找到了一些示例代碼: http : //answers.opencv.org/question/24012/reading-video-stream-from-ip-camera-in-opencv-java/

但它對我不起作用。 當我使用內部網絡攝像頭時,我的代碼有效,但是當我將攝像頭切換到 IP 時,它不起作用。 我不知道為什么。 有人可以幫幫我嗎? 這是我的代碼:

    import java.awt.BorderLayout;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.awt.image.DataBufferByte;
    import java.io.File;
    import java.io.IOException;

    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    import org.opencv.core.Core;
    import org.opencv.core.CvType;
    import org.opencv.core.Mat;
    import org.opencv.highgui.VideoCapture;
    import org.opencv.imgproc.Imgproc;

    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://192.168.0.7/image.jpg");
        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();


        }

    }



      }


import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.VideoCapture;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.core.*;



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


}

感謝您的幫助。 我能夠使用你所說的找到正確的 IP 地址(http://192.168.0.6/VIDEO.CGI) (是的,我知道 7 變成了 6,它是一個不同的相機)在我的瀏覽器中獲取視頻. 但我仍然收到以下錯誤。

 Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Width (0) and height (0) must be > 0
        at java.awt.image.SampleModel.<init>(Unknown Source)
        at java.awt.image.ComponentSampleModel.<init>(Unknown Source)
        at java.awt.image.PixelInterleavedSampleModel.<init>(Unknown Source)
        at java.awt.image.Raster.createInterleavedRaster(Unknown Source)
        at java.awt.image.Raster.createInterleavedRaster(Unknown Source)
        at java.awt.image.Raster.createInterleavedRaster(Unknown Source)
        at java.awt.image.ComponentColorModel.createCompatibleWritableRaster(Unknown Source)
        at java.awt.image.BufferedImage.<init>(Unknown Source)
        at videoCamera.Mat2BufferedImage(videoCamera.java:54)
        at videoCamera.paintComponent(videoCamera.java:74)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JLayeredPane.paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paintToOffscreen(Unknown Source)
        at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
        at javax.swing.RepaintManager.paint(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
        at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
        at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
        at java.awt.Container.paint(Unknown Source)
        at java.awt.Window.paint(Unknown Source)
        at javax.swing.RepaintManager$3.run(Unknown Source)
        at javax.swing.RepaintManager$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.access$1100(Unknown Source)
        at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
        at java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$200(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

我認為這是因為 VideoCapture 沒有正確抓取幀。

這是我嘗試使用的相機http://www.trendnet.com/products/proddetail.asp?prod=150_TV-IP100W-N

.jpg是指單個圖像文件,而.mjpg可以訪問視頻流。 定義用於連接的 IP 和端口很重要。

根據您擁有的設備和相機 Web 界面中定義的設置,URL 會有所不同:

VideoCapture camera = new VideoCapture("http://192.168.0.7:8080/?dummy=param.mjpg");

VideoCapture camera = new VideoCapture("http://192.168.0.7:8080/mjpeg.cgi");

VideoCapture camera = new VideoCapture("http://192.168.0.7:8080/mjpg/mjpeg.cgi");

VideoCapture camera = new VideoCapture("http://192.168.0.7:8080/video.mjpeg");

VideoCapture camera = new VideoCapture("http://192.168.0.7:8080/video.cgi?.mjpg");

當您通過瀏覽器訪問有效 URL 時,它應該顯示視頻流。 找到有效的地址后,只需將其傳遞給VideoCapture構造函數即可。 在這個例子中,我展示了如何通過 HTTP 訪問流,但也支持 RTSP 協議。

經過 3 到 4 周的努力,我找到了一個 100% 可行的解決方案

首先你必須動態加載ffmpeg的dll文件,即使用

System.loadLibrary("[NAME OF YOUR DLL FILE]") You can find the required dll file in **opencv/build/x64/vc11/bin**在我的例子中 DLL 的名稱是“opencv_ffmpeg2413_64.dll”復制文件到項目的默認路徑並使用

System.loadLibrary("opencv_ffmpeg2413_64");//You May have different File Name Depending on the Version of OpenCV Installed on your Computer

然后你可以簡單地使用

VideoCapture ipcamera = new VideoCapture("[RTSP URL OF THE IP Camera]")//I used this Demo Link (rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM