FaceDetectionBloc constructor

FaceDetectionBloc({
  1. void onFaceSnapshot(
    1. List<Face>
    )?,
  2. CameraController? cameraController,
})

Implementation

FaceDetectionBloc({this.onFaceSnapshot, this.cameraController})
    : super(const FaceDetectionState.initial()) {
  on<FaceDetectionEvent>((event, emit) async {
    await event.when(
      initializeCam: () async {
        description ??= await getCamera(camDirection);
        if (description == null) {
          emit(const FaceDetectionState.noCameraOnDevice());
        } else {
          _cameraController = cameraController ??
              CameraController(description!, ResolutionPreset.low);
          await _cameraController?.initialize();

          _cameraController?.startImageStream((CameraImage image) async {
            if (isDetecting) return;
            isDetecting = true;
            final facesList = await Future.microtask(
              () => detect(
                image,
                handleDetection.processImage,
                description!.sensorOrientation,
              ),
            );
            if (facesList.isNotEmpty) {
              add(const FaceDetectionEvent.faceDetected());
              if (onFaceSnapshot != null) {
                final completer = Completer();
                completer.complete([
                  onFaceSnapshot!(facesList),
                ]);
              }
            } else if (facesList.isEmpty ||
                !_cameraController!.value.isInitialized) {
              add(const FaceDetectionEvent.noFace());
            }
            isDetecting = false;
          });
        }
      },
      faceDetected: () async {
        if (state != const FaceDetectionState.faceDetected()) {
          emit(const FaceDetectionState.faceDetected());
        }
      },
      noFace: () async {
        if (state != const FaceDetectionState.noFace()) {
          emit(const FaceDetectionState.noFace());
        }
      },
    );
  });
}