gpt4 book ai didi

android - 如何在 Android 中获取像素颜色

转载 作者:IT王子 更新时间:2023-10-28 23:45:34 24 4
gpt4 key购买 nike

我正在使用 Intent 调用并显示 Gallery 中的图像,现在我可以使用以下方法获取 TextView 中图像的坐标:

final TextView textView = (TextView)findViewById(R.id.textView); 
final TextView textViewCol = (TextView)findViewById(R.id.textViewColor);
targetImage.setOnTouchListener(new ImageView.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int x=0;
int y=0;
textView.setText("Touch coordinates : " +
String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
if(pixel == Color.RED){
textViewCol.setText("It is RED");
}

/*if(redValue == 255){
if(blueValue == 0)
if(greenValue==0)
textViewCol.setText("It is Red");
}*/
return true; }
});

现在我需要做的是;获取用户选择的确切坐标的 颜色(RGB 值),然后将每个坐标分配给 #FF0000#00FF00 >#0000FF 但是现在,请根据我所拥有的帮助获取像素颜色。

干杯。

最佳答案

您可以像这样从 View 中获取像素:

ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);

现在您可以通过以下方式获取每个 channel :

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

颜色函数返回每个 channel 中的值。因此,您所要做的就是检查红色是否为 255,绿色和蓝色是否为 0,然后将 textView 文本设置为“它是红色的”。请注意,说某事是红色的不仅仅是红色 channel 大于零。 'Cos 255-Green 和 255-Red 当然是黄色的。您也可以将像素与不同的颜色进行比较。例如:

if(pixel == Color.MAGENTA){
textView.setText("It is Magenta");
}

希望对你有帮助。

关于android - 如何在 Android 中获取像素颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7807360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com