startCapture method

Future<void> startCapture({
  1. ResolutionPreset resolution = ResolutionPreset.medium,
  2. int imageQuality = 90,
})

Implementation

Future<void> startCapture({
  ResolutionPreset resolution = ResolutionPreset.medium,
  int imageQuality = 90,
}) async {
  _latestImagePathNotifier.value = null;
  _counterTimer.value = 0;
  try {
    if (_isCapturing) {
      // Already capturing, return immediately to avoid starting a new capture.
      return;
    }

    List<CameraDescription> cameras = await availableCameras();

    if (cameras.isNotEmpty) {
      _cameraController = CameraController(
        cameras[1],
        resolution,
        enableAudio: true,
      );

      await _cameraController!.initialize();
      _cameraControllerNotifier.value = _cameraController;
      print("$_cameraController hello camera");
      if (!_isCapturing) {
        _isCapturing = true;

        int count = counter;
        _timer = Timer.periodic((const Duration(seconds: 1)), (seconds) {
          _counterTimer.value = count;
          showTimer();
          count--;

          if (count < 1) {
            captureImage(imageQuality);
            _timer?.cancel();
          }
        });
      }
    }
  } catch (e) {
    if (kDebugMode) {
      print("Error initializing camera: $e");
    }
  }
}