2011-11-27 60 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?