Hi, I am working on the similar function at camera, currently I am using GLSurfaceView and I want to replace it to TextureView.
I follow the sample code that was provided form Romain Guy and I can render a rectangle with my shader, but the surfacetexture that store camera frame data seems null. Here is my flow that is based on Romain Guy: 0. Create a camera object in Activity 1. Pass camera into GLRenderThread when GLRenderThread is starting 2. Create an OpenGL Context in GLRenderThread 3. Generate texture name after context is created in GLRenderThread 4. New a SurfaceTexture with the texture name 5. Pass SurfaceTexture to camera and start preview Here is the confusing part: 6. Listen for update (TextureView listen surface is available or not using onSurfaceTextureAvailable, but this SurfaceTexture is render target not input texture. That mean I have to listen camera frame, but how? Do I have to create a new camera object and implement FrameAvailableListener?) Any tips or suggestion for me please. Thanks. Brad Grimm於 2011年12月23日星期五UTC+8上午3時04分10秒寫道: > > Brilliant! I must have been thinking too hard about it, because what > you wrote really is simple. I was able to get it working and it works > like a charm. > > Thank you! > > On Dec 22, 10:16 am, Romain Guy <[email protected]> wrote: > > It is definitely an option, that's whatSurfaceTextureis for. It's very > simple: > > > > Create an OpenGL context > > Generate an OpenGL texture name > > Create aSurfaceTexturewith the texture name > > Pass theSurfaceTextureto Camera > > Listen for updates > > OnSurfaceTextureupdate, draw the texture with OpenGL using the shader > you want > > > > Simple :) > > > > > > > > > > > > > > > > On Thu, Dec 22, 2011 at 11:59 AM, Brad Grimm <[email protected]> wrote: > > > Thanks Romain. I really just want to swap out the pixel shader on the > > >SurfaceTexture'sOpenGL texture so I can do some filtering on the > > > camera preview. But it sounds like that isn't an option. I have > > > used ColorMatrixFilters on the view itself and they work quite well, > > > just not powerful enough for what I want to do. > > > > > On Dec 21, 8:18 pm, Romain Guy <[email protected]> wrote: > > >> There's no need to get the camera preview out ofSurfaceTexture. The > whole > > >> point ofSurfaceTextureis to let a producer like the camera render > into an > > >> OpenGL texture. Just useSurfaceTexture. > > > > >> To draw on top of TextureView, just add another view on top of it. > > >> On Dec 21, 2011 9:35 PM, "Brad Grimm" <[email protected]> wrote: > > > > >> > Thanks for the post, it has been really helpful. > > > > >> > But say I want to draw on top of aSurfaceTexturethat is connected > to > > >> > a Camera via setPreviewTexture. I can get theSurfaceTextureby a > > >> > SurfaceTextureListener. But if I just try to use > > >> > eglCreateWindowSurface & eglMakeCurrent it fails due to the surface > > >> > already being connected. > > >> > 1) Is it possible to draw on this surface at all? Or does binding > it > > >> > to the camera make it impossible to do that. > > >> > 2) If it isn't possible. Can the surface be used (or copied) > quickly > > >> > to an OpenGL texture and used in a separate surface? If so how? > > >> > 3) If not, what would be the fastest way to get the camera preview > out > > >> > of aSurfaceTextureand into OpenGL? > > >> > P.S. - I too followed suit and posted to StackOverflow: > > > > >> > > http://stackoverflow.com/questions/8582090/possible-to-draw-on-surfac... > > >> > On Nov 23, 11:45 am, plafayette <[email protected]> > wrote: > > >> > > Thanks for the quick response Romain and the sample code! Will > give it > > >> > > a try. > > > > >> > > On Nov 23, 12:17 pm, Romain Guy <[email protected]> wrote: > > > > >> > > > GLSurfaceView handles GL setup for you, whichTextureViewwill > not do. A > > >> > > >TextureViewcan be used as the native window when you create an > EGL > > >> > > > surface. Here is an example (the interesting part is the call > > >> > > > to eglCreateWindowSurface()): > > > > >> > > > @Override > > >> > > > public void > onSurfaceTextureAvailable(SurfaceTexturesurface, int > > >> > > > width, int height) { > > >> > > > mRenderThread = new RenderThread(getResources(), > surface); > > >> > > > mRenderThread.start(); > > >> > > > } > > > > >> > > > private static class RenderThread extends Thread { > > >> > > > private static final String LOG_TAG = "GLTextureView"; > > > > >> > > > static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098; > > >> > > > static final int EGL_OPENGL_ES2_BIT = 4; > > > > >> > > > private volatile boolean mFinished; > > > > >> > > > private final Resources mResources; > > >> > > > private finalSurfaceTexturemSurface; > > > > >> > > > private EGL10 mEgl; > > >> > > > private EGLDisplay mEglDisplay; > > >> > > > private EGLConfig mEglConfig; > > >> > > > private EGLContext mEglContext; > > >> > > > private EGLSurface mEglSurface; > > >> > > > private GL mGL; > > > > >> > > > RenderThread(Resources resources,SurfaceTexturesurface) > { > > >> > > > mResources = resources; > > >> > > > mSurface = surface; > > >> > > > } > > > > >> > > > private static final String sSimpleVS = > > >> > > > "attribute vec4 position;\n" + > > >> > > > "attribute vec2 texCoords;\n" + > > >> > > > "varying vec2 outTexCoords;\n" + > > >> > > > "\nvoid main(void) {\n" + > > >> > > > " outTexCoords = texCoords;\n" + > > >> > > > " gl_Position = position;\n" + > > >> > > > "}\n\n"; > > >> > > > private static final String sSimpleFS = > > >> > > > "precision mediump float;\n\n" + > > >> > > > "varying vec2 outTexCoords;\n" + > > >> > > > "uniform sampler2D texture;\n" + > > >> > > > "\nvoid main(void) {\n" + > > >> > > > " gl_FragColor = texture2D(texture, > > >> > outTexCoords);\n" + > > >> > > > "}\n\n"; > > > > >> > > > private static final int FLOAT_SIZE_BYTES = 4; > > >> > > > private static final int > TRIANGLE_VERTICES_DATA_STRIDE_BYTES = > > >> > 5 * > > >> > > > FLOAT_SIZE_BYTES; > > >> > > > private static final int > TRIANGLE_VERTICES_DATA_POS_OFFSET = 0; > > >> > > > private static final int > TRIANGLE_VERTICES_DATA_UV_OFFSET = 3; > > >> > > > private final float[] mTriangleVerticesData = { > > >> > > > // X, Y, Z, U, V > > >> > > > -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, > > >> > > > 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, > > >> > > > -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, > > >> > > > 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, > > >> > > > }; > > > > >> > > > @Override > > >> > > > public void run() { > > >> > > > initGL(); > > > > >> > > > FloatBuffer triangleVertices = > > >> > > > ByteBuffer.allocateDirect(mTriangleVerticesData.length > > >> > > > * > > >> > > > > FLOAT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asFloatBuffer(); > > >> > > > > triangleVertices.put(mTriangleVerticesData).position(0); > > > > >> > > > int texture = loadTexture(R.drawable.large_photo); > > >> > > > int program = buildProgram(sSimpleVS, sSimpleFS); > > > > >> > > > int attribPosition = glGetAttribLocation(program, > > >> > "position"); > > >> > > > checkGlError(); > > > > >> > > > int attribTexCoords = glGetAttribLocation(program, > > >> > "texCoords"); > > >> > > > checkGlError(); > > > > >> > > > int uniformTexture = glGetUniformLocation(program, > > >> > "texture"); > > >> > > > checkGlError(); > > > > >> > > > glBindTexture(GL_TEXTURE_2D, texture); > > >> > > > checkGlError(); > > > > >> > > > glUseProgram(program); > > >> > > > checkGlError(); > > > > >> > > > glEnableVertexAttribArray(attribPosition); > > >> > > > checkGlError(); > > > > >> > > > glEnableVertexAttribArray(attribTexCoords); > > >> > > > checkGlError(); > > > > >> > > > glUniform1i(uniformTexture, texture); > > >> > > > checkGlError(); > > > > >> > > > while (!mFinished) { > > >> > > > checkCurrent(); > > > > >> > > > glClearColor(0.0f, 0.0f, 0.0f, 0.0f); > > >> > > > checkGlError(); > > > > >> > > > glClear(GL_COLOR_BUFFER_BIT); > > >> > > > checkGlError(); > > > > >> > > > // drawQuad > > > > >> > > > triangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET); > > >> > > > glVertexAttribPointer(attribPosition, 3, > GL_FLOAT, > > >> > false, > > >> > > > TRIANGLE_VERTICES_DATA_STRIDE_BYTES, > > >> > > > triangleVertices); > > > > >> > triangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET); > > >> > > > glVertexAttribPointer(attribTexCoords, 3, > GL_FLOAT, > > >> > false, > > >> > > > TRIANGLE_VERTICES_DATA_STRIDE_BYTES, > > >> > > > triangleVertices); > > > > >> > > > glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); > > > > >> > > > if (!mEgl.eglSwapBuffers(mEglDisplay, > mEglSurface)) { > > >> > > > throw new RuntimeException("Cannot swap > buffers"); > > >> > > > } > > >> > > > checkEglError(); > > > > >> > > > try { > > >> > > > Thread.sleep(2000); > > >> > > > } catch (InterruptedException e) { > > >> > > > // Ignore > > >> > > > } > > >> > > > } > > > > >> > > > finishGL(); > > >> > > > } > > > > >> > > > private int loadTexture(int resource) { > > >> > > > int[] textures = new int[1]; > > > > >> > > > glActiveTexture(GL_TEXTURE0); > > >> > > > glGenTextures(1, textures, 0); > > >> > > > checkGlError(); > > > > >> > > > int texture = textures[0]; > > >> > > > glBindTexture(GL_TEXTURE_2D, texture); > > >> > > > checkGlError(); > > > > >> > > > glTexParameteri(GL_TEXTURE_2D, > GL_TEXTURE_MIN_FILTER, > > >> > > > GL_LINEAR); > > >> > > > glTexParameteri(GL_TEXTURE_2D, > GL_TEXTURE_MAG_FILTER, > > >> > > > GL_LINEAR); > > > > >> > > > glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, > > >> > > > GL_CLAMP_TO_EDGE); > > >> > > > glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, > > >> > > > GL_CLAMP_TO_EDGE); > > > > >> > > > Bitmap bitmap = > BitmapFactory.decodeResource(mResources, > > >> > > > resource); > > > > >> > > > GLUtils.texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, > bitmap, > > >> > > > GL_UNSIGNED_BYTE, 0); > > >> > > > checkGlError(); > > > > >> > > > bitmap.recycle(); > > > > >> > > > return texture; > > >> > > > } > > > > >> > > > ... > > > > read more >> -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

