I'm using Intent to call and show an image from Gallery, and now I made it enable to get me the coordinates of the image in a TextView using these:
final TextView textView = (TextView)findViewById(R.id.textView); final TextView textViewCol = (TextView)findViewById(R.id.textViewColor); targetImage.setOnTouchListener(new ImageView.OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int x=0; int y=0; textView.setText("Touch coordinates : " + String.valueOf(event.getX()) + "x" + String.valueOf(event.getY())); ImageView imageView = ((ImageView)v); Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap(); int pixel = bitmap.getPixel(x,y); int redValue = Color.red(pixel); int blueValue = Color.blue(pixel); int greenValue = Color.green(pixel); if(pixel == Color.RED){ textViewCol.setText("It is RED"); } /*if(redValue == 255){ if(blueValue == 0) if(greenValue==0) textViewCol.setText("It is Red"); }*/ return true; } });
Now what I need to do is; to get the color (RGB value)
of the exact coordinates the user selects and later on assign each to #FF0000
, #00FF00
and #0000FF
but for now, please help to get the Pixel color based on what I have.
Cheers.
With the get() function we can read the color of any pixel in our program window. We can specify which pixel we are interested in by using x and y coordinates as parameters. For example, color mycolor = get(100, 200); would grab the color of pixel 100, 200 and put that color into the mycolor variable.
Use the ColorSync Utility calculator to get the color values of a pixel on your screen. In the ColorSync Utility app on your Mac, click Calculator in the toolbar of the ColorSync Utility window. Click the magnifying glass , then move your pointer over an area on the screen that you want to examine.
A pixel is the smallest block of color in a digital picture. The term is also used for the smallest block of color on your computer monitor.
To set the colors of the pixels in your pixels array, get values from the static methods of Android's Color class and assign them into your array. When you're done, use setPixels to copy the pixels back to the bitmap.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With