45

I want to be able to take an image and find out what is its average color, meaning if the image is half black and half white, I would get something in-between... some shade of gray. It could be the most frequent single color or median. Any average will do.

How can I do this in Android?

Mahozad
  • 18,032
  • 13
  • 118
  • 133
124697
  • 22,097
  • 68
  • 188
  • 315
  • 7
    Sounds pretty straightforward... what have you tried? –  Sep 13 '12 at 14:22
  • 1
    loop on all the colors - and store the average... not too bad – Randy Sep 13 '12 at 14:22
  • Unless you're into taking the NDK or RenderScript route, out of which latter made this task rather easy and fast, ``Bitmap.getPixel`` and ``Bitmap.getPixels`` are your friends. – harism Sep 13 '12 at 14:22
  • @bdares I don't know where to start. do i need some image processing library or can I do it in android platform straight away? – 124697 Sep 13 '12 at 14:25
  • @randy : only problem with it is to use a smart average method, as on big image sum(pixels value) may exceed MAX_INT – njzk2 Sep 13 '12 at 14:44
  • Read this: http://stackoverflow.com/questions/5823854/how-can-i-generate-a-palette-of-prominent-colors-from-an-image – Rafael Sanches Sep 29 '13 at 11:49

8 Answers8

54
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);
Dan O
  • 6,022
  • 2
  • 32
  • 50
  • 6
    Getting each pixel individually with `getPixel` inside your loop is wasteful and slow. You should get all of the pixels with a single call to `getPixels` before the for loops. – slayton Sep 13 '12 at 14:54
  • 2
    Slow? probably, yes. Wasteful? Maybe, depends on the size of the bitmap and how much memory is available (I believe `getPixels()` creates a copy of the bitmap data). – Dan O Sep 13 '12 at 14:59
  • 3
    `Color c = bitmap.getPixel()` should be `int c = bitmap.getPixel()` because `getPixel()` returns an int, not a Color – Stardust Jun 21 '17 at 17:55
  • @Stardust is correct. This function will not return a Color object, but an int. Otherwise this works pretty well. To improve the speed on larger images try scaling the bitmap down. CIF (352x240) works pretty well for me. – Zack Matthews Jul 05 '17 at 14:08
  • Color.rgb(redBucket / pixelCount, greenBucket / pixelCount, blueBucket / pixelCount); call requires api 26 – Sujith S Manjavana Nov 21 '19 at 18:32
  • Why not using ScanLine for fast result? – Mahan Aug 04 '23 at 13:52
  • pixelCount = bitmap.getHeight() * bitmap.getWidth() - there is no reason to increment variable pixelCount every time – Rob Aug 07 '23 at 09:06
15

I think you will have to do that yourself.

Just create an int array with all the colors:

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);  
bmp = bmp.copy(Bitmap.Config.ARGB_8888, true);    
int intArray[] = new int[bmp.getWidth()*bmp.getHeight()];  
bmp.getPixels(intArray, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight());

Then you can get the color with intArray[0], the value could be 0xFFFF0000 for red (last 6 numbers are the RGB color value).

Here is another easy solution:

  • Get you full-size image in a bitmap.

  • Create a scaled bitmap of 1*1px.

  • Get this bitmap color.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Stephane Mathis
  • 6,542
  • 6
  • 43
  • 69
12

Building off Dan O's solution, here's a method that automatically takes into account the alpha channel and makes the optimization/ tradeoff of getPixels vs getPixel.

The cost is memory but the benefit is performance, invocation of a virtual method in a loop that could possibly be run several million times [i.e. an 8MP image has 3,456x2,304 = 7,962,624 pixels]). I've even taken things one step further by removing the looped android.graphics.Color method calls.

public static int getDominantColor(Bitmap bitmap) {
   if (null == bitmap) return Color.TRANSPARENT;

   int redBucket = 0;
   int greenBucket = 0;
   int blueBucket = 0;
   int alphaBucket = 0;

   boolean hasAlpha = bitmap.hasAlpha();
   int pixelCount = bitmap.getWidth() * bitmap.getHeight();
   int[] pixels = new int[pixelCount];
   bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

   for (int y = 0, h = bitmap.getHeight(); y < h; y++)
   {
       for (int x = 0, w = bitmap.getWidth(); x < w; x++)
       {
           int color = pixels[x + y * w]; // x + y * width
           redBucket += (color >> 16) & 0xFF; // Color.red
           greenBucket += (color >> 8) & 0xFF; // Color.greed
           blueBucket += (color & 0xFF); // Color.blue
           if (hasAlpha) alphaBucket += (color >>> 24); // Color.alpha
       }
   }

   return Color.argb(
           (hasAlpha) ? (alphaBucket / pixelCount) : 255,
           redBucket / pixelCount,
           greenBucket / pixelCount,
           blueBucket / pixelCount);
}
Tom
  • 6,947
  • 7
  • 46
  • 76
  • 2
    funny that you calculate the number of pixels in an 8 _megapixel_ image wich has as the name states 8 million pixels. But thanks for the solution. – Gumbo Aug 15 '14 at 18:07
  • 4
    8 megapixel is not precise, 7,962,624 pixels is precise. – mvmn Oct 05 '16 at 11:28
11

You can use Palete class from AndroidX, or from the the v7-support library.

It provides additional methods to extract colours from a Bitmap, such as getting:

  • Most Dominant color
  • Vibrant colors
  • Muted color
  • much more
Entreco
  • 12,738
  • 8
  • 75
  • 95
aleb
  • 2,490
  • 1
  • 28
  • 46
  • Hmmm, very recent yes ;) Just a friendly reminder this class is also available in androidx. https://developer.android.com/reference/androidx/palette/graphics/Palette – Entreco Jan 07 '19 at 20:38
2

Use the Bitmap.getPixels() method to get the color values. Then to calculate the average you have to decide what you mean by that. In a grayscale image it is simple, but with colors there are no such thing as an average. You can separate into components (for example RGBA), and take the average of each component. An alternative is to search for the most commonly used color, and there are several other options I'm sure. Play with it :)

Tobias Ritzau
  • 3,327
  • 2
  • 18
  • 29
2

There is also a library that can do this for you.

http://www.michaelevans.org/blog/2013/12/12/colorart-a-library-to-do-itunes-11-style-color-matching-for-android/

NathofGod
  • 479
  • 5
  • 12
2

Expanding on the second idea from Stephane Mathis' answer, you can resize the image to 1x1 and get the color of that image:

Bitmap originalBitmap = /*your original image*/;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(originalBitmap, 1, 1, false);    //Resize the bitmap to 1x1
@ColorInt int averageColor = scaledBitmap.getPixel(0, 0);    //Get the color of the only pixel of the 1x1 bitmap

If your original image is not a Bitmap object, you can convert it to a Bitmap using one of the methods described here.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
0

Here is a way to get the dominant (NOT average) colors of an image using Material color utilities library which is currently available for Java/Kotlin, C++, Dart, TypeScript.

The colors may not necessarily be the most recurring colors; the library extracts dominant colors appropriate for Material 3 design system and appropriate to be used on light or dark themes in apps.

The library is primarily used on apps for Android 12 and above and also on the Material Design website itself but I tested it for myself and got good results.

To use the library, copy-paste the code on the Material color utilities repository for your desired language to your project and then you can extract dominant colors and color schemes.

Here is an example for Java and Kotlin:

Java:

var MAX_DESIRED_COLOR_COUNT = 128;
var file = new File("my-image.jpg");
var image = ImageIO.read(file);
var pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
var colorFrequency = QuantizerCelebi.quantize(pixels, MAX_DESIRED_COLOR_COUNT);
var decentColors = Score.score(colorFrequency);
var desiredColor = decentColors.get(0);
// You can take the desiredColor or any other decentColors and forget the rest of code below


// Could also use Scheme.dark(desiredColor); to get colors suitable for dark themes
var colorScheme = Scheme.light(desiredColor);
System.out.println("Decent colors: " + decentColors);
System.out.println("Primary color (light theme): " + colorScheme.getPrimary());

Kotlin:

val MAX_DESIRED_COLOR_COUNT = 128
val file = File("my-image.jpg")
val image = ImageIO.read(file)
val pixels = image.getRGB(0, 0, image.width, image.height, null, 0, image.width)
val colorFrequency = QuantizerCelebi.quantize(pixels, MAX_DESIRED_COLOR_COUNT)
val decentColors = Score.score(colorFrequency)
val desiredColor = decentColors.first()
// You can take the desiredColor or any other decentColors and forget the rest of code below


// Could also use Scheme.dark(desiredColor) to get colors suitable for dark themes
val colorScheme = Scheme.light(desiredColor)
println("Decent colors: ${decentColors.joinToString { it.toHexString() }}")
println("Primary color (light theme): ${colorScheme.primary.toHexString()}")

fun Int.toHexString() = "#%06X".format(this and 0xFFFFFF)

Learn more about Material Design color system and color roles here (like colorScheme.primary used in the above code snippets).

Mahozad
  • 18,032
  • 13
  • 118
  • 133