public NatCamSource(int width, int height, int framerate, bool front)
        {
            requestedWidth     = width;
            requestedHeight    = height;
            requestedFramerate = framerate;

            // Check permission
            var devices = CameraDevice.GetDevices();

            if (devices == null)
            {
                Debug.Log("User has not granted camera permission");
                return;
            }
            // Pick camera
            for (; cameraIndex < devices.Length; cameraIndex++)
            {
                if (devices[cameraIndex].IsFrontFacing == front)
                {
                    break;
                }
            }

            if (cameraIndex == devices.Length)
            {
                Debug.LogError("Camera is null. Consider using " + (front ? "rear" : "front") + " camera.");
                return;
            }

            activeCamera = devices[cameraIndex];
            activeCamera.PreviewResolution = (width : requestedWidth, height : requestedHeight);
            activeCamera.Framerate         = requestedFramerate;
        }
        public void SwitchCamera()
        {
            if (activeCamera == null)
            {
                return;
            }

            if (activeCamera != null && activeCamera.IsRunning)
            {
                activeCamera.StopPreview();
            }

            var devices = CameraDevice.GetDevices();

            if (devices == null)
            {
                Debug.Log("User has not granted camera permission");
                return;
            }

            cameraIndex  = ++cameraIndex % devices.Length;
            activeCamera = devices[cameraIndex];
            activeCamera.PreviewResolution = (width : requestedWidth, height : requestedHeight);
            activeCamera.Framerate         = requestedFramerate;
            StartPreview(startCallback, frameCallback);
        }
Ejemplo n.º 3
0
        void Start()
        {
            // Check permission
            var cameras = CameraDevice.GetDevices();

            if (cameras == null)
            {
                Debug.Log("User has not granted camera permission");
                return;
            }
            // Pick camera
            CameraDevice deviceCamera = null;

            foreach (var camera in cameras)
            {
                if (camera.IsFrontFacing == useFrontCamera)
                {
                    deviceCamera = camera;
                    break;
                }
            }
            if (!deviceCamera)
            {
                Debug.LogError("Camera is null. Consider using " + (useFrontCamera ? "rear" : "front") + " camera");
                return;
            }
            // Start preview
            deviceCamera.PreviewResolution = new Resolution {
                width = 640, height = 480
            };
            deviceCamera.StartPreview(OnStart, OnFrame);
        }
Ejemplo n.º 4
0
 // Use this for initialization
 private void Start()
 {
     // Check permission
     cameras = CameraDevice.GetDevices();
     if (cameras == null)
     {
         Debug.Log("User has not granted camera permission");
         return;
     }
     // Pick camera
     for (var i = 0; i < cameras.Length; i++)
     {
         if (cameras[i].IsFrontFacing == useFrontCamera)
         {
             activeCamera = i;
             break;
         }
     }
     if (activeCamera == -1)
     {
         Debug.LogError("Camera is null. Consider using " + (useFrontCamera ? "rear" : "front") + " camera");
         return;
     }
     // Start preview
     cameras[activeCamera].StartPreview(OnStart);
 }
Ejemplo n.º 5
0
    // Invoked by Unity when the scene opens
    private void Start()
    {
        // Start the camera preview with NatCam
        var cameraDevice = CameraDevice.GetDevices()[0];

        cameraDevice.StartPreview(previewTexture => {
            rawImage.texture         = previewTexture;
            aspectFitter.aspectRatio = preview.width / (float)preview.height;
        });
    }
Ejemplo n.º 6
0
    public void StartCamera()
    {
        //   if (NatCamCamera == null) {
        //      Debug.Log("entra aqui?");
        // NatCamCamera = DeviceCamera.FrontCamera;
        //NatCamCamera.StartPreview(OnStart, OnFrame);
        //   NatCamCamera.StartPreview(OnStart);
        //  }


        Debug.Log("Kauel: StartCamera()");

        singleton = this;

        Mode = KameraMode.Camera;

        /////////////////////////////////////////////////////   RawImageCamera.texture = previewTexture;
        //RawImageCamera.texture = preview;
        //previewTexture = preview;

#if UNITY_EDITOR
        NatCamCamera = DeviceCamera.Cameras[0];
        ////////////////////////////////////////////////////// CamCreated = true;
#elif UNITY_ANDROID
        Debug.Log("Kauel: Android");

        NatCamCamera = DeviceCamera.RearCamera;


        /*  if (NatCam.HasPermissions) {
         *
         *    Debug.Log("Kauel: NatCam permissions OK :)");
         *
         * } else {
         *
         *    Debug.Log("Kauel: NatCam has NO PERMISSIONS!!!");
         *
         * }
         *
         * NatCamCamera = CameraDevice.GetDevices()[0];
         */

        // NatCamCamera = DeviceCamera.Cameras[0];
        if (!CamCreated)
        {
            //   CamCreated = true;
            //   NatCamCamera = DeviceCamera.Cameras[0];
            //   NatCamCamera = DeviceCamera.RearCamera;
            //   NatCamCamera.PreviewResolution.Set(1920, 1080);
            //   NatCamCamera.PhotoResolution.Set(1920, 1080);
            //   Debug.Log("Kauel: CamCreated");

            // NatCam.Camera = DeviceCamera.RearCamera;
            // var NatCamCamera = CameraDevice.GetDevices()[0];
            //  NatCamCamera = CameraDevice.GetDevices()[0];

            // NatCamCamera = DeviceCamera.RearCamera;

            //  NatCamCamera.SetPreviewResolution(ResolutionPreset.FullHD);
            //  NatCamCamera.SetPhotoResolution(ResolutionPreset.FullHD);
            //  NatCamCamera.SetFramerate(FrameratePreset.Default);

            //  Debug.Log("Kauel: CamCreated");
        }
#elif UNITY_IOS
        Debug.Log("Kauel: iOS");



        if (!CamCreated)
        {
            CamCreated = true;

            //NatCam.Camera = DeviceCamera.RearCamera;
            var NatCam.Camera = CameraDevice.GetDevices()[0];

            NatCam.Camera.SetPreviewResolution(ResolutionPreset.FullHD);

            NatCam.Camera.SetPhotoResolution(ResolutionPreset.FullHD);

            Debug.Log("Kauel: CamCreated");
        }
#endif



        NatCamCamera.StartPreview(OnStart);

        PanelSelector.ShowOnlyThisPanel(0); //Solo el panel de fotos

        Debug.Log("Kauel: StartCamera() ha finalizado");
    }
Ejemplo n.º 7
0
        /// <summary>
        /// Initializes this instance by coroutine.
        /// </summary>
        protected override IEnumerator _Initialize()
        {
            if (hasInitDone)
            {
                ReleaseResources();

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

            isInitWaiting = true;


#if UNITY_ANDROID && !UNITY_EDITOR
            // Wait for one frame until ready to accurately reflects current screen orientation to the direction of a camera image.
            yield return(null);
#endif

            // Checks camera permission state.
            if (CameraDevice.GetDevices() == null)
            {
                isInitWaiting = false;
                initCoroutine = null;

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

                yield break;
            }

            // Creates the camera
            var devices = CameraDevice.GetDevices();
            if (!String.IsNullOrEmpty(requestedDeviceName))
            {
                int requestedDeviceIndex = -1;
                if (Int32.TryParse(requestedDeviceName, out requestedDeviceIndex))
                {
                    if (requestedDeviceIndex >= 0 && requestedDeviceIndex < devices.Length)
                    {
                        natCamCameraDevice = devices[requestedDeviceIndex];
                    }
                }
                else
                {
                    for (int cameraIndex = 0; cameraIndex < devices.Length; cameraIndex++)
                    {
                        if (devices[cameraIndex].UniqueID == requestedDeviceName)
                        {
                            natCamCameraDevice = devices[cameraIndex];
                            break;
                        }
                    }
                }
                if (natCamCameraDevice == null)
                {
                    Debug.Log("Cannot find camera device " + requestedDeviceName + ".");
                }
            }

            if (natCamCameraDevice == null)
            {
                // Checks how many and which cameras are available on the device
                for (int cameraIndex = 0; cameraIndex < devices.Length; cameraIndex++)
                {
                    if (devices[cameraIndex].IsFrontFacing == requestedIsFrontFacing)
                    {
                        natCamCameraDevice = devices[cameraIndex];
                        break;
                    }
                }
            }

            if (natCamCameraDevice == null)
            {
                if (devices.Length > 0)
                {
                    natCamCameraDevice = devices[0];
                }
                else
                {
                    isInitWaiting = false;
                    initCoroutine = null;

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

                    yield break;
                }
            }

            natCamCameraDevice.Framerate = (int)requestedFPS;

            // Set the camera's preview resolution
            natCamCameraDevice.PreviewResolution = new Resolution {
                width = requestedWidth, height = requestedHeight
            };

            // Starts the camera
            // Register callback for when the preview starts
            // Register for preview updates
            natCamCameraDevice.StartPreview(OnStart, OnFrame);

            int  initFrameCount = 0;
            bool isTimeout      = false;

            while (true)
            {
                if (initFrameCount > timeoutFrameCount)
                {
                    isTimeout = true;
                    break;
                }
                else if (didUpdateThisFrame)
                {
                    Debug.Log("NatCamPreviewToMatHelper:: " + "UniqueID:" + natCamCameraDevice.UniqueID + " width:" + preview.width + " height:" + preview.height + " fps:" + natCamCameraDevice.Framerate
                              + " isFrongFacing:" + natCamCameraDevice.IsFrontFacing);

                    frameMat = new Mat(preview.height, preview.width, CvType.CV_8UC4);

                    screenOrientation = Screen.orientation;
                    screenWidth       = Screen.width;
                    screenHeight      = Screen.height;

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

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

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

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

            if (isTimeout)
            {
                if (natCamCameraDevice.IsRunning)
                {
                    natCamCameraDevice.StopPreview();
                }

                isInitWaiting = false;
                initCoroutine = null;

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