How can I get the average colour of an image

后端 未结 6 545
温柔的废话
温柔的废话 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: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.
    

提交回复
热议问题