private void createCameraPreview() { try { SurfaceHolder holder = previewView.getHolder(); holder.setFixedSize(size.getWidth(), size.getHeight()); holder.setKeepScreenOn(true); final Surface surface = holder.getSurface(); captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); captureRequestBuilder.addTarget(surface); cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() { public void onConfigured(CameraCaptureSession session) { try { CaptureRequest captureRequest = captureRequestBuilder.build(); session.setRepeatingRequest(captureRequest, null, null); }catch (CameraAccessException e) { e.printStackTrace(); } } public void onConfigureFailed(CameraCaptureSession session) { Toast.makeText(Diary.this, "Failed to configure camera", Toast.LENGTH_SHORT).show(); } }, null); }catch (CameraAccessException e) { e.printStackTrace(); } }
时间: 2023-12-26 07:05:56 浏览: 136
tiaoxingma.zip_SurfaceHolder_条形码
这段代码是用来创建相机预览的,首先通过SurfaceHolder获取Surface,并设置Surface的大小和保持屏幕常亮。然后通过CameraDevice创建一个CaptureRequestBuilder对象,用CameraDevice.TEMPLATE_PREVIEW模板创建一个捕获请求,并将Surface作为目标添加到该请求中。接着使用CameraDevice.createCaptureSession()方法创建一个相机捕获会话,将Surface作为输出目标传递给该方法。在会话创建完成后,通过会话设置一个重复请求,即不断捕获相机预览画面并显示在Surface上。如果出现CameraAccessException异常,会打印错误信息。如果会话配置失败,则会显示一个Toast提示。
阅读全文