注意:此页面指的是 Camera2 软件包。除非您的应用需要 Camera2 中的特定低级功能,否则我们建议使用 CameraX。CameraX 和 Camera2 都支持 Android 5.0(API 级别 21)及更高版本。
Camera2 提供了一个扩展 API,用于访问设备制造商在各种 Android 设备上实施的扩展。有关支持的扩展模式列表,请参阅 相机扩展。
有关支持扩展的设备列表,请参阅 支持的设备。
扩展架构
下图显示了相机扩展架构。
Camera2 应用可以通过 Camera2 API 使用扩展。Camera2 API 提供了查询可用扩展、配置扩展相机会话以及与相机扩展 OEM 库通信的方法。这使您的应用能够使用夜间模式、HDR、自动模式、散景或人脸修饰等扩展。
测试相机设备是否兼容 Camera2 Extensions API
以下代码片段检查设备是否支持 Camera2 Extensions API。并非所有设备都支持扩展,或者设备可能只支持部分扩展。该代码片段返回支持相机扩展的兼容相机 ID 列表。
Kotlin
private fun getExtensionCameraIds(cameraManager: CameraManager): List<String> = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { cameraManager.cameraIdList.filter { cameraId -> val characteristics = cameraManager.getCameraCharacteristics(cameraId) val extensionCharacteristics = cameraManager.getCameraExtensionCharacteristics(cameraId) val capabilities = characteristics.get(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES) extensionCharacteristics.supportedExtensions.isNotEmpty() && capabilities?.contains( CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE ) ?: false } } else emptyList()
Java
private List<String> getExtensionCameraIds(CameraManager cameraManager) throws CameraAccessException { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { return Arrays.stream(cameraManager.getCameraIdList()).filter(cameraId -> { try { CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId); CameraExtensionCharacteristics extensionCharacteristics = cameraManager.getCameraExtensionCharacteristics(cameraId); IntStream capabilities = Arrays.stream( characteristics.get( CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES ) ); return !extensionCharacteristics.getSupportedExtensions().isEmpty() && capabilities.anyMatch(capability -> capability == CameraCharacteristics .REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE ); } catch (CameraAccessException e) { throw new RuntimeException(e); } }).collect(Collectors.toList()); } else { return Collections.emptyList(); } }
使用 Camera2 Extensions API 创建 CameraExtensionSession
Camera2 Extensions API 与兼容设备一起使用时,可让您访问某些相机扩展。以下代码片段展示了如何创建一个CameraExtensionSession
,以便为现有的 Camera2 应用程序使用夜间拍摄模式。
Kotlin
private val captureCallbacks = object : CameraExtensionSession.ExtensionCaptureCallback() { // Implement Capture Callbacks } private val extensionSessionStateCallback = object : CameraExtensionSession.StateCallback() { override fun onConfigured(session: CameraExtensionSession) { cameraExtensionSession = session try { val captureRequest = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW).apply { addTarget(previewSurface) }.build() session.setRepeatingRequest( captureRequest, Dispatchers.IO.asExecutor(), captureCallbacks ) } catch (e: CameraAccessException) { Snackbar.make( previewView, "Failed to preview capture request", Snackbar.LENGTH_SHORT ).show() requireActivity().finish() } } override fun onClosed(session: CameraExtensionSession) { super.onClosed(session) cameraDevice.close() } override fun onConfigureFailed(session: CameraExtensionSession) { Snackbar.make( previewView, "Failed to start camera extension preview", Snackbar.LENGTH_SHORT ).show() requireActivity().finish() } } private fun startExtensionSession() { val outputConfig = arrayListOf( OutputConfiguration(stillImageReader.surface), OutputConfiguration(previewSurface) ) val extensionConfiguration = ExtensionSessionConfiguration( CameraExtensionCharacteristics.EXTENSION_NIGHT, outputConfig, Dispatchers.IO.asExecutor(), extensionSessionStateCallback ) cameraDevice.createExtensionSession(extensionConfiguration) }
Java
private CameraExtensionSession.ExtensionCaptureCallback captureCallbacks = new CameraExtensionSession.ExtensionCaptureCallback() { // Implement Capture Callbacks }; private CameraExtensionSession.StateCallback extensionSessionStateCallback = new CameraExtensionSession.StateCallback() { @Override public void onConfigured(@NonNull CameraExtensionSession session) { cameraExtensionSession = session; try { CaptureRequest.Builder captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); captureRequestBuilder.addTarget(previewSurface); CaptureRequest captureRequest = captureRequestBuilder.build(); session.setRepeatingRequest(captureRequest, backgroundExecutor, captureCallbacks); } catch (CameraAccessException e) { Snackbar.make( previewView, "Failed to preview capture request", Snackbar.LENGTH_SHORT ).show(); requireActivity().finish(); } } @Override public void onClosed(@NonNull CameraExtensionSession session) { super.onClosed(session); cameraDevice.close(); } @Override public void onConfigureFailed(@NonNull CameraExtensionSession session) { Snackbar.make( previewView, "Failed to start camera extension preview", Snackbar.LENGTH_SHORT ).show(); requireActivity().finish(); } }; private void startExtensionSession() { ArrayList<OutputConfiguration> outputConfig = new ArrayList<>(); outputConfig.add(new OutputConfiguration(stillImageReader.getSurface())); outputConfig.add(new OutputConfiguration(previewSurface)); ExtensionSessionConfiguration extensionConfiguration = new ExtensionSessionConfiguration( CameraExtensionCharacteristics.EXTENSION_NIGHT, outputConfig, backgroundExecutor, extensionSessionStateCallback ); }
其他资源
有关更多信息,请参阅CameraExtensionCharacteristics
,并查看公开的Camera2 Extensions API 示例了解更多内容。