2016-05-22 25 views
3

我使用的是WebCamTexture和我在Start方法來啓動它,然後我跑的另一種方法。我使用GetPixels()獲得像素,但是,它們都是(0, 0, 0)。是否有任何修復或我可以等待的方式(Unity似乎使用while循環和WaitForSeconds)崩潰。這是我目前的入門方法:WebCamTexture想出在第一,第二或全0二

void Start() { 

    rawImage = gameObject.GetComponent<RawImage>(); 
    rawImageRect = rawImage.GetComponent<RectTransform>(); 

    webcamTexture = new WebCamTexture(); 
    rawImage.texture = webcamTexture; 
    rawImage.material.mainTexture = webcamTexture; 

    webcamTexture.Play(); 

    Method(); 

    loadingTextObject.SetActive (false); 
    gameObject.SetActive (true); 

} 

void Method(){ 

    print (webcamTexture.GetPixels [0]); 

} 

而這個打印每次(0, 0, 0)顏色。

+1

嗨迪倫。這是Unity和網絡攝像機的一個衆所周知的問題。你實際上需要做的是等到***報告一個合理的大小***。在實踐中,你跳過框架,直到你的WebCam.width大於100.它在這裏完全解釋http://answers.unity3d.com/questions/773464/webcamtexture-correct-resolution-and-ratio.html – Fattie

+0

BTW大多數人只是使用NatCam https://www.assetstore.unity3d.com/en/#!/content/52154這些天。 Unity的攝像頭軟件是廢話。這是他們「還沒有到」的事情之一。準備花上幾個星期與攝像頭一起工作。 – Fattie

+0

噢好吧我已經在別的地方做這個了。希望這會起作用。 –

回答

2

在調用webcamTexture.GetPixels之前,請先在協同程序中使用yield return new WaitForSeconds(2);等待2秒鐘。

​​

還是喜歡喬在評論部分說。等待秒數不可靠。您只需等待寬度有東西讀之前it.Just更換

yield return new WaitForSeconds(2); 

while (webcamTexture.width < 100) 
{ 
    yield return null; 
} 
+0

這似乎會導致Unity崩潰。 –

+1

@DylanSiegler哪部分崩潰Unity?嘗試具體。您可以通過評論代碼從底部到頂部找到答案。 – Programmer

+0

收益率返回WaitForSeconds(2) –

0

兩秒鐘可能是相當大量的時間花費在等待,當你grapping的大小請求的圖像。

的WebCamTexture有requsted寬度/高度,你可以訪問,它會調用的getPixels一次之後保存「真正的」寬/紋理的高度。我們在兩個與用戶分享照片的項目中使用了名爲CameraCaptureKit的資產(https://www.assetstore.unity3d.com/en/#!/content/56673)。

的代碼使用有取WebCamTexture和Graps幀的每一幀,直到它返回紋理的寬度和高度。

在更新函數TryDummySnapshot被調用,直到大小正確返回。

// ANDREAS added this: In some cases we apparently don't get correct width and height until we have tried to read pixels 
    // from the buffer. 
    void TryDummySnapshot() { 
     if(!gotAspect) { 
      if(webCamTexture.width>16) { 
       if(Application.platform == RuntimePlatform.IPhonePlayer) { 
        if(verbose)Debug.Log("Already got width height of WebCamTexture."); 
       } else { 
       } 
       gotAspect = true; 
      } else { 
       if(verbose)Debug.Log ("Taking dummy snapshot"); 
       if(tmpImg == null) {} 
       Color32[] c = webCamTexture.GetPixels32(); 
      } 
     } 
    } 
+0

您是否用'webcamTexture.width <100'閱讀了我的答案的第二部分?他沒有采取任何措施,他想要從相機中檢索圖像。破壞紋理也會減慢速度。 – Programmer

+0

我明白你的觀點 - 也許這些紋理中的一個不應該被破壞,但它真的需要多長時間?我不知道爲什麼第一個Texture被創建和銷燬的原因(當它大於10時) - 我複製粘貼代碼。摧毀質地需要多長時間? – Chris

+0

沒有像從相機中取回圖像一樣拍攝快照? – Chris

0

如果你不想等到大小報告或分配和目標平臺的iOS(iPhone/iPad的),它也可以編寫一個插件,直接從相機代碼獲取大小統一通過延伸CameraCapture.mm:

- (void)captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection 
{ 
intptr_t tex = (intptr_t)CMVideoSampling_SampleBuffer(&self->_cmVideoSampling, sampleBuffer, &self->_width, &self->_height); 

_staticWebCamTexWidth = self->_width; 
_staticWebCamTexWidth = self->_height; 

UnityDidCaptureVideoFrame(tex, self->_userData); 

if(self.firstFrameCallback!=nil) { 
    [self fireFirstFrame]; 
} 
} 

自>寬度/高度可以在加入作爲像一個插件方法的方法而返回:

[DllImport("__Internal")] 
private static extern int GetWebCamTextureWidth(); 

[DllImport("__Internal")] 
private static extern int GetWebCamTextureHeight(); 

然後在objc代碼

extern "C" int GetWebCamTextureWidth() { return _staticWebCamTexWidth; } 
extern "C" int GetWebCamTextureHeight() { return _staticWebCamTexHeight; } 

這是我爲iOS社交遊戲中使用統一的解決方案之一。在Android上,我們放棄了之前描述的方法。

相關問題