detectHumanFaces method
Detects human faces in the given image.
This method communicates with the native Android platform to detect human faces
in the specified image file. It returns the result as a String
if successful,
or null
if an error occurs.
Parameters:
imagePath
: The file path of the image to be processed for face detection.
Returns:
- A
String
containing the detection result if successful. null
if an error occurs.
Note: Debug logs will be printed if kDebugMode
is enabled.
Implementation
@override
Future<String?> detectHumanFaces(String imagePath) async {
if (imagePath.isEmpty) {
if (kDebugMode) {
print('Error: The provided image path is empty.');
}
return null; // Invalid input
}
try {
// Invoke the native method with the given image path
final String? result = await methodChannel.invokeMethod(
'detectHumanFaces',
{'imagePath': imagePath},
);
// Log the result in debug mode
if (kDebugMode) {
print('Face detection completed successfully. Result: $result');
}
return result; // Return the result from the native layer
} on PlatformException catch (e) {
// Handle platform-specific exceptions and log error details
if (kDebugMode) {
print('Error detecting human faces: ${e.code} - ${e.message}');
if (e.details != null) {
print('Details: ${e.details}');
}
}
return null; // Return null in case of an error
} catch (e) {
// Handle any unexpected exceptions
if (kDebugMode) {
print('Unexpected error during face detection: ${e.toString()}');
}
return null; // Return null for unexpected errors
}
}