首页 > 解决方案 > 流视频帧 Android-to-android

问题描述

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

标签: androidopencvstreaming

解决方案


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

下面的代码现在很旧,但在上次测试时运行良好 - 它发送整个视频,但您可以使用相同的代码发送您想要的任何文件:

//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

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

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


推荐阅读