Camera 预览之SurfaceView、TextureView、GLSurfaceView(三)


今天介绍下GLSurfaceView如何使用。GLSurfaceView的包名是android.opengl,由此可以它是opengl的一个类,它也可以预览camera,而且在预览camera上有比SurfaceView独特的优势,可以做到数据和显示的分离,比如在没有屏幕的设备照样可以开预览实时直播。下面要介绍的这个例子是获取camera预览数据编码为视频流。但这篇文章只介绍如何使用GLSurfaceView去预览,关于获取预览数据编码视频流后续会介绍。

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.opengl.GLES11Ext;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.util.AttributeSet;
import android.util.Log;

public class MyGLSurfaceView extends GLSurfaceView implements
                                    Renderer, SurfaceTexture.OnFrameAvailableListener {
    private static final String TAG = "MyGLSurfaceView";
    private Context mContext;
    private SurfaceTexture mSurface;
    private int mTextureID = -1;
    private CameraDrawer mCameraDrawer;
    
    public CameraGLSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        mContext = context;
        setEGLContextClientVersion(2);
        setRenderer(this);
        setRenderMode(RENDERMODE_WHEN_DIRTY);
    }
    
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onSurfaceCreated...");
        mTextureID = createTextureID();
        mSurface = new SurfaceTexture(mTextureID);
        mSurface.setOnFrameAvailableListener(this);
        mCameraDrawer = new CameraDrawer(mTextureID);
        CameraWrapper.getInstance().doOpenCamera(null);

    }
    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onSurfaceChanged..." + width + "/" + height);
        GLES20.glViewport(0, 0, width, height);
        if(!CameraWrapper.getInstance().isPreviewing()){
            CameraWrapper.getInstance().doStartPreview(mSurface);
        }
    }
    
    @Override
    public void onDrawFrame(GL10 gl) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onDrawFrame...");
        GLES20.glClear

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值