Radwa
Radwa

Reputation: 345

Get pixel color in Android

How to get the color at fixed pixels:

this is my code

public static void getColor(Bitmap btm,Activity act){
    int w = 300;
    int h = 500;
    btm=Bitmap.createScaledBitmap(btm, w,h, true);

    color1= btm.getPixel(30, 50);
    color2= btm.getPixel(50, 70);

}

Upvotes: 0

Views: 1070

Answers (2)

onexf
onexf

Reputation: 3754

Try this. I've used it to get the predominant color of an image.

public static int getDominantColor(Bitmap bitmap) {
    Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 1, 1, true);
    final int color = newBitmap.getPixel(0, 0);
    newBitmap.recycle();
    return color;
}

Upvotes: 3

codetiger
codetiger

Reputation: 2779

The problem is not in the method that you have shared, its the way you are generating the bitmap and passing it to this function that has different sizes on different devices.

Just because you are scaling the device to a definite size does not mean that all pixels will have the same color for different screen sizes. This problem cannot be fixed by reading color from bitmap, so you have to explain the actual purpose, so we can solve it with different techniques.

Upvotes: 0

Related Questions