hadi
hadi

Reputation: 497

Get Pixel Colour in Android

I use this code to get pixel color but each time I touch screen give me new RGB code I dont know why! I touch same pixel

    private void color1() {

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

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

            textView.setText("R= " + redValue + "B= " + blueValue + "G= " + greenValue);
            Toast.makeText(G.context, "R= " + redValue + "G= " + greenValue + "B= " + blueValue, Toast.LENGTH_LONG).show();
            return true;
        }
    });
}

Upvotes: 1

Views: 588

Answers (1)

Settembrini
Settembrini

Reputation: 1386

The code looks correct to me. I think you cannot touch precisely a single Pixel. In order to check that, you can also display the x and y values in the text. Second, even if two colors look quite similar, their RGB values can differ considerably. (There are other Color spaces that are more intuitive for the human eye in terms of Color "characteristics", e.g. HSV where Hue is the Basic Color, Saturation of that color and V the brightness).

Upvotes: 1

Related Questions