我想從Android的SurfaceTexture視圖中每隔幾秒發送一次位圖圖像。我用glReadPixels()
讀取像素,我得到的圖像是類似於使用glReadPixels從TextureSurface提取像素導致圖像不好位圖
。
我的代碼看起來是這樣的:
int size = this.width * this.height;
ByteBuffer bb = ByteBuffer.allocateDirect(size * 4);
bb.order(ByteOrder.nativeOrder());
gl.glReadPixels(0, 0, width, height, GL10.GL_RGB, GL10.GL_UNSIGNED_BYTE, bb);
int pixelsBuffer[] = new int[size];
bb.asIntBuffer().get(pixelsBuffer);
bb = null;
for(int i = 0; i < size; i++) {
pixelsBuffer[i] = ((pixelsBuffer[i] & 0xff00ff00)) | ((pixelsBuffer[i] & 0x000000ff) << 16) | ((pixelsBuffer[i] & 0x00ff0000) >> 16);
}
Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
bm.setPixels(pixelsBuffer, size - width, -width, 0, 0, width, height);
if(now - init > 5000) {
init = now;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
}
記:現在和init只是一個長着currentTimeMillis()
功能。
有誰知道有什麼問題嗎?或者是否有更好的方法將圖像轉換爲base64字符串,因爲我需要將其發送到服務器。
我試過使用'GL10.GL_RGBA'但性能不好。我如何使用'GL10.GL_RGB'並使用'Bitmap.Config.RGB_565'位圖格式配置設置像素? –