Radwa Radwa - 1 month ago 34
Android Question

Get pixel color in Android

I have a method to deal with an image to get the color at fixed pixels:

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);
color3= btm.getPixel(80,110);
color4= btm.getPixel(30, 120);
color5= btm.getPixel(100,150);
color6= btm.getPixel(200, 200);
color7= btm.getPixel(230, 300);
}


The problem is that it is giving different colors in different devices according to screen size even I give a fixed size here:

btm=Bitmap.createScaledBitmap(btm, w,h, true);


Is there any way to get the same color at the same pixel regardless of screen size?

Answer

Try this. I've used it to get the predominant color of an image. Let me know if it works

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;
}