Reputation: 645
I'm building an app that user required to send a selfie but some user just block the camera to take a picture and the result is all black, is there a way to detect if image is black?
I'm thinking of using face detection but I haven't tried it and its way to much for my simple app.
Upvotes: 3
Views: 1484
Reputation: 5423
One way you can try is using palette_generator package to extract colors in the image and then computing average luminance of the image, like so.
import 'package:palette_generator/palette_generator.dart';
Future<bool> isImageBlack(ImageProvider image) async {
final double threshold = 0.2; //<-- play around with different images and set appropriate threshold
final double imageLuminance = await getAvgLuminance(image);
print(imageLuminance);
return imageLuminance < threshold;
}
Future<double> getAvgLuminance(ImageProvider image) async {
final List<Color> colors = await getImagePalette(image);
double totalLuminance = 0;
colors.forEach((color) => totalLuminance += color.computeLuminance());
return totalLuminance / colors.length;
}
Upvotes: 3
Reputation: 601
Before use camera check if your app has permission for that.
For this porpouse i've recommend to use the permission handler
Few lines from official documentation
var status = await Permission.camera.status;
if (status.denied) {
// We didn't ask for permission yet or the permission has been denied before but not permanently.
}
Upvotes: 0