標籤:一點 resize release try void mission ack 儲存 his
本文主要將的是:使用 Camera API 採集視頻資料並儲存到檔案,分別使用 SurfaceView、TextureView 來預覽 Camera 資料,取到 NV21 的資料回調。
註: 需要許可權:<uses-permission android:name="android.permission.CAMERA" />
一、預覽 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 採集視頻資料