2011-11-27 138 views
1

我有一个应用程序内置在Android 2.2和我使用inPreferredConfig()切换到ARGB_8888格式的位图,但是,这似乎不工作,因为当以后立即检查位图是仍然在RGB_565格式。我试图将其更改为任何其他格式,而且这两种格式都无效。inPreferredConfig()在Android中不工作姜饼

如果手机或模拟器运行的是Android 2.2,但该功能无法正常工作,则该功能可以正常工作。有谁知道为什么会发生这种情况?在以后的Android版本中贬值inPreferredConfig()


我在做什么:

我使用NDK一些C代码我发现运行一些图像处理功能(从http://www.ibm.com/developerworks/opensource/tutorials/os-androidndk/section5.html拍摄)。 C代码希望图像格式在ARGB_8888中,尽管Android文档说默认情况下格式应该已经在8888中,但它肯定在565中,所以我非常困惑。

我猜我可以将它转换成C ...但我在C上很糟糕,所以我不知道从哪里开始。

我的C函数:

{ 
    AndroidBitmapInfo infocolor; 
    void*    pixelscolor; 
    AndroidBitmapInfo infogray; 
    void*    pixelsgray; 
    int    ret; 
    int    y; 
    int    x; 


    LOGI("convertToGray"); 
    if ((ret = AndroidBitmap_getInfo(env, bitmapcolor, &infocolor)) < 0) { 
     LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret); 
     return; 
    } 


    if ((ret = AndroidBitmap_getInfo(env, bitmapgray, &infogray)) < 0) { 
     LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret); 
     return; 
    } 


    LOGI("color image :: width is %d; height is %d; stride is %d; format is %d;flags is %d",infocolor.width,infocolor.height,infocolor.stride,infocolor.format,infocolor.flags); 
    if (infocolor.format != ANDROID_BITMAP_FORMAT_RGBA_8888) { 
     LOGE("Bitmap format is not RGBA_8888 !"); 
     return; 
    } 


    LOGI("gray image :: width is %d; height is %d; stride is %d; format is %d;flags is %d",infogray.width,infogray.height,infogray.stride,infogray.format,infogray.flags); 
    if (infogray.format != ANDROID_BITMAP_FORMAT_A_8) { 
     LOGE("Bitmap format is not A_8 !"); 
     return; 
    } 


    if ((ret = AndroidBitmap_lockPixels(env, bitmapcolor, &pixelscolor)) < 0) { 
     LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret); 
    } 

    if ((ret = AndroidBitmap_lockPixels(env, bitmapgray, &pixelsgray)) < 0) { 
     LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret); 
    } 

    // modify pixels with image processing algorithm 

    for (y=0;y<infocolor.height;y++) { 
     argb * line = (argb *) pixelscolor; 
     uint8_t * grayline = (uint8_t *) pixelsgray; 
     for (x=0;x<infocolor.width;x++) { 
      grayline[x] = 0.3 * line[x].red + 0.59 * line[x].green + 0.11*line[x].blue; 
     } 

     pixelscolor = (char *)pixelscolor + infocolor.stride; 
     pixelsgray = (char *) pixelsgray + infogray.stride; 
    } 

    LOGI("unlocking pixels"); 
    AndroidBitmap_unlockPixels(env, bitmapcolor); 
    AndroidBitmap_unlockPixels(env, bitmapgray); 


} 

我的Java功能:

// load bitmap from resources 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    // Make sure it is 24 bit color as our image processing algorithm expects this format 
    options.inPreferredConfig = Config.ARGB_8888; 
    bitmapOrig = BitmapFactory.decodeResource(this.getResources(), R.drawable.sampleimage,options); 
    if (bitmapOrig != null) 
     ivDisplay.setImageBitmap(bitmapOrig); 

-

bitmapWip = Bitmap.createBitmap(bitmapOrig.getWidth(),bitmapOrig.getHeight(),Config.ALPHA_8); 
    convertToGray(bitmapOrig,bitmapWip); 
    ivDisplay.setImageBitmap(bitmapWip); 

感谢,N

PS我的同一主题的最后一个问题了删除,其中h很烦人,因为我无法在任何地方找到任何答案。

+0

如果你添加一些代码,演示,你说的话,这将使它更容易为我们提供帮助。 – inazaruk

+0

我对使用Bitmap.Config方法更改位图格式的知识并不能保证所有时间。 – confucius

+0

感谢您的评论,我用一些代码片段更新了我的问题。 如果不能保证所有的时间都有其他方式来执行任务吗?也许我可以用C来转换它? –

回答

0

根据文档,默认情况下根据文档将图像加载到ARGB_8888配置,所以我的猜测是它识别您的位图的RGB_565格式并更改配置。我不明白为什么这应该是一个问题,如果原始图像RGB_565格式,并没有透明度。

这里的文档 - 阅读的最后一位:

如果为非空,解码器将尝试解码成这个内部配置。如果它为空,或者请求不能满足,解码器将尝试根据系统的屏幕深度和原始图像的特征(例如它是否具有每个像素的alpha)选择最匹配的配置(需要配置一样)。图像默认加载了ARGB_8888配置。

http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inPreferredConfig

+0

感谢您的回复,我在文档中看到了这一点,但我不相信这是真的。我已经使用Android 2.3.3在Nexus One上进行测试,并且运行相同的模拟器,都将RGB_565作为我的图像格式返回。只有在Android 2.2中运行时才会返回ARGB_8888。 –

+0

就像我说过的,当图像在RGB_565中时,我觉得试图将它们加载为ARGB_8888毫无意义。它会占用更多的记忆,但不会给你任何好处。 –

+0

好吧,看看,所以你说我应该改变我的C代码来处理RGB_565格式呢?如果Android 2.2返回ARGB_8888并且> = 2.3返回RGB_565,那么我将在C中为每个创建的滤镜使用两个单独的方法。这并不理想,特别是因为我对C的知识是基本的。 –

0

这是旧的,但到目前为止还没有满意的回答: 恰好碰到了同样的问题,其他的日子。

到目前为止,我无法解决它,我正在考虑写一个转换器。由于我使用的是OpenCV,并且它们不支持565格式。

保存图像并用不同的配置重新加载它,但不幸的是,对于实时摄像机应用来说,这是不可行的。

看一看这段代码:

How does one convert 16-bit RGB565 to 24-bit RGB888?