Camera2预览步骤学习总结
private TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture texture, int width, int height) {
Log.i(TAG,"onSurfaceTextureAvailable width:" + width + " height:" + height);
if(mCameraManager == null){
mCameraManager = (CameraManager)CameraActivity.this.getSystemService(Context.CAMERA_SERVICE);
}
try {
String[] cameraIds = mCameraManager.getCameraIdList();
for(String id : cameraIds){
Log.i(TAG,"id = " + id);
}
//open background camera
if(cameraIds.length > 1) {
mCameraCharacteristics = mCameraManager.getCameraCharacteristics(cameraIds[1]);
List> requestKeyList = mCameraCharacteristics.getAvailableSessionKeys();
for (CaptureRequest.Key requestKey : requestKeyList) {
Log.i(TAG,"requestKey.getName() = " +requestKey.getName());
}
StreamConfigurationMap configMap = mCameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
//Size[] candidateSizes = configMap.getOutputSizes(Surface.class);
Size[] candidateSizes = configMap.getOutputSizes(ImageFormat.JPEG);
for (Size candidateSize : candidateSizes){
Log.i(TAG,"prview Width = " + candidateSize.getWidth() + " height = " + candidateSize.getHeight());
}
Log.i(TAG,"orientation = " + mCameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION));
//Log.i(TAG,"scenes = " + mCameraCharacteristics.get("com.mediatek.vsdoffeature.vsdofFeaturePreviewSize").toString());
mCameraManager.openCamera(cameraIds[1], mDeviceCallback, mBgHander);
}
//android.util.Log.i("zmao", "cameraIds = " + cameraIds.toString());
} catch (Exception e){
Log.i(TAG,"onSurfaceTextureAvailable exception " + e);
}
}
private void createCameraPreviewSession(Handler handler) {
try {
Log.i(TAG,"createCameraPreviewSession");
// 获取SurfaceTexture并设置默认缓冲区大小
SurfaceTexture texture = mPreview.getSurfaceTexture();
texture.setDefaultBufferSize(mPreview.getWidth(), mPreview.getHeight());
// 创建预览Surface
Surface surface = new Surface(texture);
// 创建CaptureRequest.Builder并设置预览Surface为目标
mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewRequestBuilder.addTarget(surface);
// 创建ImageReader并设置回调
// mImageReader = ImageReader.newInstance(mTextureView.getWidth(), mTextureView.getHeight(), ImageFormat.JPEG, 1);
mImageReader = ImageReader.newInstance(mPreview.getWidth(), mPreview.getHeight(), ImageFormat.JPEG, 1);
// mImageReader = ImageReader.newInstance(mPreview.getWidth(), mPreview.getHeight(), ImageFormat.YUV_420_888, 2);
mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, handler);
// 将ImageReader的Surface添加到CaptureRequest.Builder中
Surface readerSurface = mImageReader.getSurface();
mPreviewRequestBuilder.addTarget(readerSurface);
// 创建预览会话
mCameraDevice.createCaptureSession(Arrays.asList(surface, readerSurface), mSessionCallback, handler);
Log.i(TAG,"createCameraPreviewSession end");
} catch (Exception e) {
e.printStackTrace();
}
}
private void updatePreview() {
try {
Log.i(TAG,"updatePreview");
// 设置自动对焦模式
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
// 构建预览请求
// 发送预览请求
mCameraCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
private ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
if(DBG) {
Log.d(TAG, "onImageAvailable");
}
//获得mage
Image image = null;
try {
//image = reader.acquireLatestImage();
image = reader.acquireNextImage();
if(mCapature){
Log.i(TAG,"capature save");
ImageSaver imageSaver = new ImageSaver(CameraActivity.this, image);
new Thread(imageSaver).start();
} else {
image.close();
}
}catch(Exception e){
Log.i(TAG,"OnImageAvailableListener error");
}
如此Camera2预览基本实现