在Android中交换红色和蓝色

时间:2017-08-04 18:00:08

标签: android colors android-ndk

从Sylvain Ratabouil Android NDK(第2版)做一个例子,它从相机获取图像预览并原生处理,从YUV转换为RGB并应用滤色器。

代码非常简单,问题出现在传递给此函数的过滤器中:

public native void decode(Bitmap target, byte[] source, int filter);

目标是对ImageView的引用 来源是框架预览数据
过滤器是彩色滤镜

代码如下:

decode(mImageRed,   data, 0xFFFF0000);
decode(mImageGreen, data, 0xFF00FF00);
decode(mImageBlue,  data, 0xFF0000FF);

位图显示红色和蓝色交换,绿色没问题。

当我像这样交换红色和蓝色滤镜时:

decode(mImageRed,   data, 0xFF0000FF);
decode(mImageGreen, data, 0xFF00FF00);
decode(mImageBlue,  data, 0xFFFF0000);

*更改 0xFF0000FF 过滤器,其中 0xFFFF0000 用于红色图像,反之亦然。

在原生部分中,它所做的只是将过滤器与按位运算符和(&)一起应用:

bitmapContent[yIndex] &= pFilter;

有人知道颜色交换了吗?因为我认为0xFFFF0000是红色而不是0xFF0000FF。

这是解码功能:

void JNICALL
decode(JNIEnv *pEnv, jclass pClass, jobject pTarget, jbyteArray pSource, jint pFilter) {

    // Retrieves bitmap information and locks it for drawing.
    AndroidBitmapInfo bitmapInfo;
    uint32_t *bitmapContent;

    if (AndroidBitmap_getInfo(pEnv, pTarget, &bitmapInfo) < 0)
        abort();

    if (bitmapInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888)
        abort();

    if (AndroidBitmap_lockPixels(pEnv, pTarget, (void **) &bitmapContent) < 0)
        abort();

    // Accesses source array data.
    jbyte *source = (*pEnv)->GetPrimitiveArrayCritical(pEnv, pSource, 0);
    if (source == NULL)
        abort();

    int32_t frameSize = bitmapInfo.width * bitmapInfo.height;
    int32_t yIndex, uvIndex, x, y;
    int32_t colorY, colorU, colorV;
    int32_t colorR, colorG, colorB;
    int32_t y1192;

    // Processes each pixel and converts YUV to RGB color.
    // Algorithm originates from the Ketai open source project.
    // See http://ketai.googlecode.com/.
    for (y = 0, yIndex = 0; y < bitmapInfo.height; y++) {
        colorU = 0;
        colorV = 0;
        // Y is divided by 2 because UVs are subsampled vertically.
        // This means that two consecutives iterations refer to the
        // same UV line (e.g when Y=0 and Y=1).
        uvIndex = frameSize + (y >> 1) * bitmapInfo.width;
        for (x = 0; x < bitmapInfo.width; x++, yIndex++) {
            // Retrieves YUV components. UVs are subsampled
            // horizontally too, hence %2 (1 UV for 2 Y).
            colorY = max(toInt(source[yIndex]) - 16, 0);
            if (!(x % 2)) {
                colorV = toInt(source[uvIndex++]) - 128;
                colorU = toInt(source[uvIndex++]) - 128;
            }
            // Computes R, G and B from Y, U and V.
            y1192 = 1192 * colorY;
            colorR = (y1192 + 1634 * colorV);
            colorG = (y1192 - 833 * colorV - 400 * colorU);
            colorB = (y1192 + 2066 * colorU);

            colorR = clamp(colorR, 0, 262143);
            colorG = clamp(colorG, 0, 262143);
            colorB = clamp(colorB, 0, 262143);

            // Combines R, G, B and A into the final pixel color.
            bitmapContent[yIndex] = color(colorR, colorG, colorB);
            bitmapContent[yIndex] &= pFilter;
        }
    }

    (*pEnv)->ReleasePrimitiveArrayCritical(pEnv, pSource, source, 0);
    if (AndroidBitmap_unlockPixels(pEnv, pTarget) < 0)
        abort();
}

这里是如何分配位图的:

mImageR = Bitmap.createBitmap(size.width, size.height, Bitmap.Config.ARGB_8888);
mImageG = Bitmap.createBitmap(size.width, size.height, Bitmap.Config.ARGB_8888);
mImageB = Bitmap.createBitmap(size.width, size.height, Bitmap.Config.ARGB_8888);
mImageViewR.setImageBitmap(mImageR);
mImageViewG.setImageBitmap(mImageG);
mImageViewB.setImageBitmap(mImageB);

1 个答案:

答案 0 :(得分:0)

您的decode()功能是针对NV21视频格式进行硬编码的,但相机可以设置为YV12。这可以解释颜色'交换'。请注意,对于某些分辨率,YV12可以使用行填充(NV21保证“完全打包”)。

PS 在一次运行中生成三个平面可能要快得多,并且不清楚整数运算是否真的比现代CPU上的浮点快。

如果分辨率很高,处理时间很重要,请考虑使用renderscript。