之前用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