Java Android: 如何获取GLSurfaceView上位置(X,Y处)的像素颜色?

huangapple go评论78阅读模式
英文:

Java Android: how to get pixel color at position (X, Y) of GLSurfaceView?

问题

我有一个 android.opengl.GLSurfaceView(在一个 ARCore 项目中)

我想要获取该视图在位置(X,Y)处像素的颜色。

我该如何做?(以最高效的方式,因为我每帧会多次这样做)

谢谢!

英文:

I have a android.opengl.GLSurfaceView (in a ARCore project)

I want to get the color of a pixel at the position (X, Y) of this view.

How can I do that ? (in the most efficient way because I will do that many times per frame)

Thanks !

答案1

得分: 1

没有有效的方式来执行这样的任务。原因是因为由于GLSurfaceView使用OpenGL执行渲染,像素必须从GPU中检索。不要忘记检索必须在实际的GLSurfaceView渲染线程中完成。您可以通过渲染器监听器访问其渲染线程,最佳选项是onDrawFrame回调。

要获取特定像素(或所有像素),请使用glReadPixels方法。您可以指定x/y坐标和宽度/高度中的像素数量。

ByteBuffer buffer = ByteBuffer.allocate(4); // 4 = (1宽) * (1高) * (RGBA为4)
GLES20.glReadPixels(x, y, 1, 1, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buffer);

在检索特定坐标时,请考虑OpenGL的上下颠倒性质

请注意,glReadPixels在许多设备上速度非常慢。如果只是偶尔请求一次,这不是问题,但如果必须连续调用它,需要考虑这一点。

英文:

There is no efficient way to do such task. The reason is that since GLSurfaceView performs the rendering with OpenGL, the pixel must be retrieved from the GPU. Not forgetting that the retreival must be done within the actual GLSurfaceView rendering thread. You can access its render thread, via the renderer listener, the best option is the onDrawFrame callback.

To get a specific pixel (or all pixels), use the glReadPixels method. You can specify at which x/y coordinates and the amount of pixels by width/height.

ByteBuffer buffer = ByteBuffer.allocate(4); // 4 = (1 width) * (1 height) * (4 as per RGBA)
GLES20.glReadPixels(x, y, 1, 1, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buffer);

When retrieving a specific coordinate, take into consideration the upside-down nature of OpenGL.

Notice that glReadPixels is notoriously slow in many devices. Not an issue if is for a simple one time request, but to be taken into consideration if you must call it nonstop.

huangapple
  • 本文由 发表于 2020年10月16日 18:25:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64387362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定