How can I get the average colour of an image

后端 未结 6 535
温柔的废话
温柔的废话 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: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);
    

提交回复
热议问题