// Opens the camera specified by {@link Camera2BasicFragment#mCameraId}.
        public void OpenCamera(int width, int height)
        {
            SetUpCameraOutputs(width, height);
            ConfigureTransform(width, height);
            var activity = Activity;
            var manager  = (CameraManager)activity.GetSystemService(Context.CameraService);

            try
            {
                if (CameraOpenCloseLock.TryAcquire(2500, TimeUnit.Milliseconds) == false)
                {
                    throw new RuntimeException("Time out waiting to lock camera opening.");
                }
                if (string.IsNullOrEmpty(_cameraId) == false)
                {
                    manager.OpenCamera(_cameraId, _stateCallback, BackgroundHandler);
                }
            }
            catch (Java.Lang.SecurityException ex)
            {
#if DEBUG
                ex.PrintStackTrace();
#endif
            }
            catch (CameraAccessException ex)
            {
#if DEBUG
                ex.PrintStackTrace();
#endif
            }
            catch (InterruptedException ex)
            {
                throw new RuntimeException("Interrupted while trying to lock camera opening.", ex);
            }
        }
        // Closes the current {@link CameraDevice}.
        protected void CloseCamera()
        {
            if (CaptureSession == null)
            {
                return;
            }

            try
            {
                CameraOpenCloseLock.Acquire();
                if (null != CaptureSession)
                {
                    try
                    {
                        CaptureSession.StopRepeating();
                        CaptureSession.AbortCaptures();
                    }
                    catch (CameraAccessException ex)
                    {
#if DEBUG
                        ex.PrintStackTrace();
#endif
                    }
                    catch (IllegalStateException ex)
                    {
#if DEBUG
                        ex.PrintStackTrace();
#endif
                    }

                    CaptureSession.Close();
                    CaptureSession = null;
                }

                if (null != CameraDevice)
                {
                    CameraDevice.Close();
                    CameraDevice = null;
                }

                if (null != _imageReader)
                {
                    _imageReader.Close();
                    _imageReader = null;
                }
            }
            catch (InterruptedException ex)
            {
                throw new RuntimeException("Interrupted while trying to lock camera closing.", ex);
            }
            finally
            {
                CameraOpenCloseLock.Release();
            }
        }
Beispiel #3
0
        // Undone: Call from Dispose()
        /// <inheritdoc />
        protected override async Task CloseCamera()
        {
            try
            {
                await CameraOpenCloseLock.WaitAsync().ConfigureAwait(false);

                await base.CloseCamera().ConfigureAwait(false);

                if (_imageReader != null)
                {
                    _imageReader.Close();
                    _imageReader = null;
                }
            }
            catch (InterruptedException e)
            {
                throw new RuntimeException("Interrupted while trying to lock camera closing.", e);
            }
            finally
            {
                CameraOpenCloseLock.Release();
            }
        }