Android Bitmap像素颜色不正确

时间:2017-06-06 03:22:12

标签: android colors bitmap pixel

我一直在尝试使用Android中某些Bitmap数据的输出颜色来诊断问题。有时,生成的Bitmap颜色数据似乎与我提供给eraseColor方法的颜色不同。

我将一些示例代码放入Android Studio中的空活动中以演示此问题:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Random random = new Random((int) System.currentTimeMillis());
    for (int i = 0; i < 10; i++) {
        Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
        int randomR = random.nextInt(256);
        int randomG = random.nextInt(256);
        int randomB = random.nextInt(256);
        int randomAlpha = random.nextInt(256);
        int androidColor = Color.argb(randomAlpha,randomR,randomG,randomB);
        int androidR = Color.red(androidColor);
        int androidG = Color.green(androidColor);
        int androidB = Color.blue(androidColor);
        int androidA = Color.alpha(androidColor);
        bitmap.eraseColor(androidColor);
        int pixelColor = bitmap.getPixel(200, 200);
        int pixelR = Color.red(pixelColor);
        int pixelG = Color.green(pixelColor);
        int pixelB = Color.blue(pixelColor);
        int pixelA = Color.alpha(pixelColor);
        if(pixelR != randomR || pixelG != randomG || pixelB != randomB || pixelA != randomAlpha)
            System.out.println(String.format("[%d] Not matching - r(%d,%d,%d), g(%d,%d,%d), b(%d,%d,%d), a(%d,%d,%d)", i, randomR, androidR, pixelR, randomG, androidG, pixelG, randomB, androidB, pixelB, randomAlpha, androidA, pixelA));
        else
            System.out.println(String.format("[%d] MATCHING - r(%d,%d,%d), g(%d,%d,%d), b(%d,%d,%d), a(%d,%d,%d)", i, randomR, androidR, pixelR, randomG, androidG, pixelG, randomB, androidB, pixelB, randomAlpha, androidA, pixelA));
        bitmap.recycle();
    }
}

这是生成的输出:

  

[0]不匹配 - r(234,234,234),g(153,153,149),b(148,148,149),a(24,24,24)
  [1]不匹配 - r(63,63,63),g(200,200,200),b(222,222,223),a(102,102,102)
  [2]不匹配 - r(15,15,14),g(82,82,83),b(210,210,211),a(92,92,92)
  [3]匹配 - r(21,21,21),g(231,231,231),b(155,155,155),a(227,227,227)
  [4]不匹配 - r(234,234,234),g(44,44,46),b(24,24,25),a(61,61,61)
  [5]不匹配 - r(165,165,167),g(50,50,52),b(156,156,156),a(49,49,49)
  [6]不匹配 - r(205,205,0),g(96,96,0),b(209,209,0),a(0,0,0)
  [7]不匹配 - r(39,39,38),g(238,238,239),b(132,132,130),a(47,47,47)
  [8]匹配 - r(25,25,25),g(103,103,103),b(16,16,16),a(227,227,227)
  [9]不匹配 - r(179,179,179),g(133,133,133),b(71,71,70),a(134,134,134)

我做错了吗?有什么我想念的吗?

谢谢

0 个答案:

没有答案