0

I use Android NDK to set color for individual pixel. It looks something like that:

typedef struct {
    uint8_t red;
    uint8_t green;
    uint8_t blue;
    uint8_t alpha;
} rgba;

JNIEXPORT void JNICALL Java_com_package_jniBmpTest(JNIEnv* env, jobject obj, jobject bitmapIn, jobject bitmapOut) {
    AndroidBitmapInfo   infoIn;
    void*               pixelsIn;
    AndroidBitmapInfo   infoOut;
    void*               pixelsOut;

    if ((ret = AndroidBitmap_getInfo(env, bitmapIn, &infoIn)) < 0 ||
        (ret = AndroidBitmap_getInfo(env, bitmapOut, &infoOut)) < 0) {
        return;
    }

    if (infoIn.format != ANDROID_BITMAP_FORMAT_RGBA_8888 ||
        infoOut.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
        return;
    }

    if ((ret = AndroidBitmap_lockPixels(env, bitmapIn, &pixelsIn)) < 0 ||
        (ret = AndroidBitmap_lockPixels(env, bitmapOut, &pixelsOut)) < 0) {
        LOGE("Error! %d", ret);
    }

    rgba* input = (rgba*) pixelsIn;
    rgba* output = (rgba*) pixelsOut;
    int w = infoIn.width;
    int h = infoIn.height;

    int n;
    for (n = 0; y < w * h; n++) {
        output[n].red = input[n].red;
        output[n].green = input[n].green;
        output[n].blue = input[n].blue;
        output[n].alpha = 127;
    }

    AndroidBitmap_unlockPixels(env, bitmapIn);
    AndroidBitmap_unlockPixels(env, bitmapOut);
}

I need to set bitmap semi-transparent (it is simplified example - my code is much more complicated, but bug exists in this code, too). Problem is that instead of semi-transparent bitmap as a result I have image with corrupted colors. It is semi-transparent, too, but colors are not correct (for example, white color is black, blue is green...). What's the possible problem?

Thanks for help.

4

1 回答 1

1

解决它。很抱歉在五分钟后提出问题并回答:)

解决方案如下:

float alpha;
alpha = 0.5;
output[n].red = (int) (input[n].red * alpha);
output[n].green = (int) (input[n].green * alpha);
output[n].blue = (int) (input[n].blue * alpha);
output[n].alpha = (int) (255 * alpha);
于 2012-11-28T13:34:45.867 回答