Search code examples
javaandroidcolorsbitmappixel

Issue in getPixel() function, wrong color


I have created an image that represent a car, I colored each component of the car with a different color, for example the hood RGB color is 251,252,252.

This image is displayed on the screen through a TileView, I need to implement a function that get the color of the pixel that I touched, the function that I posted work, but return to me a different RGB color compared to the original one.

Here I show the difference:

  • Original image: 251,252,252
  • Bitmap image: 255,255,255

I don't understand why the color change while the creation of the bitmap (used to get the color of the pixel), or maybe the problem is in the getDrawingCache() function maybe change some color value, sincerely I don't know...

This is a part of my code:

tileView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int eventAction = motionEvent.getAction();
                switch (eventAction) {
                    case MotionEvent.ACTION_DOWN:

                        double x = tileView.getCoordinateTranslater().translateAndScaleAbsoluteToRelativeX(tileView.getScrollX() + motionEvent.getX(), tileView.getScale());
                        double y = tileView.getCoordinateTranslater().translateAndScaleAbsoluteToRelativeY(tileView.getScrollY() + motionEvent.getY(), tileView.getScale());

                        try {
                            tileView.setDrawingCacheEnabled(true);
                            Bitmap bitmap = Bitmap.createBitmap(tileView.getDrawingCache());
                            File file = new File(Environment.getExternalStorageDirectory() + "/bitmap.png");
                            bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(file));
                            int touchColor;
                            if (bitmap == null) {
                                touchColor = 0;
                            } else {
                                touchColor = bitmap.getPixel((int) motionEvent.getX(), (int) motionEvent.getY());
                            }

                            int redValue = Color.red(touchColor);
                            int blueValue = Color.blue(touchColor);
                            int greenValue = Color.green(touchColor);
                            Log.wtf("DEBUG", "-Red: " + redValue + " -Green: " + greenValue + " -Blue: " + blueValue);

                            tileView.setDrawingCacheEnabled(false);
                            tileView.destroyDrawingCache();
                            bitmap.recycle();
                        } catch (Exception e) {

                        }
                        addPin(tileView, x, y);
                        break;
                }
                return false;
            }
        });

Solution

  • It might be a problem related to the image rather than the code. I suggest you create an image in png and color it, then set the background to the image.

    In the past I happened that for some strange reasons, the colors did not "fit" in full.