2014-08-30 88 views
0

得到RGB我从其他地方的代码,但我仍然混淆约使用移位寄存器的java

final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); 
    int[][] result = new int[height][width]; 
    int k = 0; 
    final int pixelLength = 3; 
    for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength)   { 
     int argb = 0; 
     int blue= (((int) pixels[pixel] & 0xff)); 
     argb = argb + blue; 
     int green = (((int) pixels[pixel + 1] & 0xff) << 8); 
     argb = argb + green; // green) 
     int red = (((int) pixels[pixel + 2] & 0xff) << 16); 
     argb = argb + red; 
     result[row][col] = argb; 
     k++; 
     col++; 
     if (col == width) { 
      col = 0; 
      row++; 
     } 

    } 

当我打印的红,绿,蓝为什么不打印的RGB值? 有人可以向我解释吗?像素是什么意思[pixel] & 0xff? thx很多之前:D

回答

0

这是做什么是将3个字节转换为一个int。使用pixels[pixel] & 0xff的原因是它转换成signed values to unsigned values。这是必要的,因为java没有无符号类型,因此通常是200的RGB值实际上是别的。

这些班次的目的是将3个值放在同一个地方,以便这些值彼此相邻。

+0

thx broo [已解决] – 2014-09-02 03:15:17

+0

要将问题标记为已解决,请单击我答案上投票标志下方的复选标记。 – Pokechu22 2014-09-02 03:43:37