你好我想要rgb值采用以下格式:在1d向量中,我首先需要R值,然后是G值,然后是B值。我尝试使用此代码:

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一维数组。
不是300 * 200 * 3一维数组!

最佳答案

也许就是您尝试做的

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[](您应考虑[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;
}




并以三个单独的数组(如[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;
}


第5个(=索引4)像素的rgb值为

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..127,而不是0..255。

09-27 22:51