Exemplo n.º 1
0
        /// <summary>
        /// Initializes this instance by coroutine.
        /// </summary>
        protected virtual IEnumerator _Initialize()
        {
            if (hasInitDone)
            {
                _Dispose();
            }

            isInitWaiting = true;

            if (!String.IsNullOrEmpty(requestedDeviceName))
            {
                webCamTexture = new WebCamTexture(requestedDeviceName, requestedWidth, requestedHeight, requestedFPS);
            }
            else
            {
                // Checks how many and which cameras are available on the device
                for (int cameraIndex = 0; cameraIndex < WebCamTexture.devices.Length; cameraIndex++)
                {
                    if (WebCamTexture.devices [cameraIndex].isFrontFacing == requestedIsFrontFacing)
                    {
                        webCamDevice  = WebCamTexture.devices [cameraIndex];
                        webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight, requestedFPS);

                        break;
                    }
                }
            }

            if (webCamTexture == null)
            {
                if (WebCamTexture.devices.Length > 0)
                {
                    webCamDevice  = WebCamTexture.devices [0];
                    webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight, requestedFPS);
                }
                else
                {
                    isInitWaiting = false;

                    if (onErrorOccurred != null)
                    {
                        onErrorOccurred.Invoke(ErrorCode.CAMERA_DEVICE_NOT_EXIST);
                    }

                    yield break;
                }
            }

            // Starts the camera
            webCamTexture.Play();

            int  initFrameCount = 0;
            bool isTimeout      = false;

            while (true)
            {
                if (initFrameCount > timeoutFrameCount)
                {
                    isTimeout = true;
                    break;
                }
                // If you want to use webcamTexture.width and webcamTexture.height on iOS, you have to wait until webcamTexture.didUpdateThisFrame == 1, otherwise these two values will be equal to 16. (http://forum.unity3d.com/threads/webcamtexture-and-error-0x0502.123922/)
                #if UNITY_IOS && !UNITY_EDITOR && (UNITY_4_6_3 || UNITY_4_6_4 || UNITY_5_0_0 || UNITY_5_0_1)
                else if (webCamTexture.width > 16 && webCamTexture.height > 16)
                {
                #else
                else if (webCamTexture.didUpdateThisFrame)
                {
                    #if UNITY_IOS && !UNITY_EDITOR && UNITY_5_2
                    while (webCamTexture.width <= 16)
                    {
                        if (initFrameCount > timeoutFrameCount)
                        {
                            isTimeout = true;
                            break;
                        }
                        else
                        {
                            initFrameCount++;
                        }
                        webCamTexture.GetPixels32();
                        yield return(new WaitForEndOfFrame());
                    }
                    if (isTimeout)
                    {
                        break;
                    }
                    #endif
                    #endif

                    Debug.Log("name " + webCamTexture.name + " width " + webCamTexture.width + " height " + webCamTexture.height + " fps " + webCamTexture.requestedFPS);
                    Debug.Log("videoRotationAngle " + webCamTexture.videoRotationAngle + " videoVerticallyMirrored " + webCamTexture.videoVerticallyMirrored + " isFrongFacing " + webCamDevice.isFrontFacing);

                    if (colors == null || colors.Length != webCamTexture.width * webCamTexture.height)
                    {
                        colors = new Color32[webCamTexture.width * webCamTexture.height];
                    }

                    rgbaMat           = new Mat(webCamTexture.height, webCamTexture.width, CvType.CV_8UC4);
                    screenOrientation = Screen.orientation;

                    #if !UNITY_EDITOR && !(UNITY_STANDALONE || UNITY_WEBGL)
                    if (screenOrientation == ScreenOrientation.Portrait || screenOrientation == ScreenOrientation.PortraitUpsideDown)
                    {
                        rotate90Degree = true;
                    }
                    #endif

                    if (rotate90Degree)
                    {
                        rotatedRgbaMat = new Mat(webCamTexture.width, webCamTexture.height, CvType.CV_8UC4);
                    }

                    isInitWaiting = false;
                    hasInitDone   = true;

                    if (onInitialized != null)
                    {
                        onInitialized.Invoke();
                    }

                    break;
                }
                else
                {
                    initFrameCount++;
                    yield return(0);
                }
            }

            if (isTimeout)
            {
                webCamTexture.Stop();
                webCamTexture = null;
                isInitWaiting = false;

                if (onErrorOccurred != null)
                {
                    onErrorOccurred.Invoke(ErrorCode.TIMEOUT);
                }
            }
        }
        /// <summary>
        /// Initializes this instance by coroutine.
        /// </summary>
        protected virtual IEnumerator _Initialize()
        {
            if (hasInitDone)
            {
                ReleaseResources();

                if (onDisposed != null)
                {
                    onDisposed.Invoke();
                }
            }

            isInitWaiting = true;

            float requestedFPS = this.requestedFPS;

            // Creates the camera
            if (!String.IsNullOrEmpty(requestedDeviceName))
            {
                int requestedDeviceIndex = -1;
                if (Int32.TryParse(requestedDeviceName, out requestedDeviceIndex))
                {
                    if (requestedDeviceIndex >= 0 && requestedDeviceIndex < WebCamTexture.devices.Length)
                    {
                        webCamDevice = WebCamTexture.devices[requestedDeviceIndex];

                        if (avoidAndroidFrontCameraLowLightIssue && webCamDevice.isFrontFacing == true)
                        {
                            requestedFPS = 15f;
                        }

                        if (requestedFPS < 0)
                        {
                            webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight);
                        }
                        else
                        {
                            webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight, (int)requestedFPS);
                        }
                    }
                }
                else
                {
                    for (int cameraIndex = 0; cameraIndex < WebCamTexture.devices.Length; cameraIndex++)
                    {
                        if (WebCamTexture.devices[cameraIndex].name == requestedDeviceName)
                        {
                            webCamDevice = WebCamTexture.devices[cameraIndex];

                            if (avoidAndroidFrontCameraLowLightIssue && webCamDevice.isFrontFacing == true)
                            {
                                requestedFPS = 15f;
                            }

                            if (requestedFPS < 0)
                            {
                                webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight);
                            }
                            else
                            {
                                webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight, (int)requestedFPS);
                            }
                            break;
                        }
                    }
                }
                if (webCamTexture == null)
                {
                    Debug.Log("Cannot find camera device " + requestedDeviceName + ".");
                }
            }

            if (webCamTexture == null)
            {
                // Checks how many and which cameras are available on the device
                for (int cameraIndex = 0; cameraIndex < WebCamTexture.devices.Length; cameraIndex++)
                {
                    if (WebCamTexture.devices[cameraIndex].isFrontFacing == requestedIsFrontFacing)
                    {
                        webCamDevice = WebCamTexture.devices[cameraIndex];

                        if (avoidAndroidFrontCameraLowLightIssue && webCamDevice.isFrontFacing == true)
                        {
                            requestedFPS = 15f;
                        }

                        if (requestedFPS < 0)
                        {
                            webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight);
                        }
                        else
                        {
                            webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight, (int)requestedFPS);
                        }
                        break;
                    }
                }
            }

            if (webCamTexture == null)
            {
                if (WebCamTexture.devices.Length > 0)
                {
                    webCamDevice = WebCamTexture.devices[0];

                    if (avoidAndroidFrontCameraLowLightIssue && webCamDevice.isFrontFacing == true)
                    {
                        requestedFPS = 15f;
                    }

                    if (requestedFPS < 0)
                    {
                        webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight);
                    }
                    else
                    {
                        webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight, (int)requestedFPS);
                    }
                }
                else
                {
                    isInitWaiting = false;

                    if (onErrorOccurred != null)
                    {
                        onErrorOccurred.Invoke(ErrorCode.CAMERA_DEVICE_NOT_EXIST);
                    }

                    yield break;
                }
            }

            // Starts the camera
            webCamTexture.Play();

            int  initFrameCount = 0;
            bool isTimeout      = false;

            while (true)
            {
                if (initFrameCount > timeoutFrameCount)
                {
                    isTimeout = true;
                    break;
                }
                // If you want to use webcamTexture.width and webcamTexture.height on iOS, you have to wait until webcamTexture.didUpdateThisFrame == 1, otherwise these two values will be equal to 16. (http://forum.unity3d.com/threads/webcamtexture-and-error-0x0502.123922/)
#if UNITY_IOS && !UNITY_EDITOR && (UNITY_4_6_3 || UNITY_4_6_4 || UNITY_5_0_0 || UNITY_5_0_1)
                else if (webCamTexture.width > 16 && webCamTexture.height > 16)
                {
#else
                else if (webCamTexture.didUpdateThisFrame)
                {
#if UNITY_IOS && !UNITY_EDITOR && UNITY_5_2
                    while (webCamTexture.width <= 16)
                    {
                        if (initFrameCount > timeoutFrameCount)
                        {
                            isTimeout = true;
                            break;
                        }
                        else
                        {
                            initFrameCount++;
                        }
                        webCamTexture.GetPixels32();
                        yield return(new WaitForEndOfFrame());
                    }
                    if (isTimeout)
                    {
                        break;
                    }
#endif
#endif

                    /*
                     * Debug.Log("WebCamTextureToMatHelper:: " + "devicename:" + webCamTexture.deviceName + " name:" + webCamTexture.name + " width:" + webCamTexture.width + " height:" + webCamTexture.height + " fps:" + webCamTexture.requestedFPS
                     + " videoRotationAngle:" + webCamTexture.videoRotationAngle + " videoVerticallyMirrored:" + webCamTexture.videoVerticallyMirrored + " isFrongFacing:" + webCamDevice.isFrontFacing);
                     */
                    if (colors == null || colors.Length != webCamTexture.width * webCamTexture.height)
                    {
                        colors = new Color32[webCamTexture.width * webCamTexture.height];
                    }

                    frameMat          = new Mat(webCamTexture.height, webCamTexture.width, CvType.CV_8UC4);
                    screenOrientation = Screen.orientation;
                    screenWidth       = Screen.width;
                    screenHeight      = Screen.height;

                    bool isRotatedFrame = false;
#if !UNITY_EDITOR && !(UNITY_STANDALONE || UNITY_WEBGL)
                    if (screenOrientation == ScreenOrientation.Portrait || screenOrientation == ScreenOrientation.PortraitUpsideDown)
                    {
                        if (!rotate90Degree)
                        {
                            isRotatedFrame = true;
                        }
                    }
                    else if (rotate90Degree)
                    {
                        isRotatedFrame = true;
                    }
#else
                    if (rotate90Degree)
                    {
                        isRotatedFrame = true;
                    }
#endif
                    if (isRotatedFrame)
                    {
                        rotatedFrameMat = new Mat(webCamTexture.width, webCamTexture.height, CvType.CV_8UC4);
                    }

                    isInitWaiting = false;
                    hasInitDone   = true;
                    initCoroutine = null;

                    if (onInitialized != null)
                    {
                        onInitialized.Invoke();
                    }

                    break;
                }
                else
                {
                    initFrameCount++;
                    yield return(null);
                }
            }

            if (isTimeout)
            {
                webCamTexture.Stop();
                webCamTexture = null;
                isInitWaiting = false;
                initCoroutine = null;

                if (onErrorOccurred != null)
                {
                    onErrorOccurred.Invoke(ErrorCode.TIMEOUT);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes this instance by coroutine.
        /// </summary>
        protected virtual IEnumerator _Initialize()
        {
            if (hasInitDone)
            {
                ReleaseResources();

                if (onDisposed != null)
                {
                    onDisposed.Invoke();
                }
            }

            isInitWaiting = true;


            bool   hasFilePathCoroutineCompleted = false;
            string fullPath = string.Empty;

            getFilePathCoroutine = Utils.getFilePathAsync(requestedVideoFilePath, (result) =>
            {
                hasFilePathCoroutineCompleted = true;
                fullPath = result;
            });
            StartCoroutine(getFilePathCoroutine);


            int  initFrameCount = 0;
            bool isTimeout      = false;

            while (true)
            {
                if (initFrameCount > timeoutFrameCount)
                {
                    isTimeout = true;
                    break;
                }
                else if (hasFilePathCoroutineCompleted)
                {
                    if (string.IsNullOrEmpty(fullPath))
                    {
                        videoCapture         = null;
                        isInitWaiting        = false;
                        initCoroutine        = null;
                        getFilePathCoroutine = null;

                        if (onErrorOccurred != null)
                        {
                            onErrorOccurred.Invoke(ErrorCode.VIDEO_FILE_NOT_EXIST);
                        }

                        yield break;
                    }

                    videoCapture = new VideoCapture();
                    videoCapture.open(fullPath);

                    if (!videoCapture.isOpened())
                    {
                        videoCapture.Dispose();
                        videoCapture         = null;
                        isInitWaiting        = false;
                        initCoroutine        = null;
                        getFilePathCoroutine = null;

                        if (onErrorOccurred != null)
                        {
                            onErrorOccurred.Invoke(ErrorCode.VIDEO_FILE_CANT_OPEN);
                        }

                        yield break;
                    }

                    imageBufferMat = new Mat();
                    videoCapture.read(imageBufferMat);
                    videoCapture.set(Videoio.CAP_PROP_POS_FRAMES, 0);
                    videoCapture.grab();

                    baseMat = new Mat(imageBufferMat.rows(), imageBufferMat.cols(), imageBufferMat.type());
                    imageBufferMat.copyTo(baseMat);

                    if (baseColorFormat == outputColorFormat)
                    {
                        frameMat = baseMat;
                    }
                    else
                    {
                        frameMat = new Mat(baseMat.rows(), baseMat.cols(), CvType.CV_8UC(Channels(outputColorFormat)));
                    }

                    isPlaying = true;

                    Debug.Log("VideoCaptrueToMatHelper:: " + " filePath:" + requestedVideoFilePath + " width:" + frameMat.width() + " height:" + frameMat.height() + " fps:" + videoCapture.get(Videoio.CAP_PROP_FPS));

                    isInitWaiting        = false;
                    hasInitDone          = true;
                    initCoroutine        = null;
                    getFilePathCoroutine = null;

                    if (onInitialized != null)
                    {
                        onInitialized.Invoke();
                    }

                    StartWaitFrameTimeThread();

                    break;
                }
                else
                {
                    initFrameCount++;
                    yield return(null);
                }
            }

            if (isTimeout)
            {
                videoCapture         = null;
                isInitWaiting        = false;
                initCoroutine        = null;
                getFilePathCoroutine = null;

                if (onErrorOccurred != null)
                {
                    onErrorOccurred.Invoke(ErrorCode.TIMEOUT);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initializes this instance by coroutine.
        /// </summary>
        protected virtual IEnumerator _Initialize()
        {
            if (hasInitDone)
            {
                ReleaseResources();

                if (onDisposed != null)
                {
                    onDisposed.Invoke();
                }
            }

            isInitWaiting = true;

#if (UNITY_IOS || UNITY_ANDROID) && !UNITY_EDITOR
            // Checks camera permission state.
            IEnumerator coroutine = hasUserAuthorizedCameraPermission();
            yield return(coroutine);

            if (!(bool)coroutine.Current)
            {
                isInitWaiting = false;
                initCoroutine = null;

                if (onErrorOccurred != null)
                {
                    onErrorOccurred.Invoke(ErrorCode.CAMERA_PERMISSION_DENIED);
                }

                yield break;
            }
#endif

            float requestedFPS = this.requestedFPS;

            // Creates the camera
            var devices = WebCamTexture.devices;
            if (!String.IsNullOrEmpty(requestedDeviceName))
            {
                int requestedDeviceIndex = -1;
                if (Int32.TryParse(requestedDeviceName, out requestedDeviceIndex))
                {
                    if (requestedDeviceIndex >= 0 && requestedDeviceIndex < devices.Length)
                    {
                        webCamDevice = devices[requestedDeviceIndex];

                        if (avoidAndroidFrontCameraLowLightIssue && webCamDevice.isFrontFacing == true)
                        {
                            requestedFPS = 15f;
                        }

                        if (requestedFPS < 0)
                        {
                            webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight);
                        }
                        else
                        {
                            webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight, (int)requestedFPS);
                        }
                    }
                }
                else
                {
                    for (int cameraIndex = 0; cameraIndex < devices.Length; cameraIndex++)
                    {
                        if (devices[cameraIndex].name == requestedDeviceName)
                        {
                            webCamDevice = devices[cameraIndex];

                            if (avoidAndroidFrontCameraLowLightIssue && webCamDevice.isFrontFacing == true)
                            {
                                requestedFPS = 15f;
                            }

                            if (requestedFPS < 0)
                            {
                                webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight);
                            }
                            else
                            {
                                webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight, (int)requestedFPS);
                            }
                            break;
                        }
                    }
                }
                if (webCamTexture == null)
                {
                    Debug.Log("Cannot find camera device " + requestedDeviceName + ".");
                }
            }

            if (webCamTexture == null)
            {
                // Checks how many and which cameras are available on the device
                for (int cameraIndex = 0; cameraIndex < devices.Length; cameraIndex++)
                {
#if UNITY_2018_3_OR_NEWER
                    if (devices[cameraIndex].kind != WebCamKind.ColorAndDepth && devices[cameraIndex].isFrontFacing == requestedIsFrontFacing)
#else
                    if (devices[cameraIndex].isFrontFacing == requestedIsFrontFacing)
#endif
                    {
                        webCamDevice = devices[cameraIndex];

                        if (avoidAndroidFrontCameraLowLightIssue && webCamDevice.isFrontFacing == true)
                        {
                            requestedFPS = 15f;
                        }

                        if (requestedFPS < 0)
                        {
                            webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight);
                        }
                        else
                        {
                            webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight, (int)requestedFPS);
                        }
                        break;
                    }
                }
            }

            if (webCamTexture == null)
            {
                if (devices.Length > 0)
                {
                    webCamDevice = devices[0];

                    if (avoidAndroidFrontCameraLowLightIssue && webCamDevice.isFrontFacing == true)
                    {
                        requestedFPS = 15f;
                    }

                    if (requestedFPS < 0)
                    {
                        webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight);
                    }
                    else
                    {
                        webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight, (int)requestedFPS);
                    }
                }
                else
                {
                    isInitWaiting = false;
                    initCoroutine = null;

                    if (onErrorOccurred != null)
                    {
                        onErrorOccurred.Invoke(ErrorCode.CAMERA_DEVICE_NOT_EXIST);
                    }

                    yield break;
                }
            }

            // Starts the camera
            webCamTexture.Play();

            int  initFrameCount = 0;
            bool isTimeout      = false;

            while (true)
            {
                if (initFrameCount > timeoutFrameCount)
                {
                    isTimeout = true;
                    break;
                }
                else if (webCamTexture.didUpdateThisFrame)
                {
                    Debug.Log("WebCamTextureToMatHelper:: " + "devicename:" + webCamTexture.deviceName + " name:" + webCamTexture.name + " width:" + webCamTexture.width + " height:" + webCamTexture.height + " fps:" + webCamTexture.requestedFPS
                              + " videoRotationAngle:" + webCamTexture.videoRotationAngle + " videoVerticallyMirrored:" + webCamTexture.videoVerticallyMirrored + " isFrongFacing:" + webCamDevice.isFrontFacing);

                    if (colors == null || colors.Length != webCamTexture.width * webCamTexture.height)
                    {
                        colors = new Color32[webCamTexture.width * webCamTexture.height];
                    }

                    frameMat          = new Mat(webCamTexture.height, webCamTexture.width, CvType.CV_8UC4);
                    screenOrientation = Screen.orientation;
                    screenWidth       = Screen.width;
                    screenHeight      = Screen.height;

                    bool isRotatedFrame = false;
#if !UNITY_EDITOR && !(UNITY_STANDALONE || UNITY_WEBGL)
                    if (screenOrientation == ScreenOrientation.Portrait || screenOrientation == ScreenOrientation.PortraitUpsideDown)
                    {
                        if (!rotate90Degree)
                        {
                            isRotatedFrame = true;
                        }
                    }
                    else if (rotate90Degree)
                    {
                        isRotatedFrame = true;
                    }
#else
                    if (rotate90Degree)
                    {
                        isRotatedFrame = true;
                    }
#endif
                    if (isRotatedFrame)
                    {
                        rotatedFrameMat = new Mat(webCamTexture.width, webCamTexture.height, CvType.CV_8UC4);
                    }

                    isInitWaiting = false;
                    hasInitDone   = true;
                    initCoroutine = null;

                    if (onInitialized != null)
                    {
                        onInitialized.Invoke();
                    }

                    break;
                }
                else
                {
                    initFrameCount++;
                    yield return(null);
                }
            }

            if (isTimeout)
            {
                webCamTexture.Stop();
                webCamTexture = null;
                isInitWaiting = false;
                initCoroutine = null;

                if (onErrorOccurred != null)
                {
                    onErrorOccurred.Invoke(ErrorCode.TIMEOUT);
                }
            }
        }