captureImage method

Future<void> captureImage(
  1. int imageQuality
)

Implementation

Future<void> captureImage(int imageQuality) async {
  try {
    if (!_cameraController!.value.isInitialized) {
      if (kDebugMode) {
        print("Camera is not initialized.");
      }
      return;
    }

    final XFile? imageFile = await _cameraController?.takePicture();

    if (imageFile != null) {
      final imageBytes = await imageFile.readAsBytes();
      final storageDir = await getExternalStorageDirectory();

      // Save the image with a unique name.
      final imageName = "image_${DateTime.now().millisecondsSinceEpoch}.jpg";
      _latestImagePath = path.join(storageDir!.path, imageName);

      // Update the latest image file with the new image.
      await File(_latestImagePath!).writeAsBytes(imageBytes);

      if (kDebugMode) {
        print("Image captured and saved in $_latestImagePath.");
      }
      // Notify listeners about the updated image path.
      _latestImagePathNotifier.value = _latestImagePath;

      _isCapturing = false; // Reset the flag after capturing is complete.
    }
  } catch (e) {
    if (kDebugMode) {
      print("Error capturing image: $e");
    }
    _isCapturing = false; // Reset the flag in case of an error too.
  }
}