上一篇文章简单的写了一点调用系统摄像头录制视频和使用原生videoView播放视频的方法。那么这篇文章继续完成未完成的作业。
使用SurfaceView实现镜头的预览
布局文件只需要一个SurfaceView
<SurfaceView
android:id="@+id/surface_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Activity实现回调方法
implements SurfaceHolder.Callback
注册回调
@BindView(R.id.surface_view)
SurfaceView mSurfaceView;//预览窗口
Camera camera;
//baseActivity中的方法,也可以直接写在onCreate()中
@Override
protected void initView() {
mSurfaceView.getHolder().addCallback(this);
}
在创建的回调中去开启镜头的预览
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open(BACK);
camera.setDisplayOrientation(90);
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.release();
}
使用TextureView去开启镜头浏览
首先还是定义TextureView 和Camera
TextureView textureView;
Camera camera;
定义一个TextureView.SurfaceTextureListener,并在此处开启预览
private class MySurfaceTextureViewListener implements TextureView.SurfaceTextureListener {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
try {
camera.setPreviewTexture(surfaceTexture);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
surface.release();
camera.setPreviewCallback(null);
camera.stopPreview();
camera.release();
camera = null;
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
}
初始化布局并且设置监听器,打开Camera
//baseActivity中的方法,也可以直接写在onCreate()中
@Override
protected void initView() {
textureView.setSurfaceTextureListener(new MySurfaceTextureViewListener());
camera = Camera.open();
camera.setDisplayOrientation(90);
}
切换前后摄像头
可能写得很复杂,见谅
private static final int FRONT = 1;//前置摄像头标记
private static final int BACK = 0;//后置摄像头标记
private int currentCameraType = BACK;//当前打开的摄像头标记
private void changeCamera() throws IOException{
//切换前后摄像头
int cameraCount = 0;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();//得到摄像头的个数
for(int i = 0; i < cameraCount; i++ ) {
Camera.getCameraInfo(i, cameraInfo);//得到每一个摄像头的信息
if(currentCameraType == BACK) {
//现在是后置,变更为前置
if(cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {//代表摄像头的方位,CAMERA_FACING_FRONT前置 CAMERA_FACING_BACK后置
camera.stopPreview();//停掉原来摄像头的预览
camera.release();//释放资源
camera = null;//取消原来摄像头
camera = Camera.open(i);//打开当前选中的摄像头
camera.setDisplayOrientation(90);
try {
camera.setPreviewDisplay(mSurfaceView.getHolder());//通过surfaceview显示取景画面
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();//开始预览
currentCameraType = FRONT;
break;
}
} else {
//现在是前置, 变更为后置
if(cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {//代表摄像头的方位,CAMERA_FACING_FRONT前置 CAMERA_FACING_BACK后置
camera.stopPreview();//停掉原来摄像头的预览
camera.release();//释放资源
camera = null;//取消原来摄像头
camera = Camera.open(i);//打开当前选中的摄像头
camera.setDisplayOrientation(90);
try {
camera.setPreviewDisplay(mSurfaceView.getHolder());//通过surfaceview显示取景画面
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();//开始预览
currentCameraType = BACK;
break;
}
}
}
}
取到NV21的回调
Android 中Google支持的 Camera Preview Callback的YUV常用格式有两种:一个是NV21,一个是YV12。Android一般默认使用YCbCr_420_SP的格式(NV21)。
配置回调格式
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewFormat(ImageFormat.NV21);
camera.setParameters(parameters);
在 Camera 在 release 之前设置 PreviewCallback 即可,bytes的数据就是NV21回调数据
camera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] bytes, Camera camera) {
}
});
网友评论