detectNSFWFromFile method

Future<NsfwResult?> detectNSFWFromFile(
  1. File imageFile
)

Detects NSFW content from a file.

HEIC images, which are the default camera format on iOS, are not supported by the image package and will return null.

Implementation

Future<NsfwResult?> detectNSFWFromFile(File imageFile) async {
  if (_isClosed) throw StateError('NsfwDetector has been closed.');
  try {
    final imageData = await imageFile.readAsBytes();
    final image = img.decodeImage(imageData);
    return image == null ? null : await detectNSFWFromImage(image);
  } catch (error, stackTrace) {
    if (error is NsfwDetectorException) {
      rethrow;
    }
    throw NsfwDetectorException(
      'Failed to detect NSFW content from file: ${imageFile.path}',
      cause: error,
      stackTrace: stackTrace,
    );
  }
}