U3D WebCamTexture

U3D中可以使用WebCamTexture打开摄像头,包括pc和android。

如在android中使用需要在AndroidManifest.xml中添加权限:

<uses-permission android:name="android.permission.CAMERA" />

具体代码如下:

    private WebCamTexture webTex;
    private int isOpenObCamera = 0;
    private static Color32[] imageData;
        
    void Start()
    {
        imageData = new Color32[320*240];
        StartCoroutine(OpenCamera());
    }

    void Destroy()
    {
        isOpenObCamera = 0;
        if (webTex != null)
            webTex.Stop();     
    }

    IEnumerator OpenCamera()
    {
        isOpenObCamera = 1;
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] devices = WebCamTexture.devices;
            //设置摄像机摄像的区域    
            webTex = new WebCamTexture(devices[0].name, 320, 240, 15);
            webTex.Play();//开始摄像

            isOpenObCamera = 2;
        }
    }   

    void Update()
    {
        if (webTex != null && isOpenObCamera == 2)
        {

            Array.Copy(webTex.GetPixels32(), imageData, ImageSize);        
        }

    }

 

posted @ 2017-02-27 14:07  sev  阅读(984)  评论(0编辑  收藏  举报