位图处理后颜色损坏

时间:2012-11-28 13:17:54

标签: android android-ndk android-image

我使用Android NDK为单个像素设置颜色。看起来像这样:

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);
}

我需要将位图设置为半透明(这是简化示例 - 我的代码要复杂得多,但此代码中也存在错误)。 问题是,由于我的图像颜色已损坏,而不是半透明位图。它也是半透明的,但颜色不正确(例如,白色是黑色,蓝色是绿色......)。什么是可能的问题?

感谢您的帮助。

1 个答案:

答案 0 :(得分: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);