[英]Android - Best way to draw RGB bitmap data on canvas?
民間,
在我的Android應用程序中,需要在Canvas視圖中顯示來自外部源的視頻流。 從我的Java代碼中,我通過JNI將ByteBuffer傳遞給基礎C ++庫。 該庫對流進行解碼,並在ByteBuffer中構建RGB位圖圖像,然后將其返回。
現在,在我的Java代碼中,我有一個ByteBuffer,其中包含RGB值的寬度,高度和行。 我現在可以在for循環中繪制它:
for(int y=0;y<height;y++) {
for(int x=0;x<width;x++) {
get the right RGB value from ByteBuffer and draw the pixel
}
}
我想知道是否有更有效的方法來做到這一點? 任何想法,將不勝感激。
ByteBuffer數據的布局在我的控制之下。 也許我可以重新安排它以獲得更好的性能。
預先感謝您的幫助。
問候,
彼得
閱讀此Android API“有效加載大型位圖”官方指南。 您將使用BitmapFactory,這是專門為其創建的類,並且可能像其他sdk類一樣以優化的方式進行操作。 此處的鏈接: http : //developer.android.com/training/displaying-bitmaps/load-bitmap.html
有效的方法是調用copyPixelsFromBuffer
。 不要一一畫像素。 您只需要操作int數組並將它們繪制在一起。 例如:
int[] data8888 = new int[N];
for (int i = 0; i < N; i++) {
data8888[i] = 78255360;
}
mBitmap.copyPixelsFromBuffer(makeBuffer(data8888, N));
private static IntBuffer makeBuffer(int[] src, int n) {
IntBuffer dst = IntBuffer.allocate(n);
for (int i = 0; i < n; i++) {
dst.put(src[i]);
}
dst.rewind();
return dst;
}
我能夠改寫這個問題的代碼,以便在前一天解碼(Byte [3])RGB流。 可能需要針對您的應用進行優化,但這是到目前為止我發現的最簡單的方法。
Byte[] bytesImage = {0,1,2, 0,1,2, 0,1,2, 0,1,2};
int intByteCount = bytesImage.length;
int[] intColors = new int[intByteCount / 3];
int intWidth = 2;
int intHeight = 2;
final int intAlpha = 255;
if ((intByteCount / 3) != (intWidth * intHeight)) {
throw new ArrayStoreException();
}
for (int intIndex = 0; intIndex < intByteCount - 2; intIndex = intIndex + 3) {
intColors[intIndex / 3] = (intAlpha << 24) | (bytesImage[intIndex] << 16) | (bytesImage[intIndex + 1] << 8) | bytesImage[intIndex + 2];
}
Bitmap bmpImage = Bitmap.createBitmap(intColors, intWidth, intHeight, Bitmap.Config.ARGB_8888);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.