Example #1
0
        public async void SurfaceChanged(ISurfaceHolder holder, global::Android.Graphics.Format format, int width, int height)
        {
            if (!HasValidSurface)
            {
                return;
            }

            // portrait mode
            if (height > width)
            {
                CameraService.SetViewSize(height, width);
            }
            else
            {
                CameraService.SetViewSize(width, height);
            }

            if (!Element.IsEnabled)
            {
                return;
            }

            CameraService.HaltPreview();
            await Task.Run(() => {
                CameraService.StartPreview(holder);
            });
        }
Example #2
0
        protected async override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (e.PropertyName == Xamarin.Forms.VisualElement.IsEnabledProperty.PropertyName)
            {
                if (Element.IsEnabled)
                {
                    InitializeView();
                }

                if (!Element.IsEnabled)
                {
                    ReleaseCamera();
                }
            }

            if (e.PropertyName == BarcodeScanner.PreviewActiveProperty.PropertyName)
            {
                if (Element.PreviewActive)
                {
                    await Task.Run(() => CameraService.StartPreview(Control.Holder));
                }

                if (!Element.PreviewActive)
                {
                    CameraService.HaltPreview();
                }
            }

            if (e.PropertyName == BarcodeScanner.TorchProperty.PropertyName)
            {
                if (!Platform.HasFlashPermission)
                {
                    return;
                }

                if (!Platform.HasFlash)
                {
                    return;
                }

                CameraService.ToggleTorch(Element.Torch);
            }

            if (e.PropertyName == BarcodeScanner.BarcodeDecoderProperty.PropertyName)
            {
                if (Element.BarcodeDecoder)
                {
                    CameraService.StartDecoder();
                }

                if (!Element.BarcodeDecoder)
                {
                    CameraService.StopDecoder();
                }
            }
        }
        protected async override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            this.Debug("OnElementPropertyChanged");

            if (!HasValidSurface)
            {
                return;
            }

            if (e.PropertyName == BarcodeScanner.IsEnabledProperty.PropertyName)
            {
                this.Debug("Enabled [{0}]", Element.IsEnabled);

                if (Element.IsEnabled && HasValidSurface)
                {
                    await Task.Run(() => CameraService.OpenCamera());
                }

                if (!Element.IsEnabled)
                {
                    ReleaseCamera();
                }
            }

            if (e.PropertyName == BarcodeScanner.PreviewActiveProperty.PropertyName)
            {
                this.Debug("ScannerActive [{0}]", Element.PreviewActive);

                if (Element.PreviewActive)
                {
                    await Task.Run(() => CameraService.StartPreview(Control.Holder));
                }

                if (!Element.PreviewActive)
                {
                    CameraService.HaltPreview();
                }
            }

            if (e.PropertyName == BarcodeScanner.TorchProperty.PropertyName)
            {
                if (!Platform.HasFlashPermission)
                {
                    this.Debug("Unable to use flashlight: Android Manifest '{0}' permission not granted.", Android.Manifest.Permission.Flashlight);
                    return;
                }

                if (!Platform.HasFlash)
                {
                    this.Debug("Unable to use flashlight: Device's camera does not support flash.");
                    return;
                }

                this.Debug("Torch [{0}]", Element.Torch);

                CameraService.ToggleTorch(Element.Torch);
            }

            if (e.PropertyName == BarcodeScanner.BarcodeDecoderProperty.PropertyName)
            {
                this.Debug("Decoder state [{0}]", Element.BarcodeDecoder);

                if (Element.BarcodeDecoder)
                {
                    CameraService.StartDecoder();
                }

                if (!Element.BarcodeDecoder)
                {
                    CameraService.StopDecoder();
                }
            }
        }