android - 流视频帧 Android-to-android
问题描述
我目前正在开发一个使用手机摄像头并打开 CV 来处理帧的应用程序。现在我认为能够将帧发送到另一个 Android 客户端会很酷。我认为用蒸汽机逐帧可以工作,但不知道如何设置主机以及它是否效率不高。有什么建议么?
解决方案
如果您只想将每个帧作为一组原始数据发送,您可以使用套接字。
下面的代码现在很旧,但在上次测试时运行良好 - 它发送整个视频,但您可以使用相同的代码发送您想要的任何文件:
//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
您可能还会发现有更多可用的最新套接字库,它们可能更适合您使用,但一般原则应该是相似的。
如果您想流式传输您的视频,以便其他应用程序可以像从网络上流式传输的常规视频一样播放它,那么您需要在“发送”设备上设置一个网络服务器。此时,将其发送到服务器并从那里流式传输可能会更容易。
推荐阅读
- python - 在 Google Cloud SDK 中的 python 脚本中执行节点脚本
- php - SQLSTATE [HY000]:一般错误:13 无法获取 './pics' 的统计信息(错误代码:13 - 权限被拒绝)
- properties - 如何在另一台计算机上隐藏和显示 Outlook 日历
- php - 使用 PHP 单击链接时,如何将文本分配到页面?
- ruby-on-rails - 如何覆盖我的 classes 属性如何为我的订单模型呈现 JSON
- pdf - 如何将不寻常的 Unicode 字符 (UTF-8) 转换为 PDF?
- sql-server - SQL Server 2012:如何从名为“NULL”且 ID 为 0 的索引中恢复磁盘空间?
- javascript - Node.Js / Express 将数据发送回前端
- karate - 我可以在空手道中保存/使用原始响应体吗?
- java - Netbeans 在初始化时崩溃,Eclipse 也是如此