Issue
I want to be able to take an image and find out what is the average colour. meaning if the image is half black half white, I would get something in between... some shade of gray. It could be the most frequent single colour or median. Any average will do.
How can I do this in android.
Solution
Bitmap bitmap = someFunctionReturningABitmap();
long redBucket = 0;
long greenBucket = 0;
long blueBucket = 0;
long pixelCount = 0;
for (int y = 0; y < bitmap.getHeight(); y++)
{
for (int x = 0; x < bitmap.getWidth(); x++)
{
Color c = bitmap.getPixel(x, y);
pixelCount++;
redBucket += Color.red(c);
greenBucket += Color.green(c);
blueBucket += Color.blue(c);
// does alpha matter?
}
}
Color averageColor = Color.rgb(redBucket / pixelCount,
greenBucket / pixelCount,
blueBucket / pixelCount);
Answered By - Dan O
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.