問(wèn)題描述
假設(shè)我有一臺(tái)無(wú)線攝像機(jī),我想將鏡頭實(shí)時(shí)傳輸?shù)?Unity.有沒(méi)有辦法做到這一點(diǎn)?
Let's say I have a wireless camera that I want to stream footage from to unity in real-time. Is there a way to achieve this?
額外問(wèn)題:
- 如果相機(jī)的角度更廣(180 度,甚至 360 度)
- 如果這是我想與之互動(dòng)的片段,延遲會(huì)有多大的問(wèn)題
- 除了常規(guī)鏡頭之外,是否可以發(fā)送更多數(shù)據(jù),例如深度感知(使用深度感知相機(jī))?
- 我是瘋了還是已經(jīng)這樣做了?
提前致謝
推薦答案
我假設(shè)這是一個(gè)帶有以太網(wǎng)端口或 Wi-Fi 的相機(jī),您可以連接到它并從中實(shí)時(shí)流式傳輸圖像.
I assume this is a camera with Ethernet port or Wi-Fi that you can connect to and stream images from it live.
如果是這樣,那么是的,可以使用 Unity 完成.
If so, then yes, it can be done with Unity.
如何在沒(méi)有外部庫(kù)的情況下完成:
連接到相機(jī):
1.連接到與相機(jī)相同的本地網(wǎng)絡(luò),或者如果支持unpn,您也可以通過(guò)互聯(lián)網(wǎng)連接到它.通常,您需要攝像機(jī)的 IP 和端口來(lái)執(zhí)行此操作.假設(shè)攝像機(jī) IP 地址為 192.168.1.5
,端口號(hào)為 900
.要連接的 url 是 http://192.168.1.5:900
.
1.Connect to the-same local network with the camera or if unpn is supported, you can also connect to it through internet. Usually, you need the IP and the port of the camera to do this. Let's say that the Camera IP Address is 192.168.1.5
and the port number is 900
. The url to connect to is http://192.168.1.5:900
.
有時(shí),它只是一個(gè)以 .mjpg 或 .bin 結(jié)尾的 url,例如 http://192.168.1.5/mjpg/video.mjpg
和 http://192.168.1.5/mjpg/video.bin
Sometimes, it is simply an url that ends with .mjpg or .bin such as http://192.168.1.5/mjpg/video.mjpg
and http://192.168.1.5/mjpg/video.bin
每臺(tái)相機(jī)都是不同的.找到該網(wǎng)址的唯一方法是閱讀其手冊(cè).如果該手冊(cè)不可用,請(qǐng)使用其官方應(yīng)用程序連接到它,然后使用 Wireshark 發(fā)現(xiàn)相機(jī)圖像的 url.username
和 password
(如果需要)也可以在手冊(cè)中找到.如果沒(méi)有,請(qǐng)?jiān)?Google 上搜索型號(hào),您需要的所有東西都應(yīng)找到.
Every camera is different. The only way to find the url is to read its manual. If the manual is not available, connect to it with its official Application then use Wireshark to discover the url of the camera Image. The username
and the password
(if required) can also be found in the manual. If not available, google the model number and everything you need should be found.
從相機(jī)中提取 JPEG:
連接到相機(jī)后,相機(jī)會(huì)不斷地向您發(fā)送數(shù)據(jù).您可以掃描這些數(shù)據(jù)并從中檢索圖像.
When connected to the camera, the camera will be sending endless data to you. This data you can scan and retrieve the image from it.
2.搜索 0xFF
后跟 0xD8
的 JPEG 標(biāo)頭.如果這兩個(gè)字節(jié)彼此相鄰,則開(kāi)始讀取字節(jié)并繼續(xù)將它們保存到數(shù)組中.您可以使用 index(int
) 變量來(lái)計(jì)算收到的字節(jié)數(shù).
2.Search for the JPEG header which is 0xFF
followed by 0xD8
. If these two bytes are next to each other then start reading the bytes and continue to save them to an array. You can use an index(int
) variable to keep count of how many bytes you have received.
3.從攝像頭讀取數(shù)據(jù)時(shí),檢查接下來(lái)的兩個(gè)字節(jié)是否是JPEG頁(yè)腳,即0xFF
后跟0xD9
.如果這是真的,那么您已經(jīng)收到了完整的圖像(1 幀).
3.While reading the data from the camera, check if the next two bytes is the JPEG footer which is 0xFF
followed by 0xD9
. If this is true then you have received the complete image(1 frame).
您的圖像字節(jié)應(yīng)如下所示:
Your image bytes should look something like:
0xFF
0xD8
someotherbytes(數(shù)千個(gè))..... 然后是 0xFF
0xD9
0xFF
0xD8
someotherbytes(thousands of them)..... then 0xFF
0xD9
將 receivedBytes
復(fù)制到 completeImageByte
變量中,以便以后可以使用它來(lái)顯示圖像.將 counter
變量重置為 0.
Copy receivedBytes
to the completeImageByte
variable so that it can be used to display the image later on. Reset counter
variable to 0.
在屏幕上顯示 JPEG 圖像:
4.將圖像顯示到屏幕
由于您每秒將收到許多圖像,因此我發(fā)現(xiàn)顯示此圖像的最有效方式是使用RawImage
組件.因此,如果您希望它在移動(dòng)設(shè)備上運(yùn)行,請(qǐng)不要為此使用 Image
或 Sprite Renderer
.
Since you will be receiving many images per second, the most efficient way I found to display this is to use RawImage
Component. So, don't use Image
or Sprite Renderer
for this if you want this to run on mobile devices.
camTexture = new Texture2D(2, 2);
只需在 Start()
函數(shù)中執(zhí)行一次即可.
You only need to do camTexture = new Texture2D(2, 2);
once in the Start()
function.
5.跳回步驟2并繼續(xù)執(zhí)行,直到您想要完全為止.
5.Jump back to step 2 and continue doing it until you want to quite.
用于連接相機(jī)的 API:.
如果相機(jī)需要身份驗(yàn)證(用戶(hù)名和密碼),請(qǐng)使用 HttpWebRequest
.
Use HttpWebRequest
if the camera requires authentication (username and password).
對(duì)于不需要身份驗(yàn)證的用戶(hù),請(qǐng)使用 UnityWebRequest
.使用 UnityWebRequest
時(shí),您必須從 派生自己的類(lèi)DownloadHandlerScript
否則您的應(yīng)用程序?qū)⒈罎ⅲ驗(yàn)槟鷮⒉煌5亟邮諗?shù)據(jù).
For those that does not require authentication, use UnityWebRequest
. When using UnityWebRequest
, you must derive your own classes from DownloadHandlerScript
or your app will crash as you will be receiving data non stop.
從DownloadHandlerScript
派生你自己的類(lèi)的例子:
Example of deriving your own class from DownloadHandlerScript
:
用法:
您可以讓他們?cè)?code>ReceiveData<中執(zhí)行步驟2、3、4和5CustomWebRequest
腳本中的/code> 函數(shù).
You can them perform step 2,3,4 and 5 in the ReceiveData
function from the CustomWebRequest
script.
控制攝像頭:
相機(jī)具有平移、旋轉(zhuǎn)、翻轉(zhuǎn)、鏡像和執(zhí)行其他功能的命令.這在每個(gè)相機(jī)中都不同,但它很簡(jiǎn)單,只需向相機(jī)的 url 發(fā)出 GET/POST 請(qǐng)求并提供查詢(xún).這些命令可以在相機(jī)手冊(cè)中找到.
Cameras have commands to pan, rotate, flip, mirror and perform other function.This is different in every camera but it is simple as making GET/POST request to a url of the camera and providing the queries. These commands can be found in the camera manual.
例如:http://192.168.1.5?pan=50&rotate=90
其他框架:
AForge - 一個(gè)可以同時(shí)處理 JPEG/MJPES 和 FFMPEG 來(lái)自相機(jī).您必須修改它以使用 Unity,如果您不能執(zhí)行步驟 2、3、4 和 5.
AForge - A free framework that can handle both JPEG/MJPES and FFMPEG from the camera. You have to modify it to work with Unity and you should if you can't do step 2,3,4 and 5.
這篇關(guān)于將實(shí)時(shí)鏡頭從攝像機(jī)流式傳輸?shù)?Unity3D的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!