标签:一点 resize release try void mission ack 保存 his
本文主要将的是:使用 Camera API 采集视频数据并保存到文件,分别使用 SurfaceView、TextureView 来预览 Camera 数据,取到 NV21 的数据回调。
注: 需要权限:<uses-permission android:name="android.permission.CAMERA" />
做过Android开发的人一般都知道,有两种方法能够做到这一点:SurfaceView、TextureView。
下面是使用SurfaceView预览数据的方式:
SurfaceView surfaceView;
Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = (SurfaceView) findViewById(R.id.surface_view);
surfaceView.getHolder().addCallback(this);
// 打开摄像头并将展示方向旋转90度
camera = Camera.open();
camera.setDisplayOrientation(90);
}
//------ Surface 预览 -------
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int w, int h) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
camera.release();
}
下面是使用TextureView预览数据的方式:
TextureView textureView; Camera camera; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textureView = (TextureView) findViewById(R.id.texture_view); textureView.setSurfaceTextureListener(this);// 打开摄像头并将展示方向旋转90度 camera = Camera.open(); camera.setDisplayOrientation(90); }
//------ Texture 预览 ------- @Override public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) { try { camera.setPreviewTexture(surfaceTexture); camera.startPreview(); } catch (IOException e) { e.printStackTrace(); } } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) { } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { camera.release(); return false; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { }
Android 音视频开发(四):使用 Camera API 采集视频数据
标签:一点 resize release try void mission ack 保存 his
原文地址:http://www.cnblogs.com/renhui/p/7472778.html