Exemplo n.º 1
0
        //public void RefreshCamera()
        //{
        //    if (_holder == null) return;

        //    ApplyCameraSettings();

        //    try
        //    {
        //        Camera.SetPreviewDisplay(_holder);
        //        Camera.StartPreview();
        //    }
        //    catch (Exception ex)
        //    {
        //        Android.Util.Log.Debug(nameof(CameraController), ex.ToString());
        //    }
        //}



        public void SetupCamera(CameraFacing facing)
        {
            if (Camera != null)
            {
                return;
            }

            PermissionsHandler.CheckCameraPermissions(_context);


            OpenCamera(facing);


            if (Camera == null)
            {
                return;
            }


            ApplyCameraSettings();

            try
            {
                Camera.SetPreviewDisplay(_holder);


                //var previewParameters = Camera.GetParameters();
                //var previewSize = previewParameters.PreviewSize;
                //var bitsPerPixel = ImageFormat.GetBitsPerPixel(previewParameters.PreviewFormat);


                //int bufferSize = (previewSize.Width * previewSize.Height * bitsPerPixel) / 8;
                //const int NUM_PREVIEW_BUFFERS = 5;
                //for (uint i = 0; i < NUM_PREVIEW_BUFFERS; ++i)
                //{
                //    using (var buffer = new FastJavaByteArray(bufferSize))
                //        Camera.AddCallbackBuffer(buffer);
                //}



                Camera.StartPreview();

                // Camera.SetNonMarshalingPreviewCallback(_cameraEventListener);
            }
            catch (Exception ex)
            {
                Android.Util.Log.Debug(nameof(CameraController), ex.ToString());
                return;
            }

            // Docs suggest if Auto or Macro modes, we should invoke AutoFocus at least once
            var currentFocusMode = Camera.GetParameters().FocusMode;

            if (currentFocusMode == Camera.Parameters.FocusModeAuto ||
                currentFocusMode == Camera.Parameters.FocusModeMacro)
            {
                AutoFocus();
            }
        }
        protected async override void OnElementChanged(ElementChangedEventArgs <CameraPreviewerView> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
            {
                if (Context is Activity activity)
                {
                    await PermissionsHandler.RequestPermissionsAsync(activity);
                }


                cameraPreview      = new DroidCameraView(Context);
                cameraPreview.Type = Element.Camera;
                SetNativeControl(cameraPreview);
                Element.Init(cameraPreview);
            }
        }