Example #1
0
 public override void OnError(CameraDevice camera, CameraError error)
 {
     recordManager.cameraOpenCloseLock.Release();
     camera.Close();
     recordManager.cameraDevice = null;
     if (null != activity)
     {
         activity.Finish();
     }
 }
        public void openCamera(int width, int height, CameraFacing cameraFacing = CameraFacing.FRONT)
        {
            if (null == this || activity.IsFinishing)
            {
                return;
            }

            manager = (CameraManager)activity.GetSystemService(Context.CameraService);
            try
            {
                if (!cameraOpenCloseLock.TryAcquire(2500, TimeUnit.Milliseconds))
                {
                    throw new RuntimeException("Time out waiting to lock camera opening.");
                }
                string cameraId = null;
                CameraCharacteristics characteristics = null;
                var cameraList = manager.GetCameraIdList();
                if (cameraList != null && cameraList.Length >= 0)
                {
                    if (cameraList.Length > 1)
                    {
                        if (cameraFacing == CameraFacing.BACK)
                        {
                            LensFacing      = CameraFacing.BACK;
                            cameraId        = cameraList[0];
                            characteristics = manager.GetCameraCharacteristics(cameraId);
                        }
                        else if (cameraFacing == CameraFacing.FRONT)
                        {
                            LensFacing      = CameraFacing.FRONT;
                            cameraId        = cameraList[1];
                            characteristics = manager.GetCameraCharacteristics(cameraId);
                        }
                    }
                    else if (cameraList.Length == 1)
                    {
                        cameraFacing = CameraFacing.BACK;
                        activity.imgSwitchCamera.Visibility = ViewStates.Gone;
                        cameraId        = cameraList[0];
                        characteristics = manager.GetCameraCharacteristics(cameraId);
                    }
                }
                else
                {
                    activity.imgSwitchCamera.Visibility = ViewStates.Gone;
                    Toast.MakeText(activity, "Camera not avaiable", ToastLength.Short);
                    activity.Finish();
                    return;
                }
                configureTransform(width, height);
                if (!string.IsNullOrEmpty(cameraId))
                {
                    manager.OpenCamera(cameraId, stateListener, null);
                }
                SetUpCameraOutputs(cameraId);
            }
            catch (CameraAccessException ex)
            {
                Toast.MakeText(activity, "Cannot access the camera.", ToastLength.Short).Show();
                activity.Finish();
            }
            catch (NullPointerException)
            {
            }
            catch (InterruptedException)
            {
                throw new RuntimeException("Interrupted while trying to lock camera opening.");
            }
        }