2016-09-14 127 views
3

比方說,我有一臺無線攝像頭,可以實時將素材流式傳輸到Unity。有沒有辦法做到這一點?將相機中的實況素材流式傳輸到Unity3D

獎金的問題:

  • 什麼相機與更廣泛的角度(180,甚至360)
  • 如何的問題在很大程度上將延遲是如果這是一個鏡頭我想與
  • 進行交互
  • 除了常規素材之外,還可以發送更多數據,如深度知覺(使用深度感知相機)嗎?
  • 我瘋了還是這樣做了?

在此先感謝

回答

3

我想這是有以太網端口或Wi-Fi的相機,你可以連接到並從它進行視頻直播的圖像。

如果是這樣,那麼是的,它可以用Unity來完成。

如何它是在沒有外部庫完成:

連接到相機

.Connect用相機或如果unpn支持的,同一局域網內,你也可以通過互聯網連接它。通常情況下,您需要攝像機的IP和端口來執行此操作。假設攝像機IP地址爲192.168.1.5,端口號爲900。要連接的網址是http://192.168.1.5:900

有時,它只是的.bin如http://192.168.1.5/mjpg/video.mjpghttp://192.168.1.5/mjpg/video.bin

每個相機是不同的與.mjpg重圓的網址。找到網址的唯一方法是閱讀它的手冊。如果手冊不可用,請使用官方應用程序連接到手冊,然後使用Wireshack發現相機圖像的URL。 usernamepassword(如果需要)也可以在手冊中找到。如果不可用,請搜索Google型號和所需的全部內容。

從相機解壓縮JPEG:

當連接到相機,相機會發出無窮的數據給你。這些數據可以掃描並從中檢索圖像。

。搜索JPEG標頭0xFF後跟0xD8。如果這兩個字節彼此相鄰,則開始讀取字節並繼續將它們保存到數組中。您可以使用索引(int)變量來保持您收到的字節數。

int counter = 0; 
byte[] completeImageByte = new byte[500000]; 
byte[] receivedBytes = new byte[500000]; 
receivedBytes[counter] = byteFromCamera; 
counter++; 

。在從相機讀取數據的同時,檢查接下來的兩個字節是否是JPEG頁腳0xFF,然後是0xD9。如果這是真的,那麼你已經收到完整的圖像(1幀)。

您的圖片字節應該是這個樣子:

0xFF0xD8 someotherbytes(幾千條).....然後0xFF0xD9

複製receivedBytescompleteImageByte變量,以便它可以用來顯示稍後的圖像。重置counter變量爲0。

Buffer.BlockCopy(receivedBytes, 0, completeImageByte, 0, counter); 
counter = 0; 

顯示JPEG圖像到屏幕

。顯示圖像來篩選

因爲要每秒接收許多圖像,我發現顯示的最多efficient方式是使用RawImage組件。所以,如果你想讓它在移動設備上運行,請不要使用ImageSprite Renderer

public RawImage screenDisplay; 
if(updateFrame){ 
Texture2D camTexture = new Texture2D(2, 2); 
camTexture.LoadImage(completeImageByte); 
screenDisplay.texture = camTexture; 
} 

,您只需要在Start()功能做camTexture = new Texture2D(2, 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(); 
    } 
} 

可以對其執行步驟,,和在ReceiveData功能來自CustomWebRequest腳本。

控制攝像頭

相機具有命令進行平移,旋轉,翻轉,鏡子和執行其它功能。本是在每一個照相機不同,但它是作爲簡單製作GET/POST請求的一個url相機並提供查詢。這些命令可以在相機手冊中找到。

例如:http://192.168.1.5?pan=50&rotate=90

其他框架

AForge - 一個免費的框架,可以處理來自相機雙方JPEG/MJPESFFMPEG。你必須修改它與團結的工作,你應該,如果你不能做到一步,,和。