2

你好。我想要这种格式的 rgb 值:在一维向量中,我想要第一个 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 一维数组!

4

1 回答 1

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[])填充[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];

也不要忘记 Javabyte-128 .. 127,而不是0 .. 255

于 2012-11-30T18:05:47.933 回答