SurfaceView预览Camera+GLSurfaceView绘制

        之前用opencv做车道识别是在Camera的帧上做处理,直接影响到了预览的帧速,我想这样对于客户来说是非常不爽的,所以受FastCV Sample例子的启发,想到了用双SurfaceView来解决这个问题。当然,这里我是用SurfaceView来预览Camera,GLSurfaceView来绘制(GLSurfaceView本质上是SurfaceView)。

        言归正传,接下来,让我们一步一步来完成这个Demo。

        首先,我先创建SurfaceView来用作Camera预览,和之前的文章不同的是,我在此处将Camera封装到了SurfaceView中,下面请看我的MySurfaceView.java

package org.hwm.app.opengldemo;

import java.util.List;

import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback{
	
	private static final String TAG = "MySurfaceView";
	
	private SurfaceHolder mSurfaceHolder;
	private Camera mCamera;
	
	public MySurfaceView(Context context) {
		super(context);
		init();
	}

	public MySurfaceView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	private void init() {
		mSurfaceHolder = getHolder();
		mSurfaceHolder.addCallback(this);
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        //mCamera.setDisplayOrientation(90);
        mCamera.startPreview();
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		try {
			
	        mCamera = Camera.open();
	        Camera.Parameters cameraParams = mCamera.getParameters();
	        List<Camera.Size> supportPreviewSizes = cameraParams.getSupportedPictureSizes();
	        cameraParams.setPreviewSize(supportPreviewSizes.get(0).width, supportPreviewSizes.get(0).height);
	        
			if (mCamera != null) {
				mCamera.setPreviewDisplay(mSurfaceHolder);
			}
		} catch (Exception exception) {
			Log.i(TAG, "SufaceView Create failed!");
		}
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		// Surface will be destroyed when replaced with a new screen
		// Always make sure to release the Camera instance
		mCamera.stopPreview();
		mCamera.release();
		mCamera = null;
		
	}

}

        其实和之前是一个道理,只是这样的话&#x
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值