【发布时间】:2021-05-21 03:37:23
【问题描述】:
我正在构建一个用户需要发送自拍照的应用,但有些用户只是挡住了相机拍照,结果全黑,有没有办法检测图像是否为黑色?
我正在考虑使用人脸检测,但我还没有尝试过它,它对我的简单应用程序的影响很大。
【问题讨论】:
我正在构建一个用户需要发送自拍照的应用,但有些用户只是挡住了相机拍照,结果全黑,有没有办法检测图像是否为黑色?
我正在考虑使用人脸检测,但我还没有尝试过它,它对我的简单应用程序的影响很大。
【问题讨论】:
您可以尝试的一种方法是使用palette_generator 包来提取图像中的颜色,然后计算图像的平均亮度,如下所示。
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;
}
【讨论】:
在使用相机之前,请检查您的应用是否有权限。
对于这个海豚,我建议使用permission handler
官方文档中的几行
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.
}
【讨论】: