具体代码参考:https://github.com/google/grafika/tree/master/src/com/android/grafika
主要流程:
1、生成一个oes textrure
2、通过上面生成的oes texture,new一个surfacetexture,设置回调surfacetexture.setOnFrameAvailableListener(CB);
3、将surfacetexture设置给 camera,开始采集
4、camera数据有更新会通过第二步会调通知
5、有数据回调是通过第一步的texture绘制
6、如果需要编码,则创建编码器,注意format
format.setInteger(MediaFormat.KEY_COLOR_FORMAT,
MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
7、得到编码器的surface
mInputSurface = mEncoder.createInputSurface();
8、在7步的surface上建立EGL环境
9、camera画面更新时,通过OpenGL es2.0绘制一下,绘制代码如下:画完编码。
package com.android.grafika.gles;
import android.opengl.GLES11Ext;
import android.opengl.GLES20;
import android.util.Log;
import java.nio.FloatBuffer;
/**
* GL program and supporting functions for textured 2D shapes.
*/
public class Texture2dProgram {
private static final String TAG = GlUtil.TAG;
public enum ProgramType {
TEXTURE_2D, TEXTURE_EXT, TEXTURE_EXT_BW, TEXTURE_EXT_FILT
}
// Simple vertex shader, used for all programs.
private static final String VERTEX_SHADER =
"uniform mat4 uMVPMatrix;\n" +
"uniform mat4 uTexMatrix;\n" +
"attribute vec4 aPosition;\n" +
"attribute vec4 aTextureCoord;\n" +
"varying vec2 vTextureCoord;\n" +
"void main() {\n" +
" gl_Position = uMVPMatrix * aPosition;\n" +
" vTextureCoord = (uTexMatrix * aTextureCoord).xy;\n" +
"}\n";
// Simple fragment shader for use with "normal" 2D textures.
private static final String FRAGMENT_SHADER_2D =
"precision mediump float;\n" +
"varying vec2 vTextureCoord;\n" +
"uniform sampler2D sTexture;\n" +
"void main() {\n" +
" gl_FragColor = texture2D(sText