2011-01-12 40 views
5

使用Android 2.2時,獲取NDK中的位圖數據非常簡單,但使用2.1或更低版本時,AndroidBitmap_lockPixels函數不可用。我一直在尋找過去的幾個小時,但沒有任何工作。Android Pass Bitmap to Native in 2.1 and lower

如何在不使用該函數的情況下訪問位圖的像素數據?

+0

GuyNoir您是否檢查了android-opencv http://code.google.com/p/android-opencv/它們支持1.5及以上版本 – 100rabh 2011-01-14 08:09:34

+0

我真的不想爲這樣一個簡單的函數實現openCV。 – GuyNoir 2011-01-15 23:25:03

回答

1

創建原始圖像的尺寸和ARGB_8888格式空位圖:

int width = src.getWidth(); 
int height = src.getHeight(); 
Bitmap dest = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 

複製像素從源位圖中的int數組:

int[] pixels = new int[width * height]; 
src.getPixels(pixels, 0, width, 0, 0, width, height); 

並設置這些像素到目標位圖:

dest.setPixels(pixels, 0, width, 0, 0, width, height); 
+0

對不起,我忘記提及我正在使用NDK。像素在Java中工作正常。 – GuyNoir 2011-01-14 19:10:36

1

在您的Java代碼中創建一個IntBuffer並將該數組傳遞給t Ø您的本機庫:

// this is called from native code 
buffer = IntBuffer.allocate(width*height); 
return buffer.array(); 

使用GetIntArrayElements得到一個jint *和寫入陣列:

寫入陣列,完成後,釋放:

env->ReleaseIntArrayElements((jintArray)bufferArray, arr, 0); 

通知該數組已更新的Java代碼並使用Canvas.drawBitmap()繪製IntBuffer:

canvas.drawBitmap(buffer.array(), ....); 

提請位圖,與位圖

... new Canvas(bitmap) 
+0

這看起來越來越近了。我會測試一下。謝謝。 – GuyNoir 2011-01-19 02:37:25

1

剛纔有人問同樣的問題初始化畫布 - 我只是鏈接到它,以避免重複我的回答:

Android rendering to live wallpapers

無論如何,您可能不希望在每次需要在Java和JNI代碼之間交換位圖數據時複製位圖數據,因此如果您的代碼對性能非常敏感,這可能是您在Android 2.1及更低版本上的唯一選項。