2

I am a newbie using Android and have been really working on this problem for four days now. I would really appreciate someone's help.

I have an image in an ImageView, and I would like to get the color of the part of the image that the user touches. For that I am using the Bitmap.getPixel() function. The problem is, that the returned value of this function,is always negative, which as the documentation says, is wrong. I am not getting the right color values, and I have really tried it in several ways (RGB, HSV...). Could somebody explain me please, why is my Bitmap.getPixel() function returning negative values all the time? Thanks in advance.

Here is my .java code:

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
        imageView.setOnTouchListener(new ImageView.OnTouchListener(){
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN) {
                    Drawable imgDrawable = ((ImageView)imageView).getDrawable();
                    Bitmap mutableBitmap = Bitmap.createBitmap(imageView.getWidth(), imageView.getHeight(), Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(mutableBitmap);
                    imgDrawable.draw(canvas);
                    int pixel = mutableBitmap.getPixel((int)event.getX(), (int)event.getY());
                    Log.i("PIXEL COLOR", ""+pixel);

                    int alpha = Color.alpha(pixel);
                    int red = Color.red(pixel);
                    int blue = Color.blue(pixel);
                    int green = Color.green(pixel);
                    String color = String.format("#%02X%02X%02X%02X", alpha, red, green, blue);
                    Log.i("RGB", color);

                    float[] hsv = new float[3];
                    Color.RGBToHSV(red, green, blue, hsv);
                    Log.i("HSV_H", "Hue=" + hsv[0]);
                    Log.i("HSV_H", "Saturation=" + hsv[1]);
                    Log.i("HSV_H", "Value=" + hsv[2]);
               }
               return true;
            }
        });
}

}

Here is my .xml code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"
        android:src="@drawable/lapices" />

</LinearLayout>
patriciasc
  • 133
  • 1
  • 7

3 Answers3

2

That's probably because it's not a BitmapDrawable.

check here https://stackoverflow.com/a/16037415/906362 on my answer the proper way of getting a Drawable into a Bitmap.

edit:

try this:

Drawable imgDrawable = ((ImageView)imageView).getDrawable();
Bitmap mutableBitmap = Bitmap.createBitmap(imageView.getWidth(), imageView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mutableBitmap);
imgDrawable.draw(canvas);
int pixel = mutableBitmap.getPixel((int)event.getX(), (int)event.getY());
Community
  • 1
  • 1
Budius
  • 39,391
  • 16
  • 102
  • 144
  • I have looked at the example. Sorry, but I am confused with it. It is using the Bitmap, and the Drawable class, but not the BitmapDrawable one. Do you mean that I should be using a BitmapDrawable class instead of a Bitmap class?. Sorry for the questions, but this is a bit confusing, and it is hard for me to understand the differences between Bitmap and Drawable too. – patriciasc Apr 16 '13 at 15:25
  • 1
    Bitmap is just a bit ByteArray with the pixel information. Drawable is something that can be used to draw into a Canvas. BitmapDrawable is a extending class of Drawable that happens to be drawing a Bitmap on the Canvas. What that example code do is not care what type of Drawable is used and simply ask it to draw it into a canvas that have a Bitmap. That way all the bytes on that Bitmap will get their colors – Budius Apr 16 '13 at 15:42
  • Thanks for your response Budious, very appreciated. I changed the code using the canvas this time, as I saw in your example, but I still get a negative value. I do not know if I understood this right, but I have the guess that I did not quite. – patriciasc Apr 16 '13 at 17:49
  • I've copied a piece of your code and edited some stuff (mostly simplifying it). If you still have -1, try calling 'imageView.setImageBitmap(mutableBitmap);' just to see if the Bitmap is ok. – Budius Apr 16 '13 at 19:58
  • Thanks once again for the help Budius. With these changes, the Log.i, that I am using for printing the RGB and HSV format, is returning the right color values now, but Bitmap.getPixel() is still returning a negative value, even when I used 'imageView.setImageBitmap(mutableBitmap);'. I do not understand what is going on. I updated the code above too. – patriciasc Apr 17 '13 at 11:27
  • 3
    So it's all correct now as defined by Color (https://developer.android.com/reference/android/graphics/Color.html) a white opaque color is 0xFFFFFFFF and black opaque is 0xFF000000 (the first FF is the transparency, so alpha=255). But the first bit on the left side of an integer as 'true' defines that integer as negative. The important is that the decomposition of that integer gives you the correct colours. 'imageView.setImageBitmap(mutableBitmap); was just to put the created Bitmap onto the screen to see what it looks like (should be a copy of the original) – Budius Apr 17 '13 at 13:27
  • Great Budius. Thanks a lot for your explanation and patience! :))) – patriciasc Apr 17 '13 at 14:17
1

The answer is not true: "That's probably because it's not a bitmap drawable."

As mentioned in the small comments, It is because of the size of the integer that contains the color. It turns out negative or positive.

So: GetPixel is returning the right result.

Ton Snoei
  • 2,637
  • 22
  • 23
1

There might be a case when you set your image to fitXY scaletype you may end up getting wrong values.Remove scaletype and try again.

Yyy
  • 2,285
  • 16
  • 29