Skip to content

Instantly share code, notes, and snippets.

@daverix
Last active September 12, 2016 16:46
Show Gist options
  • Save daverix/6f539826e558ad3c135ba647ac20ef37 to your computer and use it in GitHub Desktop.
Save daverix/6f539826e558ad3c135ba647ac20ef37 to your computer and use it in GitHub Desktop.
calculate color from pixel buffer in RGBX_8888 mode
final int limit = buffer.limit();
final int pixels = limit / 4;
long red = 0;
long green = 0;
long blue = 0;
for(int i=0;i<limit;i+=4) {
int color = buffer.getInt(i);
red += 0xFF & color;
green += 0x00FF & (color >> 8);
blue += 0x0000FF & (color >> 16);
}
int avgRed = (int) (red / pixels);
int avgGreen = (int)(green / pixels);
int avgBlue = (int) (blue / pixels);
Log.d("BLS", String.format(Locale.ENGLISH, "0x%s",
Integer.toHexString(avgRed << 16 | avgGreen << 8 | avgBlue)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment