Anroid camera + mediacodec

本文介绍如何在Android中结合相机和mediacodec进行视频采集和编码。通过生成OES纹理,设置SurfaceTexture回调,将SurfaceTexture应用到相机并监听帧更新,然后利用OpenGL ES 2.0绘制并编码视频流。详细代码参考链接提供。
摘要由CSDN通过智能技术生成

具体代码参考: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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值