java 从Android上的相机流式传输实时视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15125439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:34:36  来源:igfitidea点击:

Stream live video from camera on android

javaandroidcameravideo-streaminglive-streaming

提问by Ricky Casavecchia

I am attempting to make an app that will stream a video from the camera of an android phone over the internet using the TCP or UDP protocol. I am currently able to transfer a byte array from the android phone to my computer which is running a server that I have written in C#. I have done streaming video before by sending .jpeg's over the network and showing them at 30 fps but this uses up too much bandwidth.

我正在尝试制作一个应用程序,该应用程序将使用 TCP 或 UDP 协议通过互联网从 Android 手机的摄像头流式传输视频。我目前能够将字节数组从 android 手机传输到我的计算机,该计算机运行我用 C# 编写的服务器。我之前通过网络发送 .jpeg 并以 30 fps 显示它们来完成流式视频,但这占用了太多带宽。

First what would be the best way of capturing the images from the camera? I'm looking at...

首先,从相机捕获图像的最佳方式是什么?我在看...

onPictureTaken(byte[] data, Camera camera)

or

或者

onPreviewFrame (byte[] data, Camera camera)

I'm just interested in the byte[] data, taking that and encoding / compressing it then sending it over the network.

我只对 byte[] 数据感兴趣,将其编码/压缩然后通过网络发送。

Second, how should I turn these frames into a compressed video that is a byte array that can be streamed over the network? I don't care too much about video quality, I care more about cutting down on bandwidth.

其次,我应该如何将这些帧转换为可以通过网络流式传输的字节数组的压缩视频?我不太关心视频质量,我更关心减少带宽。

Here is what I am trying to do, but I don't need high quality video. https://code.google.com/p/spydroid-ipcamera/

这是我想要做的,但我不需要高质量的视频。 https://code.google.com/p/spydroid-ipcamera/

采纳答案by stinepike

if you are planning to encode data by yourself by using any encoder then user

如果您打算使用任何编码器自己编码数据,则用户

onPreviewFrame (byte[] data, Camera camera)

Or you can try in a different method by sending rtsp stream. SpyDroidis a very nice project to look at to learn about this method.

或者您可以通过发送 rtsp 流来尝试不同的方法。SpyDroid是一个非常好的项目,可以查看以了解此方法。

回答by Marko

If you're concerned about bandwidth, maybe you should try to send a JPEG image byte array? Since databyte array is in YUV format, it's bigger than JPEG one. When running a JPEG compression, you're able to define it's quality, which would impact on sending byte array size.

如果您担心带宽,也许您应该尝试发送 JPEG 图像字节数组?由于data字节数组是 YUV 格式,它比 JPEG 格式大。运行 JPEG 压缩时,您可以定义它的质量,这会影响发送字节数组的大小。

 public void onPreviewFrame(byte[] data, Camera camera){

 YuvImage image = new YuvImage(data, ImageFormat.NV21,
                            size.width, size.height, null);
 baos = new ByteArrayOutputStream();
 int jpeg_quality = 100;

 image.compressToJpeg(new Rect(0, 0, size.width, size.height),
                         jpeg_quality, baos);

 byte[] sending_array = baos.toByteArray();

} 

where sizewas previously defined as

其中size先前定义为

Camera.Size size = parameters.getPreviewSize();