computeAverageLuminance method

double computeAverageLuminance(
  1. CameraImage image
)

Computes the average luminance of a camera frame.

For iOS, it samples the green channel of the BGRA8888 format. For Android, it samples the Y plane of the NV21 format.

Implementation

double computeAverageLuminance(CameraImage image) {
  if (image.planes.isEmpty) return 255.0;

  final bool isIOS = defaultTargetPlatform == TargetPlatform.iOS;
  final Uint8List bytes = image.planes[0].bytes;
  int total = 0;
  int count = 0;

  if (isIOS) {
    for (int i = 1; i < bytes.length; i += 40) {
      total += bytes[i];
      count++;
    }
  } else {
    for (int i = 0; i < bytes.length; i += 10) {
      total += bytes[i];
      count++;
    }
  }

  return count > 0 ? total / count : 255.0;
}