一、預覽Camera數(shù)據(jù)
做過Android開發(fā)的人一般都知道骚秦,有兩種方法能夠做到這一點:SufaceView甲馋、TextureView擂找。
- 下面是使用SurfaceView預覽數(shù)據(jù)的方式:
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預覽數(shù)據(jù)的方式:
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) {
}
二、取到NV21的數(shù)據(jù)回調
Android 中Google支持的Camera Preview CallBack的YUV常用格式有兩種:一種是NV21省有,一種是YV12,Android一般默認使用的是YCbCR_420_sp(NV21)
- 我們可以配置數(shù)據(jù)回調的格式:
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewFormat(ImageFormat.NV21);
camera.setParameters(parameters);
- 通過setPreviewCallback方法監(jiān)聽預覽的回調:
這里面的Bytes的數(shù)據(jù)就是NV21格式的數(shù)據(jù)痒留。