【问题标题】:How to detect if image is all black using flutter?如何使用颤振检测图像是否全黑?
【发布时间】:2021-05-21 03:37:23
【问题描述】:

我正在构建一个用户需要发送自拍照的应用,但有些用户只是挡住了相机拍照,结果全黑,有没有办法检测图像是否为黑色?

我正在考虑使用人脸检测,但我还没有尝试过它,它对我的​​简单应用程序的影响很大。

【问题讨论】:

    标签: image flutter dart


    【解决方案1】:

    您可以尝试的一种方法是使用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;
      }
    

    【讨论】:

      【解决方案2】:

      在使用相机之前,请检查您的应用是否有权限。

      对于这个海豚,我建议使用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.
          }
      

      【讨论】:

      • 谢谢您的回答,但我目前正在使用相机的权限,我想要识别所拍摄图像的颜色
      猜你喜欢
      • 1970-01-01
      • 2020-05-04
      • 2021-08-17
      • 1970-01-01
      • 2021-03-06
      • 2017-05-15
      • 2021-07-27
      • 2021-09-03
      • 2021-10-23
      相关资源
      最近更新 更多