2011-11-10 75 views
1

你能告訴我這段代碼有什麼問題嗎?Android位圖處理

//bmp is a bitmap of already present image 
int width=bmp.getWidth(); 
int height=bmp.getHeight(); 
int rgbval[]=new int[width*height]; 
bmp.getPixels(rgbval, 0, width, 0, 0, width, height); 
rgbval=actual(rgbval); 
Bitmap bmp2=bmp.copy(Bitmap.Config.ARGB_8888,true); 
bmp2.setPixels(rgbval, 0, width, 0, 0, width, height); 

actual是我已經創建操縱一個bmp的RGB值的函數。通過使用Eclipse的調試功能,我已檢查它是否正常工作,但是當我嘗試恢復bmp2的rgb值時,我沒有得到操縱值。

回答

1

我認爲這個問題是由於一些Android的錯誤。我操作了位圖的像素,並且我需要像素值完全相同,但是當我使用setPixels設置位圖的完整像素時,它不起作用。我找到了解決方案,通過使用

index=0; 
for (int j = 0; j < height; j++) 
    for (int i = 0; i < width; i++) 
    { 
    destBitmap.setPixel(i, j, pixelvalues[index]); 

    } 

所以,如果我一個,而不是所有的像素設置一次一個不知它的作品!

2

首先,看這個答案:Android Bitmap setPixel doens't work correctly? (set value, then read a different value)

所以,看來,如果你的原始圖像沒有alpha通道(即hasAlpha()返回false),它會自動將RGB值轉換爲一種預乘的格式。 這就是你正在經歷的? bmp.hasAlpha()是否爲false?

我剛剛測試了默認Android圖標作爲位圖的代碼,hasAlpha()返回true。輸入和輸出顏色數組中的alpha通道是相同的。然而,紅色通道在2304個像素中有15個被關閉一個或兩個。我沒有檢查藍色或綠色通道。我認爲這是內部舍入錯誤?我發佈了相當冗長且只用於測試的代碼,以防萬一有人需要添加任何內容。

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon); 

int width=bmp.getWidth(); 
int height=bmp.getHeight(); 
int rgbval[]=new int[width*height]; 
bmp.getPixels(rgbval, 0, width, 0, 0, width, height); 
int rgbval2[] = actual(rgbval); 

Bitmap bmp2=bmp.copy(Bitmap.Config.ARGB_8888,true); 
bmp2.setPixels(rgbval2, 0, width, 0, 0, width, height); 

int rgb2[] = new int[width*height]; 
bmp2.getPixels(rgb2, 0, width, 0, 0, width, height); 

int alpha1[] = new int[width*height]; 
int alpha2[] = new int[width*height]; 
int red1[] = new int[width*height]; 
int red2[] = new int[width*height]; 

for(int i=0; i<alpha2.length; ++i) { 
    alpha1[i] = Color.alpha(rgbval2[i]); 
    alpha2[i] = Color.alpha(rgb2[i]); 
    red1[i] = Color.red(rgbval2[i]); 
    red2[i] = Color.red(rgb2[i]); 

    if(red1[i]!=red2[i]) { 
     int a1 = alpha1[i]; 
     int a2 = alpha2[i]; 
     int r1 = red1[i]; 
     int r2 = red2[i]; 


     Log.e("E", String.format("a1: %d, a2: %d, r1: %d, r2: %d", a1, a2, r1, r2)); 
    } 
} 


private int[] actual(int rgb[]) { 
    int rgb2[] = new int[rgb.length]; 

    for(int i=0; i<rgb.length; ++i) { 
     rgb2[i] = Color.argb(Color.alpha(rgb[i]), Color.red(rgb[i])/2, 
         Color.green(rgb[i])/2, Color.blue(rgb[i])/2); 
    } 

    return rgb2; 
} 

這裏是logcat的輸出

a1: 64, a2: 64, r1: 30, r2: 32 
a1: 64, a2: 64, r1: 30, r2: 32 
a1: 142, a2: 142, r1: 58, r2: 57 
a1: 142, a2: 142, r1: 58, r2: 57 
a1: 216, a2: 216, r1: 56, r2: 55 
a1: 216, a2: 216, r1: 56, r2: 55 
a1: 57, a2: 57, r1: 6, r2: 4 
a1: 59, a2: 59, r1: 6, r2: 4 
a1: 231, a2: 231, r1: 90, r2: 91 
a1: 216, a2: 216, r1: 95, r2: 94 
a1: 216, a2: 216, r1: 95, r2: 94 
a1: 217, a2: 217, r1: 57, r2: 58 
a1: 216, a2: 216, r1: 88, r2: 89 
a1: 199, a2: 199, r1: 66, r2: 67 
a1: 199, a2: 199, r1: 39, r2: 38 
+0

感謝你的回覆,我檢查了我的位圖,結果發現hasAlpha()爲false。你可以建議我可以解決它的任何方式,即我的代碼甚至可以在hasAlpha()爲false的位圖上工作 –

+1

我的帖子中的鏈接說,調用bmp.setHasAlpha(true);這個技巧......但是,該功能只能在API級別12之後纔可用。如果hasAlpha()爲false,則可以輕鬆地將讀取的像素的Alpha值設置爲255。 –

+0

我想設計我的項目在API級別7或8工作,所以我不能使用setHasAlpha(),同時將alpha值自動設置爲255似乎也不工作,你有沒有其他的解決方法 –