How to calculate brightness from list of unsigned 8 bytes integer which represents an image in dart?

1

I wanted to calculate the brightness of an UintList image. The image I used are picked from my phone (Using image_picker plugin in flutter). I tried a for loop on every value of this list and did this:

int r = 0, b = 0, g = 0, count = 0;
for (int value in imageBytesList) {
  /// The red channel of this color in an 8 bit value.
  int red = (0x00ff0000 & value) >> 16;

  /// The blue channel of this color in an 8 bit value.
  int blue = (0x0000ff00 & value) >> 8;

  /// The green channel of this color in an 8 bit value.
  int green = (0x000000ff & value) >> 0;
  r += red;
  b += blue;
  g += green;
  count++;
}
double result = (r + b + g) / (count * 3);

I know that the result should represent a brightness level between 0 and 255, where 0 = totally black and 255 = totally bright. but, what I get are really weird values like 0.0016887266175341332. What calculation mistakes am I making? (I know my method is gravely wrong but I wasn't able to find a way).

The flutter image widget does convert this Uint8List from memory to an Image with correct height & width using Image.memory() constructor. What is the logic behind it?

flutter
dart
rasterizing
asked on Stack Overflow Jan 22, 2020 by Syed Mushaheed

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0