2012-11-30 2 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[] (당신이 byte[]을 고려해야합니다) 바이트

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; 
} 
위해 같은


[R][G][B][R][G][B]...을 작성되는


그리고 5의 [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

관련 문제