具体代码参考: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(sTexture, vTextureCoord);\n" +"}\n";// Simple fragment shader for use with external 2D textures (e.g. what we get from// SurfaceTexture).private static final String FRAGMENT_SHADER_EXT ="#extension GL_OES_EGL_image_external : require\n" +"precision mediump float;\n" +"varying vec2 vTextureCoord;\n" +"uniform samplerExternalOES sTexture;\n" +"void main() {\n" +"    gl_FragColor = texture2D(sTexture, vTextureCoord);\n" +"}\n";// Fragment shader that converts color to black & white with a simple transformation.private static final String FRAGMENT_SHADER_EXT_BW ="#extension GL_OES_EGL_image_external : require\n" +"precision mediump float;\n" +"varying vec2 vTextureCoord;\n" +"uniform samplerExternalOES sTexture;\n" +"void main() {\n" +"    vec4 tc = texture2D(sTexture, vTextureCoord);\n" +"    float color = tc.r * 0.3 + tc.g * 0.59 + tc.b * 0.11;\n" +"    gl_FragColor = vec4(color, color, color, 1.0);\n" +"}\n";// Fragment shader with a convolution filter.  The upper-left half will be drawn normally,// the lower-right half will have the filter applied, and a thin red line will be drawn// at the border.//// This is not optimized for performance.  Some things that might make this faster:// - Remove the conditionals.  They're used to present a half & half view with a red//   stripe across the middle, but that's only useful for a demo.// - Unroll the loop.  Ideally the compiler does this for you when it's beneficial.// - Bake the filter kernel into the shader, instead of passing it through a uniform//   array.  That, combined with loop unrolling, should reduce memory accesses.public static final int KERNEL_SIZE = 9;private static final String FRAGMENT_SHADER_EXT_FILT ="#extension GL_OES_EGL_image_external : require\n" +"#define KERNEL_SIZE " + KERNEL_SIZE + "\n" +"precision highp float;\n" +"varying vec2 vTextureCoord;\n" +"uniform samplerExternalOES sTexture;\n" +"uniform float uKernel[KERNEL_SIZE];\n" +"uniform vec2 uTexOffset[KERNEL_SIZE];\n" +"uniform float uColorAdjust;\n" +"void main() {\n" +"    int i = 0;\n" +"    vec4 sum = vec4(0.0);\n" +"    if (vTextureCoord.x < vTextureCoord.y - 0.005) {\n" +"        for (i = 0; i < KERNEL_SIZE; i++) {\n" +"            vec4 texc = texture2D(sTexture, vTextureCoord + uTexOffset[i]);\n" +"            sum += texc * uKernel[i];\n" +"        }\n" +"    sum += uColorAdjust;\n" +"    } else if (vTextureCoord.x > vTextureCoord.y + 0.005) {\n" +"        sum = texture2D(sTexture, vTextureCoord);\n" +"    } else {\n" +"        sum.r = 1.0;\n" +"    }\n" +"    gl_FragColor = sum;\n" +"}\n";private ProgramType mProgramType;// Handles to the GL program and various components of it.private int mProgramHandle;private int muMVPMatrixLoc;private int muTexMatrixLoc;private int muKernelLoc;private int muTexOffsetLoc;private int muColorAdjustLoc;private int maPositionLoc;private int maTextureCoordLoc;private int mTextureTarget;private float[] mKernel = new float[KERNEL_SIZE];private float[] mTexOffset;private float mColorAdjust;/*** Prepares the program in the current EGL context.*/public Texture2dProgram(ProgramType programType) {mProgramType = programType;switch (programType) {case TEXTURE_2D:mTextureTarget = GLES20.GL_TEXTURE_2D;mProgramHandle = GlUtil.createProgram(VERTEX_SHADER, FRAGMENT_SHADER_2D);break;case TEXTURE_EXT:mTextureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES;mProgramHandle = GlUtil.createProgram(VERTEX_SHADER, FRAGMENT_SHADER_EXT);break;case TEXTURE_EXT_BW:mTextureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES;mProgramHandle = GlUtil.createProgram(VERTEX_SHADER, FRAGMENT_SHADER_EXT_BW);break;case TEXTURE_EXT_FILT:mTextureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES;mProgramHandle = GlUtil.createProgram(VERTEX_SHADER, FRAGMENT_SHADER_EXT_FILT);break;default:throw new RuntimeException("Unhandled type " + programType);}if (mProgramHandle == 0) {throw new RuntimeException("Unable to create program");}Log.d(TAG, "Created program " + mProgramHandle + " (" + programType + ")");// get locations of attributes and uniformsmaPositionLoc = GLES20.glGetAttribLocation(mProgramHandle, "aPosition");GlUtil.checkLocation(maPositionLoc, "aPosition");maTextureCoordLoc = GLES20.glGetAttribLocation(mProgramHandle, "aTextureCoord");GlUtil.checkLocation(maTextureCoordLoc, "aTextureCoord");muMVPMatrixLoc = GLES20.glGetUniformLocation(mProgramHandle, "uMVPMatrix");GlUtil.checkLocation(muMVPMatrixLoc, "uMVPMatrix");muTexMatrixLoc = GLES20.glGetUniformLocation(mProgramHandle, "uTexMatrix");GlUtil.checkLocation(muTexMatrixLoc, "uTexMatrix");muKernelLoc = GLES20.glGetUniformLocation(mProgramHandle, "uKernel");if (muKernelLoc < 0) {// no kernel in this onemuKernelLoc = -1;muTexOffsetLoc = -1;muColorAdjustLoc = -1;} else {// has kernel, must also have tex offset and color adjmuTexOffsetLoc = GLES20.glGetUniformLocation(mProgramHandle, "uTexOffset");GlUtil.checkLocation(muTexOffsetLoc, "uTexOffset");muColorAdjustLoc = GLES20.glGetUniformLocation(mProgramHandle, "uColorAdjust");GlUtil.checkLocation(muColorAdjustLoc, "uColorAdjust");// initialize default valuessetKernel(new float[] {0f, 0f, 0f,  0f, 1f, 0f,  0f, 0f, 0f}, 0f);setTexSize(256, 256);}}/*** Releases the program.* <p>* The appropriate EGL context must be current (i.e. the one that was used to create* the program).*/public void release() {Log.d(TAG, "deleting program " + mProgramHandle);GLES20.glDeleteProgram(mProgramHandle);mProgramHandle = -1;}/*** Returns the program type.*/public ProgramType getProgramType() {return mProgramType;}/*** Creates a texture object suitable for use with this program.* <p>* On exit, the texture will be bound.*/public int createTextureObject() {int[] textures = new int[1];GLES20.glGenTextures(1, textures, 0);GlUtil.checkGlError("glGenTextures");int texId = textures[0];GLES20.glBindTexture(mTextureTarget, texId);GlUtil.checkGlError("glBindTexture " + texId);GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_NEAREST);GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_LINEAR);GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S,GLES20.GL_CLAMP_TO_EDGE);GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T,GLES20.GL_CLAMP_TO_EDGE);GlUtil.checkGlError("glTexParameter");return texId;}/*** Configures the convolution filter values.** @param values Normalized filter values; must be KERNEL_SIZE elements.*/public void setKernel(float[] values, float colorAdj) {if (values.length != KERNEL_SIZE) {throw new IllegalArgumentException("Kernel size is " + values.length +" vs. " + KERNEL_SIZE);}System.arraycopy(values, 0, mKernel, 0, KERNEL_SIZE);mColorAdjust = colorAdj;//Log.d(TAG, "filt kernel: " + Arrays.toString(mKernel) + ", adj=" + colorAdj);}/*** Sets the size of the texture.  This is used to find adjacent texels when filtering.*/public void setTexSize(int width, int height) {float rw = 1.0f / width;float rh = 1.0f / height;// Don't need to create a new array here, but it's syntactically convenient.mTexOffset = new float[] {-rw, -rh,   0f, -rh,    rw, -rh,-rw, 0f,    0f, 0f,     rw, 0f,-rw, rh,    0f, rh,     rw, rh};//Log.d(TAG, "filt size: " + width + "x" + height + ": " + Arrays.toString(mTexOffset));}/*** Issues the draw call.  Does the full setup on every call.** @param mvpMatrix The 4x4 projection matrix.* @param vertexBuffer Buffer with vertex position data.* @param firstVertex Index of first vertex to use in vertexBuffer.* @param vertexCount Number of vertices in vertexBuffer.* @param coordsPerVertex The number of coordinates per vertex (e.g. x,y is 2).* @param vertexStride Width, in bytes, of the position data for each vertex (often*        vertexCount * sizeof(float)).* @param texMatrix A 4x4 transformation matrix for texture coords.  (Primarily intended*        for use with SurfaceTexture.)* @param texBuffer Buffer with vertex texture data.* @param texStride Width, in bytes, of the texture data for each vertex.*/public void draw(float[] mvpMatrix, FloatBuffer vertexBuffer, int firstVertex,int vertexCount, int coordsPerVertex, int vertexStride,float[] texMatrix, FloatBuffer texBuffer, int textureId, int texStride) {GlUtil.checkGlError("draw start");// Select the program.GLES20.glUseProgram(mProgramHandle);GlUtil.checkGlError("glUseProgram");// Set the texture.GLES20.glActiveTexture(GLES20.GL_TEXTURE0);GLES20.glBindTexture(mTextureTarget, textureId);// Copy the model / view / projection matrix over.GLES20.glUniformMatrix4fv(muMVPMatrixLoc, 1, false, mvpMatrix, 0);GlUtil.checkGlError("glUniformMatrix4fv");// Copy the texture transformation matrix over.GLES20.glUniformMatrix4fv(muTexMatrixLoc, 1, false, texMatrix, 0);GlUtil.checkGlError("glUniformMatrix4fv");// Enable the "aPosition" vertex attribute.GLES20.glEnableVertexAttribArray(maPositionLoc);GlUtil.checkGlError("glEnableVertexAttribArray");// Connect vertexBuffer to "aPosition".GLES20.glVertexAttribPointer(maPositionLoc, coordsPerVertex,GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);GlUtil.checkGlError("glVertexAttribPointer");// Enable the "aTextureCoord" vertex attribute.GLES20.glEnableVertexAttribArray(maTextureCoordLoc);GlUtil.checkGlError("glEnableVertexAttribArray");// Connect texBuffer to "aTextureCoord".GLES20.glVertexAttribPointer(maTextureCoordLoc, 2,GLES20.GL_FLOAT, false, texStride, texBuffer);GlUtil.checkGlError("glVertexAttribPointer");// Populate the convolution kernel, if present.if (muKernelLoc >= 0) {GLES20.glUniform1fv(muKernelLoc, KERNEL_SIZE, mKernel, 0);GLES20.glUniform2fv(muTexOffsetLoc, KERNEL_SIZE, mTexOffset, 0);GLES20.glUniform1f(muColorAdjustLoc, mColorAdjust);}// Draw the rect.GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, firstVertex, vertexCount);GlUtil.checkGlError("glDrawArrays");// Done -- disable vertex array, texture, and program.GLES20.glDisableVertexAttribArray(maPositionLoc);GLES20.glDisableVertexAttribArray(maTextureCoordLoc);GLES20.glBindTexture(mTextureTarget, 0);GLES20.glUseProgram(0);}
}

注意:Texture2dProgram 初始化的时候可以设置oes、2D等参数,需要根据自己具体传入什么类型的纹理决定,比如我们传入的是oes,就设置oes.

Anroid camera + mediacodec相关推荐

  1. 游戏开发需要具备哪些技术_短视频 SDK 开发 (一) 开发一款短视频 SDK 需要具备哪些知识?...

    前言 2020 年要属什么最火,肯定是短视频和直播带货了.我自己基本上每天晚上睡觉之前都会刷一会儿 douyin 短视频,不得不承认 douyin 的推荐算法是真 nb ,推荐的都是我的最爱 ? .那 ...

  2. 短视频 SDK 开发 (一) 开发一款短视频 SDK 需要具备哪些知识?

    前言 2020 年要属什么最火,肯定是短视频和直播带货了.我自己基本上每天晚上睡觉之前都会刷一会儿 douyin 短视频,不得不承认 douyin 的推荐算法是真 nb ,推荐的都是我的最爱 ???? ...

  3. Android硬编解码接口MediaCodec使用完全解析(一)

    使用异步读取编码(解码)后的数据,效率会大增. 可以直接起一个线程不断地读. ------------------------------------------------------------- ...

  4. 研究Android音视频-3-在Android设备上采集音视频并使用MediaCodec编码为H.264

    原文 : https://juejin.cn/post/69601302052266311754 本文解决的问题 本文主要使用 MediaCodec 硬编码器对 Android 设备采集的音视频编码 ...

  5. MediaCodec问题汇总

    参考:http://blog.csdn.net/mincheat/article/details/51385144 MediaCodec的基本用法,网上一大把,这里就不写了 1, 获取支持分辨率问题 ...

  6. Android平台使用Camera2(5.0+)替代过时的Camera

    转自:https://forums.developer.amazon.com/articles/2707/using-camera2-to-replace-deprecated-camera-api. ...

  7. YUV通过MediaCodec编码H264

    Camear_MainActivity.java,主界面类 import android.app.Activity; import android.graphics.ImageFormat; impo ...

  8. Android平台美颜相机/Camera实时滤镜/视频编解码/影像后期/人脸技术探索——1.1 工程思路与难点

    回到目录 本文主要探讨搭建一款Android平台下美颜相机可能需要填的坑,内容会不断更新.. 相机框架 相机框架相对比较简单,现有的开源代码很多,可以很容易的实现拍照和录像的功能. 预览尺寸选择 预览 ...

  9. 【mediacodec】MediaRecorder--MediaCodec

    文章目录 前言 查看代码重点关注log 1 应用层 1.1 创建 1.2 获取 1.3 设置VideoSource 1.4 设置输出模式 1.5 设置文件名 1.6 设置编码方式 2 获取video ...

最新文章

  1. 设计模式-行为-迭代器
  2. ZAB协议选主过程详解
  3. define、const、typedef区别
  4. 串口服务器的通讯模式
  5. Google Map二次开发——API方式
  6. java socket长连接怎么维持_socket长连接的维持
  7. au如何关闭预览编辑器_VS Code如何内置Chrome浏览器?超简单
  8. LeetCode 647. Palindromic Substrings
  9. 【风马一族_C】进制转化
  10. 7.20-7.26 字节推荐算法(DATA-EDU)5道面试题分享
  11. 绘声绘影2023简体中文版新功能介绍
  12. 面试系列(二十):金山云 C++开发
  13. ctfshow-web入门-node.js
  14. PCB javascript实现个税5000计算
  15. 为什么程序员都喜欢节后跳槽?内行人告诉你原因
  16. 计算机休眠设置电源开关,电脑中的待机、休眠、睡眠和关机状态的区别。
  17. 来自python的【数字Number总结/Math数字函数】
  18. 芯旺微烧录器使用及驱动安装
  19. 竞价猎马技术具体操作
  20. 逆向工程师逆向还原APP和代码,国产化就是这样

热门文章

  1. S2SH新手框架结构的准备工作只需要导入这些文件
  2. 安装nginx之前的组件
  3. 多么漂亮的重载构造方法呀!爱上.net
  4. 索爱确认2月13日发布Xperia Play
  5. WebNN 人物检测、人脸识别、超分辨率、图像字幕、情感分析、噪声抑制
  6. Spring Boot中文文档
  7. 干货:手把手教你在音频分类DCASE2017比赛中夺冠
  8. 深度学习 --- 优化入门六(正则化、参数范数惩罚L0、L1、L2、Dropout)
  9. linux bash 删除所有空格,删除字符串中的所有的空白并用空格分割单词.md
  10. python爬虫 书籍 外文_Python爬虫——WuXiaWorld英文版小说