我假设这是一台带有以太网端口或 Wi-Fi 的相机,您可以连接到它并实时传输图像。
如果是这样,那么可以使用 Unity 来完成。
在没有外部库的情况下如何完成:
连接到相机:
1.与相机连接到同一本地网络,或者如果支持unpn,您也可以通过互联网连接到它。通常,您需要摄像机的 IP 和端口来执行此操作。假设摄像机 IP 地址是192.168.1.5
端口号是900
。要连接的 url 是http://192.168.1.5:900
.
有时,它只是一个以以下结尾的网址.mjpg or .bin例如http://192.168.1.5/mjpg/video.mjpg
and http://192.168.1.5/mjpg/video.bin
每个相机都是不同的。找到该网址的唯一方法是阅读其手册。如果手册不可用,请使用其官方应用程序连接到它,然后使用 Wireshark 发现相机图像的 url。这username
和password
(如果需要)也可以在手册中找到。如果没有,请用谷歌搜索型号,应该可以找到您需要的一切。
从相机中提取 JPEG:
当连接到相机时,相机将向您发送无尽的数据。您可以扫描这些数据并从中检索图像。
2.搜索 JPEG 标头,该标头是0xFF
其次是0xD8
。如果这两个字节彼此相邻,则开始读取字节并继续将它们保存到数组中。您可以使用索引(int
) 变量来记录您收到的字节数。
int counter = 0;
byte[] completeImageByte = new byte[500000];
byte[] receivedBytes = new byte[500000];
receivedBytes[counter] = byteFromCamera;
counter++;
3.从相机读取数据时,检查接下来的两个字节是否是JPEG页脚,即0xFF
其次是0xD9
。如果这是真的,那么您已经收到完整的图像(1 帧)。
您的图像字节应类似于:
0xFF
0xD8
一些其他字节(数千个)......然后0xFF
0xD9
Copy receivedBytes
to the completeImageByte
变量,以便稍后可以使用它来显示图像。重置counter
变量为 0。
Buffer.BlockCopy(receivedBytes, 0, completeImageByte, 0, counter);
counter = 0;
在屏幕上显示 JPEG 图像:
4.将图像显示到屏幕上
由于您每秒将收到许多图像,因此最多高效的我发现显示它的方法是使用RawImage
成分。所以,不要使用Image
or 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.跳回步骤2并继续这样做,直到你想停止为止。
连接相机的API:.
Use 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));
}
}
Usage:
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();
}
}
你可以让他们执行步骤2,3,4 and 5 in the ReceiveData
函数从CustomWebRequest
script.
控制相机:
摄像机具有平移、旋转、翻转、镜像和执行其他功能的命令。每个摄像机的情况都不同,但很简单,只需向摄像机的 URL 发出 GET/POST 请求并提供查询即可。这些命令可以在相机手册中找到。
例如:http://192.168.1.5?pan=50&rotate=90
其他框架:
AForge- 一个可以处理两者的免费框架JPEG/MJPES and FFMPEG从相机。你必须修改它才能与 Unity 一起使用,如果你不能执行步骤,你应该修改它2,3,4 and 5.