Пример #1
0
        static Task ToggleTorchAsync(bool switchOn)
        {
            return(Task.Run(() =>
            {
                lock (locker)
                {
                    if (Platform.HasApiLevel(BuildVersionCodes.M) && !AlwaysUseCameraApi)
                    {
                        var cameraManager = Platform.CameraManager;
                        foreach (var id in cameraManager.GetCameraIdList())
                        {
                            var hasFlash = cameraManager.GetCameraCharacteristics(id).Get(CameraCharacteristics.FlashInfoAvailable);
                            if (Java.Lang.Boolean.True.Equals(hasFlash))
                            {
                                cameraManager.SetTorchMode(id, switchOn);
                                break;
                            }
                        }
                    }
                    else
                    {
                        if (camera == null)
                        {
                            if (surface == null)
                            {
                                surface = new SurfaceTexture(0);
                            }

#pragma warning disable CS0618 // Camera types are deprecated in Android 10+
                            camera = Camera.Open();

                            // Nexus 5 and some devices require a preview texture
                            camera.SetPreviewTexture(surface);
                        }

                        var param = camera.GetParameters();

                        // Deprecated in an earlier android version
                        param.FlashMode = switchOn ? Camera.Parameters.FlashModeTorch : Camera.Parameters.FlashModeOff;

                        camera.SetParameters(param);

                        if (switchOn)
                        {
                            camera.StartPreview();
                        }
                        else
                        {
                            camera.StopPreview();
                            camera.Release();
                            camera.Dispose();
#pragma warning restore CS0618 // Type or member is obsolete
                            camera = null;
                            surface.Dispose();
                            surface = null;
                        }
                    }
                }
            }));
        }
Пример #2
0
        private void StopCamera()
        {
            camera.SetPreviewCallback(null);

            camera.StopPreview();
            camera.Release();
            camera.Dispose();
            camera = null;
        }
Пример #3
0
        public void SurfaceDestroyed(ISurfaceHolder holder)
        {
            Logger.LogInfo(nameof(SurfaceHolderCallback), nameof(SurfaceDestroyed), "called.");

            _Camera.StopPreview();
            _Camera.Release();
            _Camera.Dispose();
            _Camera = null;
        }
Пример #4
0
        Task ToggleTorchAsync(bool switchOn)
        {
            return(Task.Run(() =>
            {
                lock (locker)
                {
                    if (OperatingSystem.IsAndroidVersionAtLeast((int)BuildVersionCodes.M) && !AlwaysUseCameraApi)
                    {
                        foreach (var id in CameraManager.GetCameraIdList())
                        {
                            var hasFlash = CameraManager.GetCameraCharacteristics(id).Get(CameraCharacteristics.FlashInfoAvailable);
                            if (Java.Lang.Boolean.True.Equals(hasFlash))
                            {
                                try
                                {
                                    CameraManager.SetTorchMode(id, switchOn);
                                    break;
                                }
                                catch (Exception ex)
                                {
                                    System.Diagnostics.Debug.WriteLine($"Unable to SetTorchMode on {id}: {ex.Message}");
                                }
                            }
                        }
                    }
                    else
                    {
                        if (camera == null)
                        {
                            if (surface == null)
                            {
                                surface = new SurfaceTexture(0);
                            }

#pragma warning disable CS0618 // Camera types are deprecated in Android 10+
                            camera = Camera.Open();

                            // Nexus 5 and some devices require a preview texture
                            camera.SetPreviewTexture(surface);
                        }

                        var param = camera.GetParameters();

                        // Deprecated in an earlier android version
                        param.FlashMode = switchOn ? Camera.Parameters.FlashModeTorch : Camera.Parameters.FlashModeOff;

                        camera.SetParameters(param);

                        if (switchOn)
                        {
                            camera.StartPreview();
                        }
                        else
                        {
                            camera.StopPreview();
                            camera.Release();
                            camera.Dispose();
#pragma warning restore CS0618 // Type or member is obsolete
                            camera = null;
                            surface.Dispose();
                            surface = null;
                        }
                    }
                }
            }));
        }
Пример #5
0
 public void SurfaceDestroyed(ISurfaceHolder holder)
 {
     _camera.Dispose();
 }