串流视频帧到Android

时间:2020-04-05 04:35:33

标签: android opencv streaming

我目前正在使用手机摄像头并打开CV处理帧的应用程序上工作。现在,我认为能够将帧发送到另一个Android客户端会很酷。我以为可以用蒸笼一帧一帧地工作,但不知道如何设置主机以及效率不高。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

如果只想将每个帧作为原始数据集发送,则可以使用套接字。

下面的这段代码现在已经很旧了,但是在上次测试时仍然可以正常工作-它可以发送完整的视频,但是您可以使用它发送想要的任何文件:

//Send the video file to helper over a Socket connection so he helper can compress the video file 
        Socket helperSocket = null;

        try {
            Log.d("VideoChunkDistributeTask doInBackground","connecting to: " + helperIPAddress + ":" + helperPort);
            helperSocket = new Socket(helperIPAddress, helperPort);
            BufferedOutputStream helperSocketBOS = new BufferedOutputStream(helperSocket.getOutputStream());
            byte[] buffer = new byte[4096];

            //Write the video chunk to the output stream
            //Open the file
            File videoChunkFile = new File(videoChunkFileName);
            BufferedInputStream chunkFileIS = new BufferedInputStream(new FileInputStream(videoChunkFile));

            //First send a long with the file length - wrap the BufferedOutputStream  in a DataOuputStream to
            //allow us send a long directly
            DataOutputStream helperSocketDOS = new DataOutputStream(
                     new BufferedOutputStream(helperSocket.getOutputStream()));
            long chunkLength = videoChunkFile.length();
            helperSocketDOS.writeLong(chunkLength);
            Log.d("VideoChunkDistributeTask doInBackground","chunkLength: " + chunkLength);

            //Now loop through the video chunk file sending it to the helper via the socket - note this will simply 
            //do nothing if the file is empty
            int readCount = 0;
            int totalReadCount = 0;
            while(totalReadCount < chunkLength) {
                //write the buffer to the output stream of the socket
                readCount = chunkFileIS.read(buffer);
                helperSocketDOS.write(buffer, 0, readCount);
                totalReadCount += readCount;
            }

            Log.d("VideoChunkDistributeTask doInBackground","file sent");
            chunkFileIS.close();
            helperSocketDOS.flush();
        } catch (UnknownHostException e) {
            Log.d("VideoChunkDistributeTask doInBackground","unknown host");
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            Log.d("VideoChunkDistributeTask doInBackground","IO exceptiont");
            e.printStackTrace();
            return null;
        }

完整的源代码位于:https://github.com/mickod/ColabAndroid/tree/master/src/com/amodtech/colabandroid

您可能还会发现有更多可用的套接字库,可能更适合您使用,但一般原理应该相似。

如果您要流式传输视频,以便另一个应用程序可以像从网络上流式传输的常规视频那样播放视频,则您需要在“发送”设备上设置网络服务器。此时,将其发送到服务器并从那里流式传输可能会更容易。