initializeCamera static method
Future<void>
initializeCamera({
- required CameraLens initialLens,
- required dynamic onError(
- dynamic
- ImageResolution imageResolution = ImageResolution.high,
- CameraFlashMode defaultFlashMode = CameraFlashMode.off,
- IndicatorShape indicatorShape = IndicatorShape.defaultShape,
- AccuracyConfig? accuracyConfig,
Initialize camera and start face detection
Implementation
static Future<void> initializeCamera({
required CameraLens initialLens,
required Function(dynamic) onError,
ImageResolution imageResolution = ImageResolution.high,
CameraFlashMode defaultFlashMode = CameraFlashMode.off,
IndicatorShape indicatorShape = IndicatorShape.defaultShape,
AccuracyConfig? accuracyConfig,
}) async {
try {
// Store the configuration
_accuracyConfig = accuracyConfig;
_imageResolution = imageResolution;
_defaultFlashMode = defaultFlashMode;
_indicatorShape = indicatorShape;
// Clean up any existing camera resources first
_faceDetectionTimer?.cancel();
if (_cameraController != null) {
if (_cameraController!.value.isInitialized) {
await _cameraController!.dispose();
}
_cameraController = null;
}
// Reset face detection values
faceDetected.value = false;
statusMessage.value = '';
faceLeft.value = null;
faceTop.value = null;
faceWidth.value = null;
faceHeight.value = null;
// Initialize face detection first
await initialize();
// Initialize cameras after face detection is ready
_cameras = await availableCameras();
if (_cameras.isEmpty) {
onError('No cameras available');
return;
}
// Select initial camera
final initialCamera = initialLens == CameraLens.front
? _cameras.firstWhere(
(camera) => camera.lensDirection == CameraLensDirection.front,
orElse: () => _cameras.first)
: _cameras.firstWhere(
(camera) => camera.lensDirection == CameraLensDirection.back,
orElse: () => _cameras.first);
// Map ImageResolution to ResolutionPreset
ResolutionPreset resolutionPreset =
_mapResolutionToPreset(_imageResolution);
// Initialize camera controller with resolution
_cameraController = CameraController(
initialCamera,
resolutionPreset,
enableAudio: false,
);
await _cameraController!.initialize();
// Apply flash mode settings
await _applyInitialFlashMode();
// Start face detection after ensuring everything is initialized
await Future.delayed(const Duration(milliseconds: 1000));
startFaceDetection();
} catch (e) {
print('Camera initialization error: $e');
onError(e);
}
}