【发布时间】:2019-03-01 01:04:12
【问题描述】:
我有一个应用程序,我想在其中处理来自相机的每个给定帧以执行一些 ARCore 工作。所以我有一个实现GLSurfaceView.Renderer 的类,在这个类中我有onDrawFrame(GL10 gl) 方法。在此方法中,我想使用 Android 位图,因此我调用此代码从当前帧获取位图:
private Bitmap getTargetImageBitmapOpenGL(int cx, int cy, int w, int h) {
try {
if (currentTargetImageBitmap == null) {
currentTargetImageBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
byteBuffer = ByteBuffer.allocateDirect(w * h * 4);
byteBuffer.order(ByteOrder.nativeOrder());
}
// cy = height - cy;
if ((cx + w / 2) > width) {
Log.e(TAG, "TargetImage CenterPoint invalid A: " + cx + " " + cy);
cx = width - w / 2;
}
if ((cx - w / 2) < 0) {
Log.e(TAG, "TargetImage CenterPoint invalid B: " + cx + " " + cy);
cx = w / 2;
}
if ((cy + h / 2) > height) {
Log.e(TAG, "TargetImage CenterPoint invalid C: " + cx + " " + cy);
cy = height - h / 2;
}
if ((cy - h / 2) < 0) {
Log.e(TAG, "TargetImage CenterPoint invalid D: " + cx + " " + cy);
cy = h / 2;
}
int x = cx - w / 2;
int y = cy - h / 2;
GLES20.glReadPixels(x, y, w, h, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE,
byteBuffer);
IntBuffer currentTargetImagebuffer = byteBuffer.asIntBuffer();
currentTargetImagebuffer.rewind();
currentTargetImageBitmap.copyPixelsFromBuffer(currentTargetImagebuffer);
return currentTargetImageBitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
这个方法大约需要 90 毫秒,这对于实时处理每个传入的帧肯定太慢了,我需要这样做,因为onDrawFrame(GL10 gl) 方法也会将此帧绘制到表面视图。知道为什么这么慢吗?如果我只能读取每隔一帧的像素,但将每一帧都绘制到我的 SurfaceView 上,这也足够了。我尝试调用AsyncTask.execute()中显示的方法,但是另一个线程无法通过GLES20.glReadPixels()方法读取,因为它不是GL线程。
【问题讨论】: