How do I get the main color of an ImageView?

0

I wanted to know without having to post my code, how to get the main color from a certain point within a ImageView , for example I have a frame and inside that frame has 3 colors, the color on the right is red, the color on the left is blue, and the color on the middle is black, but I just want to get the color from the middle without having to return that the color on the left or on the right is blue / red. How do I do this? If anyone could help me I would be very grateful.

    
asked by anonymous 23.11.2014 / 21:04

1 answer

2

To get the color of a given pixel from an ImageView getPixel Bitmap associated with ImageView .

Code to get center pixel color:

Bitmap bitmap=((BitmapDrawable)imageView.getDrawable()).getBitmap();
//Coordenada x do centro
int x = bitmap.getWidth()/2;
//Coordenada y do centro
int y = bitmap.getHeight()()/2;

int cor = bitmap.getPixel(x,y);

Please note that the (x, y) coordinates must be relative to Bitmap and not ImageView >. To get the color of any pixel, if the (x, y) coordinates refer to ImageView , they will have to be converted:

double bitmapWidth = bitmap.getWidth();
double bitmapHeight = bitmap.getHeight();
double imageViewWidth = imageView.getWidth();
double imageViewHeight = imageView.getHeigth();

int bitmapX = (int)(x * (bitmapWidth / imageViewWidht));
int bitmapY = (int)(y * (bitmapHeight / imageViewHeight));
int cor = bitmap.getPixel(bitmapX,bitmapY);
    
24.11.2014 / 16:10