2012-11-30 147 views
1

嗨。我想要這種格式的rgb值:在一維矢量中,我需要第一個R值,然後是G值,然後是B值。我試圖使用此代碼:RGB像素陣列只有一個維度不是3向量

pixels = new int[bitmap.getHeight() * bitmap.getWidth()]; 
     bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, 
       bitmap.getWidth(), bitmap.getHeight()); 
     // int R, G, B,Y; 
     for (int y = 0; y < bitmap.getHeight(); y++) { 
      for (int x = 0; x < bitmap.getWidth(); x++) { 
       int index = y * bitmap.getHeight() + x; 
       int R = (pixels[index] >> 16) & 0xff; // bitwise shifting 
       int G = (pixels[index] >> 8) & 0xff; 
       int B = pixels[index] & 0xff; 

       // R,G.B - Red, Green, Blue 
       // to restore the values after RGB modification, use 
       // next statement 
       pixels[index] = 0xff000000 | (R << 16) | (G << 8) | B; 
      } 
     } 

     bitmap.recycle(); 
    } catch (NullPointerException exception) { 
     Log.e("Error Utils", 
       "Photo is damaged or does not support this format!"); 
    } 
    return pixels; 

但是,我仍然只有一個300 * 200的1d數組。 不是300 * 200 * 3 1d數組!

+1

請澄清你的問題,我沒有完全理解你真正想要什麼。 –

+0

'new int [bitmap.getHeight()* bitmap.getWidth()* 3]'? – zapl

回答

0

也許這是你嘗試做

public static int[] getPixel(Bitmap bitmap) { 
    final int width = bitmap.getWidth(); 
    final int height = bitmap.getHeight(); 

    int[] pixelIn = new int[width * height]; 
    bitmap.getPixels(pixelIn, 0, width, 0, 0, width, height); 
    bitmap.recycle(); 

    int[] pixelOut = new int[width * height * 3]; 
    for (int y = 0; y < height; y++) { 
     for (int x = 0; x < width; x++) { 
      int index = y * height + x; 
      int R = (pixelIn[index] >> 16) & 0xff; 
      int G = (pixelIn[index] >> 8) & 0xff; 
      int B = (pixelIn[index] >> 0) & 0xff; 

      int indexOut = index * 3; 
      pixelOut[indexOut++] = R; 
      pixelOut[indexOut++] = G; 
      pixelOut[indexOut ] = B; 
     } 
    } 
    return pixelOut; 
} 

未經測試,但它應該創建一個int[]一個充滿[R][G][B][R][G][B]...


相同的字節

public static byte[] getPixelBytes(Bitmap bitmap) { 
    final int width = bitmap.getWidth(); 
    final int height = bitmap.getHeight(); 
    final int total = width * height; 

    int[] pixelIn = new int[total]; 
    bitmap.getPixels(pixelIn, 0, width, 0, 0, width, height); 
    bitmap.recycle(); 

    byte[] pixelOut = new byte[total * 3]; 
    int indexOut = 0; 
    for (int pixel : pixelIn) { 
     byte R = (byte) ((pixel >> 16) & 0xff); 
     byte G = (byte) ((pixel >> 8) & 0xff); 
     byte B = (byte) ((pixel  ) & 0xff); 
     pixelOut[indexOut++] = R; 
     pixelOut[indexOut++] = G; 
     pixelOut[indexOut++] = B; 
    } 
    return pixelOut; 
} 
(你應該考慮 byte[]

而得到它在三個單獨的陣列等[R R R R][G G G G][B B B B]

public static byte[][] getPixelBytes(Bitmap bitmap) { 
    final int width = bitmap.getWidth(); 
    final int height = bitmap.getHeight(); 
    final int total = width * height; 

    int[] pixelIn = new int[total]; 
    bitmap.getPixels(pixelIn, 0, width, 0, 0, width, height); 
    bitmap.recycle(); 

    byte[][] result = new byte[3][total]; 
    int index = 0; 

    for (int pixel : pixelIn) { 
     byte R = (byte) ((pixel >> 16) & 0xff); 
     byte G = (byte) ((pixel >> 8) & 0xff); 
     byte B = (byte) ((pixel  ) & 0xff); 
     result[0][index] = R; 
     result[1][index] = G; 
     result[2][index] = B; 
     index++; 
    } 
    return result; 
} 

的第五的RGB值(=索引4)像素將是

byte R = result[0][4]; 
byte G = result[1][4]; 
byte B = result[2][4]; 

或該分離成3個陣列

byte[] rArray = result[0]; // each 0 .. (width x height - 1) 
byte[] gArray = result[1]; 
byte[] bArray = result[2]; 

也別忘了Java的byte-128 .. ,而不是 .. 。

+0

好的我想R R R .... 300 * 200 G G G..300 * 200 B B B ... 300 * 200但是我嘗試了 –

+0

@LukeNumerati看到上面的內容, – zapl