How to Get Pixel Color in Android

How to Get Pixel Color in Android

You can get the pixel from the view like this:

ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);

Now you can get each channel with:

int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);

The Color functions return the value in each channel. So all you have to do is check if Red is 255 and green and blue are 0, than set the textView text to "it is red". Just pay attention that saying that something is red is not simply that the red channel is the greater than zero. 'Cos 255-Green and 255-Red is yellow, of course.
You can also just compare the pixel to different color.
for example:

if(pixel == Color.MAGENTA){
textView.setText("It is Magenta");
}

Hope it helps.

How to get pixel color in Android?

First, you can get the Bitmap with the BitmapFactory like this:

Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);

Now you have the Bitmap. On this Bitmap, you can call the function getPixel(int x, int y) to get the Color of this Pixel.

I guess you then can get the alpha from that Color..

See the following links for further information:

  • Bitmap Example
  • getPixel(..)
  • Android Color

Get pixel color in background on Android

To those who need to do something like I am doing,

I used the system's command /system/bin/screencap -p to capture the screen and check for the pixel on the screenshot.

This method requires the root permission

private void getPixelColor(int xcoord, int ycoord)
{
try {
Process sh = Runtime.getRuntime().exec("su", null,null);

OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + "/sdcard/colorPickerTemp.png").getBytes("ASCII"));
os.flush();

os.close();
sh.waitFor();

Bitmap screen = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + File.separator + "colorPickerTemp.png");
int pixel = screen.getPixel(xcoord ,ycoord + statusHeight);
Log.d("pixel color", "Pixel Color: + " + Integer.toHexString(pixel) + " at x:" + xcoord + " y:" + ycoord);
}
catch (Exception e)
{
e.printStackTrace();
}
}

Get pixel color in Android

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

get pixel color from bitmap android

This should find the first column (from left) containing a black pixel:

    Bitmap bmp = SavePixels(0, 0, screenWidth, screenHeight, gl);
boolean found = false;
for(int x = 0; x < 50 && !found; x++){
for(int y = 0; y < 100 && !found; y++){
int pixel = bmp.getPixel(x, y);
if(pixel == Color.BLACK){
cordinate = x;
found=true;
}
}
}

How to get a pixel color in a video being played?

Not a perfect solution but you can try

  • Use MediaMetadataRetriever to get a frame for any microsecond.

  • then get pixel color from bitmap frame

 MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();

mediaMetadataRetriever.setDataSource(url, new HashMap<String, String>());

Bitmap frame = mediaMetadataRetriever.getFrameAtTime(10000);

I dont know how complex it will be. Never tried.

how to get color at the spot(or pixel) of a image on touch event in android

try this:

final Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
imageView.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event){
int x = (int)event.getX();
int y = (int)event.getY();
int pixel = bitmap.getPixel(x,y);

//then do what you want with the pixel data, e.g
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
return false;
}
});

How to get pixel color in a certain area

For this, you might have to create a logic of your own.
If I am getting this right you might want to do something like this,

Create a method pass the area in any way you want then you by use of the

getPixel()

You can get colors of that area, There is a high possibility that will find multiple shades of one color or multiple colors also.

So based on this you can store the list of colors you are getting and you can compare the most no of colors you get from your DB and write your logic for that.

I have used this idea to get the icon color from the screen and write a text in that color something like NovaLauncher of android if you have seen.



Related Topics



Leave a reply



Submit