Files
Edouard Marquez b55b6aa8d1 fix: Black camera screen (#1732)
* This should fix the black screen with the camera scanner

* Mark two methods as private

* A few more comments

* const constructor for CameraHelper

* Update packages/smooth_app/lib/pages/scan/lifecycle_manager.dart

Co-authored-by: monsieurtanuki <fabrice_fontaine@hotmail.com>

* Update packages/smooth_app/lib/pages/scan/lifecycle_manager.dart

Co-authored-by: monsieurtanuki <fabrice_fontaine@hotmail.com>

Co-authored-by: monsieurtanuki <fabrice_fontaine@hotmail.com>
2022-05-04 18:02:27 +02:00

52 lines
1.6 KiB
Dart

import 'package:camera/camera.dart';
class CameraHelper {
const CameraHelper._();
static List<CameraDescription>? _cameras;
/// Mandatory method to call before [findBestCamera]
static Future<void> init() async {
_cameras = await availableCameras();
}
/// Find the most relevant camera to use if none of these criteria are met,
/// the default value of [_cameraIndex] will be used to select the first
/// camera in the global cameras list.
/// if non matching is found we fall back to the first in the list
/// initValue of [_cameraIndex]/
static CameraDescription? findBestCamera({
CameraLensDirection cameraLensDirection = CameraLensDirection.back,
}) {
if (_cameras == null) {
throw Exception('Please call [init] before!');
} else if (_cameras!.isEmpty) {
return null;
}
int cameraIndex = -1;
if (_cameras!.any(
(CameraDescription element) =>
element.lensDirection == cameraLensDirection &&
element.sensorOrientation == 90,
)) {
cameraIndex = _cameras!.indexOf(
_cameras!.firstWhere((CameraDescription element) =>
element.lensDirection == cameraLensDirection &&
element.sensorOrientation == 90),
);
} else if (_cameras!.any((CameraDescription element) =>
element.lensDirection == cameraLensDirection)) {
cameraIndex = _cameras!.indexOf(
_cameras!.firstWhere(
(CameraDescription element) =>
element.lensDirection == cameraLensDirection,
),
);
}
return _cameras![cameraIndex];
}
}