【问题标题】:get face skin tone color from image从图像中获取面部肤色
【发布时间】:2017-12-08 18:46:30
【问题描述】:

我尝试了下面的代码,它返回了我传入参数的特定像素的颜色

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

我也尝试过调色板下面的链接是参考链接,也没有返回完美的面部肤色

this is i have also tried

有人知道如何提取或获取面部肤色吗??

【问题讨论】:

  • 显示您的调色板代码
  • medium.com/david-developer/… 试试这个。与毕加索一起使用托盘。如果对你有帮助,请告诉我
  • 调色板调色板 = Palette.generate(bitmap); int 默认值 = 0x000000; int充满活力=调色板.getVibrantColor(默认); int VibrantLight = 调色板.getLightVibrantColor(默认); int VibrantDark = 调色板.getDarkVibrantColor(默认); int muted = palette.getMutedColor(default); int mutedLight = palette.getLightMutedColor(默认); int mutedDark = palette.getDarkMutedColor(default);

标签: android colors


【解决方案1】:

使用此代码从图像中获取颜色。 您可以按照本教程了解更多信息。

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

【讨论】:

  • 图片没有加载到毕加索不知道是什么问题
【解决方案2】:

使用这个

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

希望这会有所帮助

只需在您的代码中进行更改,不要将颜色像素转换为十六进制。 直接在 setBackGroundColor(pixel) 中使用颜色像素 喜欢

btn.setBackgroundColor(pixel);

这是我尝试过的完整代码

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

尝试 catch 不是必需的,但我只是为了预防任何意外错误

【讨论】:

  • 你好@RajatN v obj 是什么??
  • 它的View对象,在onTouch函数onTouch(View v, MotionEvent event)
  • 是的,如果我直接传递像素,那么将传递透明颜色,这也不起作用@RajatN
  • 在我的情况下它工作正常我不知道为什么它在你的情况下不起作用我将完整的代码放在答案中检查你是否错误地犯了任何错误
猜你喜欢
  • 2013-04-20
  • 2011-04-11
  • 2012-05-18
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
  • 2019-07-27
  • 2012-08-16
  • 2013-06-04
相关资源
最近更新 更多