-1

I am trying to get a color pixel from a image and then compare it with some other colors using switch case and display using toast but the problem is I am not able to match it using switch case. can any one help me. here is my code

        public boolean onTouch(View v, MotionEvent event) { 
            int x = (int)event.getX();
            int y = (int)event.getY();
            int pixel = bitmap.getPixel(x,y);
            switch (pixel) {
            case Color.RED:
                Toast.makeText(getApplicationContext(), "color", Toast.LENGTH_LONG).show();
                break;

            default:
                Toast.makeText(getApplicationContext(), "default", Toast.LENGTH_LONG).show();
                break;
           }


            return false;
        }
Ke Di
  • 345
  • 1
  • 12
  • > **Returns** The argb Color at the specified coordinate It is not working because you probably do not get excatly values. Try putting a red square image – maatik5 Jul 24 '15 at 07:03

1 Answers1

0

I think the reason why the case never match is because the pixel you get is not exactly the Color.RED. Can you print some log to check what's the pixel really is.

The really problem should be how to get the colour name for RGB value, and here is the answer you are looking for.

Below I'll show you a more straight forward but not that serious function:

public boolean onTouch(View v, MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        int pixel = bitmap.getPixel(x, y);

        int distanceRed = Math.abs(Color.red(pixel) - 255) + Math.abs(Color.blue(pixel) - 0) + Math.abs(Color.green(pixel) - 0);

        if (distanceRed < RED_THESHOLD) {
            Toast.makeText(getActivity(), "RED", Toast.LENGTH_LONG).show();
        }

        return false;//
    }
Community
  • 1
  • 1
Ke Di
  • 345
  • 1
  • 12
  • pixel:-5464781color.red: -65536 pixel.RED: 172 pixel.BLUE: 51 pixel.GREEN: 157 this is wht i get in log – Jatin Malhotra Jul 24 '15 at 07:39
  • Where is this pixel from? It's from a image or your draw it on the screen? – Ke Di Jul 24 '15 at 07:43
  • from an Image i am using in imageview – Jatin Malhotra Jul 24 '15 at 07:44
  • I think the image may looks like red but mathematically the value of it's not exactly red. So can you use something like if (Color.red(pixel) > 150) {Toast.makeText(getActivity(), "RED", Toast.LENGTH_LONG).show();} – Ke Di Jul 24 '15 at 07:47
  • actually i want the user to click on colors and then a toast is shown of tht color and hence i was using switch statement for that. If i use If statement it works fine but doesnt work with switch – Jatin Malhotra Jul 24 '15 at 07:53
  • whts the theshold? it cant resolve it to a variable the main problem is tht i want to compare "pixel" with the case (value). – Jatin Malhotra Jul 24 '15 at 08:12
  • By showing the code I just mean to give you an idea. For exactly threshold value you need to test. This solution only suitable for applications not that serious about the result. If you really need the result very accurate, then better you follow this solution http://stackoverflow.com/a/20670056/2873453 – Ke Di Jul 24 '15 at 08:16