Android中换入红色和蓝色

Android中换入红色和蓝色

问题描述:

以Sylvain Ratabouil Android NDK(第2版)为例,它从相机获取图像预览并自然处理,从YUV转换为RGB并将颜色过滤器应用于其中。Android中换入红色和蓝色

的代码非常简单,在传递给该功能的过滤器出现问题:

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); 
+0

确保错误不在于您显示这些图像的代码中,否则,这部分代码对我来说看起来很好。 – ZeekHuge

+1

你是否使用过OpenCV(因为它默认使用BGR)? – AKarthik10

+0

我编辑了问题以发布de本机代码。 – PedroNakano

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

PS在一次运行中生成三个平面的速度可能明显更快,而且不清楚整数运算是否比现代CPU上的浮点运算更快。

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

+0

太棒了!我的手机默认使用YV12。 并感谢提高性能的提示。 – PedroNakano