Android平台Camera实时滤镜实现方法探讨(五)--GLSurfaceView实现Camera预览

前面有一篇探讨了如何在片段着色器中将YUV数据转换为RGB数据并显示,但采用samplerExternalOES将SurfaceTexture作为OpenGL外部纹理,需要使用GL_TEXTURE_EXTERNAL_OES作为纹理模板通过SetPreviewTexture将Camera数据图像输出到SurfaceTexture,调用updateTexImage()时将对应纹理更新为最新的一帧,然后通知OpenGL绘制对应纹理。

1.创建纹理ID

int[] texture = new int[1];    
GLES20.glGenTextures(1, texture, 0);  
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture[0]);  
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,  
            GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR);          
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,  
            GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);  
GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,  
            GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);  
GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,  
            GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);         
return texture[0];  
2.根据纹理id创建SurfaceTexture

mSurfaceTexture = new SurfaceTexture(textureId);
mSurfaceTexture.setOnFrameAvailableListener(listener);
private OnFrameAvailableListener listener = new OnFrameAvailableListener() {
		
	@Override
	public void onFrameAvailable(SurfaceTexture surfaceTexture) {
		// TODO Auto-generated method stub
		mGlSurfaceView.requestRender();
	}
};
3.更改Shader

顶点着色器与之前相比多了个textureTransform,用于接收SurfaceTexture变换矩阵,如果不需要也可以不盖被vertext shader。

片段着色器需要增加#extension GL_OES_EGL_image_external : require,因为第一步的纹理都绑定到GL_TEXTURE_EXTERNAL_OES

对应采样方式由Samlpe2D更改成samplerExternalOES。

public static final String NO_FILTER_VERTEX_SHADER = "" +
    		"attribute vec4 position;\n" +  
		   " attribute vec4 inputTextureCoordinate;\n" +  
		   " \n" +
		    "uniform mat4 textureTransform;\n" +  
		    "varying vec2 textureCoordinate;\n" +  
		    " \n" +
		   " void main()\n" +  
		    "{\n" +  
		    "textureCoordinate = (textureTransform * inputTextureCoordinate).xy;\n" +  
		    "gl_Position = position;\n" +  
		    "}";
    public static final String NO_FILTER_FRAGMENT_SHADER = "" +
    		"#extension GL_OES_EGL_image_external : require\n"+  
            "precision mediump float;" +  
            "varying vec2 textureCoordinate;\n" +  
            "uniform samplerExternalOES inputImageTexture;\n" +  
            " \n" +
            "void main() {" +  
            "  gl_FragColor = texture2D( inputImageTexture, textureCoordinate );\n" +  
            "}";  

4.绘制

与之前采用glTexImage2D绑定2D图像纹理不同,这里通过GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureid);绑定外部纹理即可。

如果使用了SurfaceTexture变换矩阵,可采用glUniformMatrix4fv传递给Shader


采用片段着色器方案的时间消耗主要在数据转移中,本方法的格式相关工作交给EGLImage处理,用到了Lock ANativeWindow。具体优劣尚未探讨,如有经验人士希望指点迷津。

你可能感兴趣的:(Android平台Camera实时滤镜实现方法探讨(五)--GLSurfaceView实现Camera预览)