From 92cd26b50f0d9170334d13eacd1aa5bac2be375c Mon Sep 17 00:00:00 2001 From: Alex Li Date: Sun, 15 May 2022 14:17:43 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E2=AC=86=EF=B8=8F=20Upgrade=20`camera`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index fadcad0..f5fddbe 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,7 +12,7 @@ dependencies: sdk: flutter bindings_compatible: ^1.0.1 - camera: ^0.9.4+21 + camera: ^0.9.5 path: ^1.8.0 path_provider: ^2.0.8 photo_manager: ^2.1.0+2 From 79ef0c2ed2469c7fee084d64749096619c38ffe0 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Sun, 15 May 2022 14:18:24 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=8E=A8=20Handle=20controllers=20more?= =?UTF-8?q?=20graceful?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/src/widgets/camera_picker.dart | 65 +++++++++++++----------------- 1 file changed, 29 insertions(+), 36 deletions(-) diff --git a/lib/src/widgets/camera_picker.dart b/lib/src/widgets/camera_picker.dart index 05579fd..3ab258d 100644 --- a/lib/src/widgets/camera_picker.dart +++ b/lib/src/widgets/camera_picker.dart @@ -153,7 +153,6 @@ class CameraPickerState extends State /// 当前相机实例的控制器 CameraController get controller => _controller!; CameraController? _controller; - bool _shouldLockInitialize = false; /// Available cameras. /// 可用的相机实例 @@ -318,10 +317,7 @@ class CameraPickerState extends State } if (state == AppLifecycleState.inactive) { c.dispose(); - } else if (state == AppLifecycleState.resumed && !_shouldLockInitialize) { - // Drop initialize when the controller has been already initialized. - // This will typically resolve the lifecycle issue on iOS when permissions - // are requested for the first time. + } else if (state == AppLifecycleState.resumed) { initCameras(currentCamera); } } @@ -352,17 +348,20 @@ class CameraPickerState extends State /// Initialize cameras instances. /// 初始化相机实例 - void initCameras([CameraDescription? cameraDescription]) { + Future initCameras([CameraDescription? cameraDescription]) async { // Save the current controller to a local variable. final CameraController? c = _controller; - // Then unbind the controller from widgets, which requires a build frame. + // Dispose at last to avoid disposed usage with assertions. + if (c != null) { + _controller = null; + await c.dispose(); + } + // Then request a new frame to unbind the controller from elements. safeSetState(() { - _shouldLockInitialize = true; _maxAvailableZoom = 1; _minAvailableZoom = 1; _currentZoom = 1; _baseZoom = 1; - _controller = null; // Meanwhile, cancel the existed exposure point and mode display. _exposureModeDisplayTimer?.cancel(); _exposurePointDisplayTimer?.cancel(); @@ -374,9 +373,6 @@ class CameraPickerState extends State // **IMPORTANT**: Push methods into a post frame callback, which ensures the // controller has already unbind from widgets. useWidgetsBinding().addPostFrameCallback((_) async { - // Dispose at last to avoid disposed usage with assertions. - await c?.dispose(); - // When the [cameraDescription] is null, which means this is the first // time initializing cameras, so available cameras should be fetched. if (cameraDescription == null) { @@ -386,7 +382,6 @@ class CameraPickerState extends State // After cameras fetched, judge again with the list is empty or not to // ensure there is at least an available camera for use. if (cameraDescription == null && (cameras.isEmpty)) { - _shouldLockInitialize = false; handleErrorWithHandler( CameraException( 'No CameraDescription found.', @@ -408,49 +403,47 @@ class CameraPickerState extends State index = currentCameraIndex; } // Initialize the controller with the given resolution preset. - _controller = CameraController( + final CameraController newController = CameraController( cameraDescription ?? cameras[index], config.resolutionPreset, enableAudio: enableAudio, imageFormatGroup: config.imageFormatGroup, - )..addListener(() { - if (controller.value.hasError) { - handleErrorWithHandler( - CameraException( - 'CameraController exception', - controller.value.errorDescription, - ), - config.onError, - ); - } - }); + ); try { - await controller.initialize(); - safeSetState(() {}); + await newController.initialize(); + if (newController.value.hasError) { + handleErrorWithHandler( + CameraException( + 'CameraController exception', + controller.value.errorDescription, + ), + config.onError, + ); + return; + } // Call recording preparation first. if (shouldPrepareForVideoRecording) { - await controller.prepareForVideoRecording(); + await newController.prepareForVideoRecording(); } // Then call other asynchronous methods. await Future.wait(>[ if (config.lockCaptureOrientation != null) - controller.lockCaptureOrientation(config.lockCaptureOrientation), + newController.lockCaptureOrientation(config.lockCaptureOrientation), (() async => _maxAvailableExposureOffset = - await controller.getMaxExposureOffset())(), + await newController.getMaxExposureOffset())(), (() async => _minAvailableExposureOffset = - await controller.getMinExposureOffset())(), + await newController.getMinExposureOffset())(), (() async => - _maxAvailableZoom = await controller.getMaxZoomLevel())(), + _maxAvailableZoom = await newController.getMaxZoomLevel())(), (() async => - _minAvailableZoom = await controller.getMinZoomLevel())(), + _minAvailableZoom = await newController.getMinZoomLevel())(), ]); + _controller = newController; } catch (e, s) { handleErrorWithHandler(e, config.onError, s: s); } finally { - safeSetState(() { - _shouldLockInitialize = false; - }); + safeSetState(() {}); } }); } From 0613a1bc36a22470e3f7e7c26a38ba9dce0cc723 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Tue, 24 May 2022 13:09:09 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E2=AC=86=EF=B8=8F=20Use=20the=20latest=20`?= =?UTF-8?q?camera`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/src/widgets/camera_picker.dart | 10 ---------- pubspec.yaml | 2 +- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/lib/src/widgets/camera_picker.dart b/lib/src/widgets/camera_picker.dart index 3ab258d..928bb01 100644 --- a/lib/src/widgets/camera_picker.dart +++ b/lib/src/widgets/camera_picker.dart @@ -412,16 +412,6 @@ class CameraPickerState extends State try { await newController.initialize(); - if (newController.value.hasError) { - handleErrorWithHandler( - CameraException( - 'CameraController exception', - controller.value.errorDescription, - ), - config.onError, - ); - return; - } // Call recording preparation first. if (shouldPrepareForVideoRecording) { await newController.prepareForVideoRecording(); diff --git a/pubspec.yaml b/pubspec.yaml index f5fddbe..4d66d7d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,7 +12,7 @@ dependencies: sdk: flutter bindings_compatible: ^1.0.1 - camera: ^0.9.5 + camera: ^0.9.6 path: ^1.8.0 path_provider: ^2.0.8 photo_manager: ^2.1.0+2