【问题标题】:Android: Problems getPixels from BitmapAndroid:从位图获取像素的问题
【发布时间】:2019-10-31 15:53:05
【问题描述】:

我有以下代码...

BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.test, options);
int[] pixels = new int[1];
if(b != null){
  pixels = new int[b.getHeight()*b.getWidth()];
  b.getPixels(pixels, 0, b.getWidth(), 0, 0, b.getWidth(), b.getHeight());
  for(int i = 0 ; i < 100 ; i++){
    Log.d("test","Pixel is :"+pixels[i]);
  }
}

R.drawable.test 是 res/drawable-nodpi 目录中的 .bmp 文件

但我希望它看起来更像是这样,而不是 0-255 值...

06-03 21:49:24.364: D/test(4088): Pixel is :-16777088
06-03 21:49:24.364: D/test(4088): Pixel is :-16777088
06-03 21:49:24.364: D/test(4088): Pixel is :-16777088
06-03 21:49:24.364: D/test(4088): Pixel is :-16777088
06-03 21:49:24.364: D/test(4088): Pixel is :-16777088
06-03 21:49:24.364: D/test(4088): Pixel is :-16777088
06-03 21:49:24.374: D/test(4088): Pixel is :-16777088

这个数字看起来根本不对,有人可以帮我解决我所缺少的吗?

【问题讨论】:

    标签: android


    【解决方案1】:

    您需要将像素转换为 RGB

    这是一个代码示例

                 int[] pix = new int[picw * pich];
                 bitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);
    
                 int R, G, B, Y;
    
                 for (int y = 0; y < pich; y++) {
    
                     for (int x = 0; x < picw; x++)
                         {
                         int index = y * picw + x;
                         int R = (pix[index] >> 16) & 0xff;     //bitwise shifting
                         int G = (pix[index] >> 8) & 0xff;
                         int B = pix[index] & 0xff;
    
                         //R, G, B - Red, Green, Blue
                         //to restore the values after RGB modification, use 
                         //next statement
    
                         pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
    
                        }
                }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多