0

i have tried below code that is returning me color of specific pixel which i have pass in argument

int x = (int)event.getX();
int y = (int)event.getY();
int pixel = bitmap.getPixel(x,y);


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

int[] color={redValue ,blueValue ,greenValue };
btn.setBackgroundColor(getHexColor(color));


public static int getHexColor(int[] color) {
return android.graphics.Color.rgb(color[0], color[1], color[2]);
}  

i have also try color palette following link is reference link that is also not returning perfect skin tone color of face

this is i have also tried

anyone have idea how to extract or get face skin tone color ??

  • show your color pallete code – Muhammad Saad Dec 08 '17 at 07:14
  • https://medium.com/david-developer/extracting-colors-from-images-integrating-picasso-and-palette-b9ba45c9c418 Try this. use pallete with picasso. if it helps you then let me know – Muhammad Saad Dec 08 '17 at 07:15
  • Palette palette = Palette.generate(bitmap); int default = 0x000000; int vibrant = palette.getVibrantColor(default); int vibrantLight = palette.getLightVibrantColor(default); int vibrantDark = palette.getDarkVibrantColor(default); int muted = palette.getMutedColor(default); int mutedLight = palette.getLightMutedColor(default); int mutedDark = palette.getDarkMutedColor(default); – Tarang Acharya Dec 08 '17 at 07:20
  • Please check this one:- http://www.truiton.com/2015/05/android-palette-pick-colors-images/ – InsaneCat Dec 08 '17 at 07:23

2 Answers2

0

Use this code for getting colors from images. You can follow this tutorial for further information.

https://medium.com/david-developer/extracting-colors-from-images-integrating-picasso-and-palette-b9ba45c9c418

 Palette.from(bitmap)
            .generate(new Palette.PaletteAsyncListener() {
                @Override
                public void onGenerated(Palette palette) {
                    Palette.Swatch textSwatch = palette.getVibrantSwatch();
                    if (textSwatch == null) {
                        Toast.makeText(MainActivity.this, "Null swatch :(", Toast.LENGTH_SHORT).show();
                        return;
                }
                backgroundGroup.setBackgroundColor(textSwatch.getRgb());
                titleColorText.setTextColor(textSwatch.getTitleTextColor());
                bodyColorText.setTextColor(textSwatch.getBodyTextColor());
            }
        });
Muhammad Saad
  • 713
  • 1
  • 9
  • 31
0

Use this

Matrix inverse = new Matrix();
                    v.getMatrix().invert(inverse);
                    float[] touchPoint = new float[] {event.getX(), event.getY()};
                    inverse.mapPoints(touchPoint);
                    int xCoord = (int) touchPoint[0];
                    int yCoord = (int) touchPoint[1];
                    int intColor = ((BitmapDrawable)imageView.getDrawable()).getBitmap().getPixel(xCoord,yCoord);


btn.setBackgroundColor(intColor);

Hope This will help

OR

Simply Change this in you code, don't convert the color pixel to hex. Use the color pixel directly in setBackGroundColor(pixel) like

btn.setBackgroundColor(pixel);

This is the full code i have tried

imageView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction()==(MotionEvent.ACTION_DOWN)){

            Matrix inverse = new Matrix();
            v.getMatrix().invert(inverse);
            float[] touchPoint = new float[] {event.getX(), event.getY()};
            inverse.mapPoints(touchPoint);
            int xCoord = (int) touchPoint[0];
            int yCoord = (int) touchPoint[1];
            int intColor = ((BitmapDrawable)imageView.getDrawable()).getBitmap().getPixel(xCoord ,yCoord );


            try {
                btn.setBackgroundColor(intColor);
            }catch (Exception e){
                e.printStackTrace();
            }

            return false;
        }
        return false;
    }
});

Try catch is not necessary but i am putting it only for precaution for any unplanned error

RajatN
  • 223
  • 1
  • 8