How can I get the average colour of an image

后端 未结 6 510
温柔的废话
温柔的废话 2020-12-07 16:57

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

相关标签:
6条回答
  • 2020-12-07 17:09

    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);
    }
    
    0 讨论(0)
  • 2020-12-07 17:14

    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).

    EDIT : Easy solution :

    Get you full-size image in a bitmap.
    
    Create a scaled bitmap of 1*1px.
    
    Get this bitmap color.
    
    0 讨论(0)
  • 2020-12-07 17:19

    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/

    0 讨论(0)
  • 2020-12-07 17:21
    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);
    
    0 讨论(0)
  • 2020-12-07 17:23

    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 :)

    0 讨论(0)
  • 2020-12-07 17:34

    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
    0 讨论(0)
提交回复
热议问题