2012-12-25 26 views
0

我嘗試在android renderscript中製作簡單的圖像過濾器,並且它適用於小圖像。不過,我得到out of memory error的照片與照相機拍攝的照片一樣大(儘管對於小圖像一切正常)。我知道我的代碼是有點糟糕(主要來自here複製),所以就如何使大位圖的renderScript任何計算的小費。這裏是調用RS代碼:爲renderscript分配大位圖(android)

RenderScript rs = RenderScript.create(this); 
     Allocation allocIn = Allocation.createFromBitmap(rs, bmp); 
     Allocation allocOut = Allocation.createTyped(rs, allocIn.getType()); 

     ScriptC_sample sc = new ScriptC_sample(rs, getResources(), R.raw.sample); 

     sc.set_in(allocIn); 
     sc.set_out(allocOut); 

     sc.forEach_root(allocIn, allocOut); 

     Bitmap bmpOut = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig()); 
     allocOut.copyTo(bmpOut); 
     imgView.setImageBitmap(bmpOut);  

,這裏是我所相信的是

rs_allocation in; 

void root(const uchar4* v_in, uchar4* v_out, const void* usrData, 
uint32_t x, uint32_t y) { 

    float4 curPixel = rsUnpackColor8888(*v_in); 

    // ... computations 

    *v_out = rsPackColorTo8888(curPixel.r, curPixel.g, curPixel.b, curPixel.a); 
} 

我也明白,我真的不能加載這麼大的位圖到內存(我雖然可以將文件加載到ImageView的?),和我以前用過inJustDecodeBounds處理:從rendescript文件本身有關它..但在這裏我不知道如何以及在哪裏使用它,我不想調整位圖大小(我想處理原始文件並保存相同大小的相同文件)

回答

1

如果您使用Android 2.3.3或更高版本的設備運行,那麼您可以使用BitmapRegionDecoder來只讀取圖像文件的一部分。因此,您可以閱讀區域,處理它並保存結果,然後重複,直到整個圖像處理完畢。

根據您正在進行的圖像處理,您可能需要重疊區域以正確處理其邊緣。

在Android框架中,我不相信有一個等效的BitmapRegionDecoder用於保存部分中的大圖像,因此您將不得不使用外部庫來實現該功能。類似PNGJ允許逐行加載並保存PNG文件(我沒有使用PNGJ,因此無法對庫本身發表評論)。

+0

謝謝,我已經想出了'BitmapRegionDecoder',很高興你確認。 真的沒有辦法保存文件,然後不使用外部庫? – wasyl