如何获得正确的像素颜色

时间:2016-03-14 10:51:20

标签: android css

我还想从' Image'中读出像素的颜色。我在这里已经阅读了很多关于此的主题,但成功仍然缺失。 所以经过很多不必要的值之后,我试图简化我的代码和图像。我尝试了以下内容:



//..
Android.Graphics.BitmapFactory.Options op = new BitmapFactory.Options();
op.InPreferredConfig = Bitmap.Config.Argb8888;
Bitmap b = Android.Graphics.BitmapFactory.DecodeResource(this.Resources, Resource.Drawable.Image, op);
//imageView.SetImageBitmap(b);
          
  int pixel1 = b.GetPixel(3, 4);
  int pixel2 = b.GetPixel(12, 11);
  int pixel3 = b.GetPixel(19, 20);
  int pixel4 = b.GetPixel(27, 28);
  int pixel5 = b.GetPixel(27, 19);
  int pixel6 = b.GetPixel(20, 11);
            
  int redV1 = Android.Graphics.Color.GetRedComponent(pixel1);
  int greenV1 = Android.Graphics.Color.GetGreenComponent(pixel1);
  int blueV1 = Android.Graphics.Color.GetBlueComponent(pixel1);

  int redV2 = Android.Graphics.Color.GetRedComponent(pixel2);
  int greenV2 = Android.Graphics.Color.GetGreenComponent(pixel2);
  int blueV2 = Android.Graphics.Color.GetBlueComponent(pixel2);
//..



 令人惊讶的是我得到的结果如下: pixel1颜色没问题。 pixel2颜色不是它的pixel1颜色。 pixel3颜色不是它的pixel2颜色。 pixel4颜色不是它的pixel2颜色。 pixel5颜色不是它的pixel2颜色。 pixel6颜色不是我的图像中现有的颜色,但在不同的坐标上。 (我的代码中没有提到)

在imageView中呈现.png图像似乎很流畅。 任何人都可以帮助我的错吗? THX!

pixel1..6值似乎是正确的(不同颜色不同,相同),它们对我来说已经足够了,但是在我开始使用真正的.png文件后,所有值都变为-1(1500x1500)图像作为资源)。这个太大了吗?这种情况下的最大值是多少? THX!

1 个答案:

答案 0 :(得分:0)

......好吧,也许我弄错了。确切地说,不是错误,而是可用的解决方案!现在我尝试使用更小的.png图像尺寸500x500。它足够小,可以使用。 但是:似乎BitmapFactory创建了一个更大的图像(1000 x 1000)。因此,如果我想检查原始图像上的正确颜色,我必须将每个坐标除以2。还有另一个问题:Getpixel()读取的某些颜色可能不同。它可以是R,G,B值的微小差异。例如:RGB(148,0,198)而不是(153,0,204)。这意味着一个''额外的工作让我识别所有新的Android颜色'在我的位图:(我希望,我可以帮助其他人这个主题。找到一个接一个的错误/问题并不容易。

相关问题