Java OpenCV如何从HTTP URL捕获视频

时间:2017-12-21 09:24:24

标签: java opencv url video-streaming video-capture

我的小机器人捕获视频,但没有足够的性能来在本地处理图像文件。我已经获得了一些简单的视频流代码,我可以在桌面Chrome浏览器中远程查看。如下所示,机器人上的“服务器代码”工作正常。

public class HttpStreamOpenCV {
    public static void main(String[] args) throws Exception {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat mat = new Mat();
        VideoCapture vid = new VideoCapture(0);
        vid.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, 160);
        vid.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, 120);
        vid.open(0);
        System.out.println("Camera open");

        ServerSocket ss = new ServerSocket(8080);
        Socket sock = ss.accept();
        System.out.println("Socket connected");
        String boundary = "Thats it folks!";
        writeHeader(sock.getOutputStream(), boundary);
        System.out.println("Written header");

        long stime = System.currentTimeMillis();
        int cnt = 0;
        while (Button.ESCAPE.isUp()) {
            vid.read(mat);
            if (!mat.empty()) {
                writeJpg(sock.getOutputStream(), mat, boundary);
                System.out.println("Written jpg");
                if (cnt++ >= 100)
                {
                    long stop = System.currentTimeMillis();
                    System.out.println("Frame rate: " + (cnt*1000/(stop - stime)));
                    cnt = 0;
                    stime = stop;
                }
            } else  System.out.println("No picture");
        }
        sock.close();
        ss.close();    
    }

    private static void writeHeader(OutputStream stream, String boundary) throws IOException {
        stream.write(("HTTP/1.0 200 OK\r\n" +
            "Connection: close\r\n" +
            "Max-Age: 0\r\n" +
            "Expires: 0\r\n" +
            "Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0\r\n" +
            "Pragma: no-cache\r\n" + 
            "Content-Type: multipart/x-mixed-replace; " +
            "boundary=" + boundary + "\r\n" +
            "\r\n" +
            "--" + boundary + "\r\n").getBytes());
    }

    private static void writeJpg(OutputStream stream, Mat img, String boundary) throws IOException {
        MatOfByte buf = new MatOfByte();
        Highgui.imencode(".jpg", img, buf);
        byte[] imageBytes = buf.toArray();
        stream.write(("Content-type: image/jpeg\r\n" +
            "Content-Length: " + imageBytes.length + "\r\n" +
            "\r\n").getBytes());
        stream.write(imageBytes);
        stream.write(("\r\n--" + boundary + "\r\n").getBytes());
    }
}

我想在桌面计算机上使用Java OpenCV来读取此流。从OpenCV的片段我推断我应该能够用这样的URL打开视频捕获..

String url = "http://192.168.1.86:8080/?dummy=param.jpg";
VideoCapture capture =new VideoCapture(url); 

但是这个“客户端代码”什么也没做。我没有抛出错误,它甚至没有尝试打开套接字(使用Wireshark检查)。我已经尝试了openCV 2411和331.我已经四处搜索并且没有看到任何其他人让它工作的证据,所以它应该工作吗?如果不是,我需要将倒数客户端代码编写到上面的writeJpg方法(ReadJpg)来缓冲并将图像传递给OpenCV方法,目前这超出了我的范围。

1 个答案:

答案 0 :(得分:0)

大多数浏览器都不允许通过javascript进行客户端捕获(由于跨脚本规则.Mozilla确实有一个来自Iframe的expollmental获取缩略图,但只支持firefox。或者,在服务器端,php中有imagecreatefromwebp应该能够读取页面/嵌入然后屏幕捕获它在服务器端,然后使用imagejpeg函数保存到jpg。

它会像:

         <?php

           $im = imagecreatefromwebp('URL');

        // Convert it to a jpeg file with 100% quality
             $newimage=imagejpeg($im, './example.jpeg', 100);
             imagedestroy($im);
              echo  $newimage;
          ?>