比方说,我有一个无线摄像头,我想实时播放从视频到统一的视频。有办法做到这一点吗?
奖金问题:
提前感谢
发布于 2016-09-14 19:38:55
我想这是一个带有以太网端口或with的摄像头,你可以通过它实时连接和传输图像。
如果是这样的话,那么是的,它可以通过统一来完成。
它是如何在没有外部库的情况下完成的
连接到照相机
1.Connect与摄像头连接到同一个本地网络,如果支持unpn,您也可以通过互联网连接到它。通常,你需要IP和摄像头的端口来做这件事。假设相机IP地址是192.168.1.5,端口号是900。要连接到的url是http://192.168.1.5:900。
有时,它只是一个url,以.mjpg或.bin (如http://192.168.1.5/mjpg/video.mjpg和http://192.168.1.5/mjpg/video.bin )结尾。
每个相机都不一样。找到网址的唯一方法是阅读它的手册。如果手册不可用,请连接到它的官方应用程序,然后使用Wireshark来发现相机图像的网址。username和password(如果需要的话)也可以在手册中找到。如果没有,谷歌的型号和你需要的一切都应该找到。
从相机中提取JPEG
当连接到相机时,相机会向你发送无穷无尽的数据。这些数据可以扫描并从中检索图像。
2.Search用于JPEG头,它是0xFF,后面跟着0xD8。如果这两个字节相邻,那么开始读取字节并继续将它们保存到数组中。可以使用索引(int)变量来统计接收到的字节数。
int counter = 0;
byte[] completeImageByte = new byte[500000];
byte[] receivedBytes = new byte[500000];
receivedBytes[counter] = byteFromCamera;
counter++;3.While从摄像机读取数据,检查下两个字节是否是0xFF页脚,后面是0xD9。如果这是真的,那么您已经收到完整的图像(1帧)。
您的图像字节应该如下所示:
0xFF 0xD8其他字节(千字节).然后是0xFF 0xD9
将receivedBytes复制到completeImageByte变量,以便以后可以用于显示图像。将counter变量重置为0。
Buffer.BlockCopy(receivedBytes, 0, completeImageByte, 0, counter);
counter = 0;在屏幕上显示图像
4.Display图像到屏幕
由于您将每秒收到许多图像,所以我发现显示这一点的大多数高效率方式是使用RawImage组件。因此,如果您想让Image或Sprite Renderer在移动设备上运行,就不要使用它们。
public RawImage screenDisplay;
if(updateFrame){
Texture2D camTexture = new Texture2D(2, 2);
camTexture.LoadImage(completeImageByte);
screenDisplay.texture = camTexture;
}您只需要在camTexture = new Texture2D(2, 2);函数中执行一次Start()。
5.Jump返回到步骤2,并继续执行,直到您想要完成为止。
用于连接到相机的API:。
如果相机需要身份验证(用户名和密码),请使用HttpWebRequest。
对于不需要身份验证的用户,请使用UnityWebRequest。使用UnityWebRequest时,您必须从DownloadHandlerScript派生您自己的类,否则应用程序会崩溃,因为您将不间断地接收数据。
从DownloadHandlerScript派生您自己的类的示例
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class CustomWebRequest : DownloadHandlerScript
{
// Standard scripted download handler - will allocate memory on each ReceiveData callback
public CustomWebRequest()
: base()
{
}
// Pre-allocated scripted download handler
// Will reuse the supplied byte array to deliver data.
// Eliminates memory allocation.
public CustomWebRequest(byte[] buffer)
: base(buffer)
{
}
// Required by DownloadHandler base class. Called when you address the 'bytes' property.
protected override byte[] GetData() { return null; }
// Called once per frame when data has been received from the network.
protected override bool ReceiveData(byte[] byteFromCamera, int dataLength)
{
if (byteFromCamera == null || byteFromCamera.Length < 1)
{
//Debug.Log("CustomWebRequest :: ReceiveData - received a null/empty buffer");
return false;
}
//Search of JPEG Image here
return true;
}
// Called when all data has been received from the server and delivered via ReceiveData
protected override void CompleteContent()
{
//Debug.Log("CustomWebRequest :: CompleteContent - DOWNLOAD COMPLETE!");
}
// Called when a Content-Length header is received from the server.
protected override void ReceiveContentLength(int contentLength)
{
//Debug.Log(string.Format("CustomWebRequest :: ReceiveContentLength - length {0}", contentLength));
}
}使用
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class Test : MonoBehaviour
{
CustomWebRequest camImage;
UnityWebRequest webRequest;
byte[] bytes = new byte[90000];
void Start()
{
string url = "http://camUrl/mjpg/video.mjpg";
webRequest = new UnityWebRequest(url);
webRequest.downloadHandler = new CustomWebRequest(bytes);
webRequest.Send();
}
}您可以从CustomWebRequest脚本在ReceiveData函数中执行步骤2、3、4和5。
控制摄像机
相机有命令进行平移、旋转、翻转、镜像和执行其他function.This是不同的在每一个相机,但它是简单的提出获取/发布请求到相机的一个网址,并提供查询。这些命令可以在相机手册中找到。
例如:http://192.168.1.5?pan=50&rotate=90
其他框架
AForge -一个免费的框架,可以同时处理JPEG/MJPES和FFMPEG的相机。您必须修改它才能与Unity一起工作,如果不能执行步骤2、3、4和5,则应该这样做。
https://stackoverflow.com/questions/39494986
复制相似问题