Android Camera采集数据缓存问题
撞墙的开始
刚开始初始化Camera如下
private byte[] yuvData = new byte[size];
public void initCamera() {
mCamera = Camera.open(id);
Parameter cp = mCamera.getParameters();
cp.setPreviewSize(WIDTH, HEIGHT);
mCamera.setParameters(cp);
try {
mCamera.setPreviewCallback(MainActivity.this);
mCamera.setPreviewTexture(new SurfaceTexture(10));
} catch (IOException e) {
Log.w(TAG, "setPreviewTexture error e = " + e.getMessage());
}
}
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
//通过如此方法将数据引用出去
yuvData = data;
}
小米4出现预览画面静止,某款终端正常预览但是出现如下报错
摸爬滚打-小进步
public void openCamera(int id){
mCamera = Camera.open(id);
Parameters cp = mCamera.getParameters();
cp.setPreviewSize(WIDTH, HEIGHT);
//该步骤没有设置,导致186终端绿线花屏,因为186终端默认1280*720采集,但是按照640*480的格式播放,所以造成BUG
mCamera.setParameters(cp);
try {
//复用mData来采集数据
mCamera.setPreviewCallbackWithBuffer(MainActivity.this);
//传入mData
mCamera.addCallbackBuffer(mData);
mCamera.setPreviewTexture(new SurfaceTexture(10));
} catch (IOException e) {
Log.w(TAG, "setPreviewTexture error e = " + e.getMessage());
}
}
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
camera.addCallbackBuffer(data);
}
如此修改后解决如上问题,并且小米4终端能正常预览.