detectNSFWFromBytes method

Future<NsfwResult?> detectNSFWFromBytes(
  1. Uint8List imageData
)

Detects NSFW content from bytes.

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

Implementation

Future<NsfwResult?> detectNSFWFromBytes(Uint8List imageData) async {
  if (_isClosed) throw StateError('NsfwDetector has been closed.');
  if (imageData.isEmpty) {
    throw ArgumentError('imageData must not be empty.');
  }
  try {
    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 image bytes.',
      cause: error,
      stackTrace: stackTrace,
    );
  }
}