1

没有找到任何将Camera2ConfigCamera2Config.Extender设置为CameraX的示例。

您能否提供一个将这些对象设置为CameraX的示例,以便例如获取回调方法调用。

基本上我想以androidx.camera.camera2.impl.Camera.State的格式获取 Camera 的状态。

4

1 回答 1

5

我最近也需要这个,在撰写本文时,这是对我有用的方式。请注意,这种方式可能会停止工作,因为 CameraX 处于 alpha 状态。

基本上,在调用它并创建 UseCase之前Config.ExtendableBuilder,您将 a 传递给您的构造函数。Camera2Config.Extender build()

作为示例,我从CameraX 示例中获取代码并将其调整为使用Camera2Config.Extender.

// Set up the view finder use case to display camera preview
val viewFinderConfigBuilder = PreviewConfig.Builder().apply {
    setLensFacing(lensFacing)
    // We request aspect ratio but no resolution to let CameraX optimize our use cases
    setTargetAspectRatio(screenAspectRatio)
    // Set initial target rotation, we will have to call this again if rotation changes
    // during the lifecycle of this use case
    setTargetRotation(viewFinder.display.rotation)
}

// Create the extender and pass in the config builder we want to extend
val previewExtender = Camera2Config.Extender(viewFinderConfigBuilder)
// Listen to camera state changes
previewExtender.setDeviceStateCallback(object : CameraDevice.StateCallback() {
    // implementation omitted for sake of simplicity
})

// Build your config as usual and create your wanted UseCase with it
val viewFinderConfig = viewFinderConfigBuilder.build()

// Use the auto-fit preview builder to automatically handle size and orientation changes
preview = AutoFitPreviewBuilder.build(viewFinderConfig, viewFinder)

另外我建议不要使用任何实现细节,而是使用CameraDevice.StateCallback上面示例中的类似。

于 2019-08-12T08:08:20.073 回答