public void StartPreview()
 {
     try
     {
         var numberOfCameras = Camera.NumberOfCameras;
         int? rearFacingCameraId = null;
         // Find the ID of the default camera
         Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
         for (int i = 0; i < numberOfCameras; i++)
         {
             Camera.GetCameraInfo(i, cameraInfo);
             if (cameraInfo.Facing == CameraFacing.Back)
             {
                 rearFacingCameraId = i;
             }
         }
         if (rearFacingCameraId.HasValue)
         {
             camera = Camera.Open(rearFacingCameraId.Value);
             if (cameraPreview != null)
             {
                 cameraPreview.PreviewCamera = camera;
             }
         }
     }
     catch (CameraAccessException ex)
     {
     }
     catch (NullPointerException)
     {
     }
     catch (System.Exception ex)
     {
     }
 }
Exemple #2
0
        /// <summary>
        /// Open the camera.
        /// </summary>
        /// <returns>The opened camera.</returns>
        private Camera OpenCameraFunc(out int cameraOrientation)
        {
            Camera camera = null;
            cameraOrientation = 0;

            var cameraInfo = new Camera.CameraInfo();
            int cameraCount = Camera.NumberOfCameras;
            for (int camId = 0; camId < cameraCount; camId++)
            {
                Camera.GetCameraInfo(camId, cameraInfo);
                if (cameraInfo.Facing == CameraFacing.Back)
                {
                    try
                    {
                        camera = Camera.Open(camId);
                        cameraOrientation = cameraInfo.Orientation;
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine("Exception in OpenCamera {0}", ex, null);
                    }
                }
            }

            return camera;
        }
		public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
		{
			// Open an instance of the first camera and retrieve its info.
			camera = GetCameraInstance (CAMERA_ID);
			Camera.CameraInfo cameraInfo = null;

			if (camera != null) {
				// Get camera info only if the camera is available
				cameraInfo = new Camera.CameraInfo ();
				Camera.GetCameraInfo (CAMERA_ID, cameraInfo);
			}

			if (camera == null || cameraInfo == null) {
				Toast.MakeText (Activity, "Camera is not available.", ToastLength.Short).Show ();
				return inflater.Inflate (Resource.Layout.fragment_camera_unavailable, null);
			}

			View root = inflater.Inflate (Resource.Layout.fragment_camera, null);

			// Get the rotation of the screen to adjust the preview image accordingly.
			SurfaceOrientation displayRotation = Activity.WindowManager.DefaultDisplay.Rotation;

			// Create the Preview view and set it as the content of this Activity.
			cameraPreview = new CameraPreview (Activity, camera, cameraInfo, displayRotation);
			var preview =  root.FindViewById <FrameLayout> (Resource.Id.camera_preview);
			preview.AddView (cameraPreview);

			return root;
		}
		public CameraPreview (Context context, Camera camera, Camera.CameraInfo cameraInfo, 
			SurfaceOrientation displayOrientation) :
			base (context)
		{
			// Do not initialize if no camera has been set
			if (camera == null || cameraInfo == null)
				return;
			
			this.camera = camera;
			this.cameraInfo = cameraInfo;
			this.displayOrientation = displayOrientation;

			// Install a SurfaceHolder.Callback so we get notified when the
			// underlying surface is created and destroyed.
			holder = Holder;
			holder.AddCallback (this);
		}
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate (savedInstanceState);

            // Create a RelativeLayout container that will hold a SurfaceView,
            // and set it as the content of our activity.
            mPreview = new Preview (this.Activity);

            // Find the total number of cameras available
            mNumberOfCameras = Camera.NumberOfCameras;

            // Find the ID of the default camera
            Camera.CameraInfo cameraInfo = new Camera.CameraInfo ();
            for (int i = 0; i < mNumberOfCameras; i++) {
                Camera.GetCameraInfo (i, cameraInfo);
                if (cameraInfo.Facing == Camera.CameraInfo.CameraFacingBack) {
                    mDefaultCameraId = i;
                }
            }
            SetHasOptionsMenu (mNumberOfCameras > 1);
        }
		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);

			// Hide the window title and go fullscreen.
			RequestWindowFeature (WindowFeatures.NoTitle);
			Window.AddFlags (WindowManagerFlags.Fullscreen);

			// Create our Preview view and set it as the content of our activity.
			mPreview = new Preview (this);
			SetContentView (mPreview);

			// Find the total number of cameras available
			numberOfCameras = Camera.NumberOfCameras;

			// Find the ID of the default camera
			Camera.CameraInfo cameraInfo = new Camera.CameraInfo ();
			for (int i = 0; i < numberOfCameras; i++) {
				Camera.GetCameraInfo (i, cameraInfo);
				if (cameraInfo.Facing == CameraFacing.Back) {
					defaultCameraId = i;
				}
			}
		}
      private bool CreateCamera(int cameraIndex)
      {
#if !DEBUG
         try
#endif
         {
            StopCamera();
            _camera = Camera.Open(cameraIndex);
            _camera.SetPreviewDisplay(_surfaceHolder);
            if (OnCameraCreated != null)
            {
               if (_cameraInfo == null)
                  _cameraInfo = new Android.Hardware.Camera.CameraInfo();

               Camera.GetCameraInfo(cameraIndex, _cameraInfo);
               OnCameraCreated(this, new CameraInfoEventArgs(_camera, _cameraInfo));
            }
         }
#if !DEBUG
         catch (Exception excpt)
         {
            _camera.Release();
            _camera = null;

            while (excpt.InnerException != null)
               excpt = excpt.InnerException;
            
            Report.Error(Context.PackageName, String.Format("Unable to create camera: {0}", excpt.Message));

            return false;
            // TODO: add more exception handling logic here
         }
#endif

         return true;
      }
 public CameraInfoEventArgs(Camera camera, Camera.CameraInfo cameraInfo)
 {
    _camera = camera;
    _cameraInfo = cameraInfo;
 }