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; } } } })); }
public void OnSurfaceChanged(IGL10 unused, int width, int height) { // Store width and height. _width = width; _height = height; // Calculate view aspect ratio. _aspectRatio[0] = (float)System.Math.Min(_width, _height) / _width; _aspectRatio[1] = (float)System.Math.Min(_width, _height) / _height; // Initialize textures. if (_fboExternal.Width != _width || _fboExternal.Height != _height) { _fboExternal.init(_width, _height, 1, true); } if (_fboOffscreen.Width != _width || _fboOffscreen.Height != _height) { _fboOffscreen.init(_width, _height, 1, false); } // Allocate new SurfaceTexture. SurfaceTexture oldSurfaceTexture = _surfaceTexture; _surfaceTexture = new SurfaceTexture(_fboExternal.GetTexture(0)); _surfaceTexture.SetOnFrameAvailableListener(this); // TODO: Ici vs OnFrameAvailable _surfaceTexture.FrameAvailable += _surfaceTexture_FrameAvailable; //???? Remove if (_observer != null) { _observer.OnSurfaceTextureCreated(_surfaceTexture); } if (oldSurfaceTexture != null) { oldSurfaceTexture.Dispose(); // Originally Release. Don't know the Xamarin translation of android java texture disposing. } RequestRender(); }
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; } } } })); }