gpt4 book ai didi

Android-CameraX:在多个前置摄像头之间切换

转载 作者:行者123 更新时间:2023-12-02 13:30:41 38 4
gpt4 key购买 nike

我正在尝试编写我的第一个 Android 相机应用程序,但它总是选择变焦相机而不是主相机。 (我在华为 P30 Pro 上测试过)

并且代码基于官方的camerax示例应用程序(https://github.com/android/camera-samples/tree/master/CameraXBasic)

相关代码:

/** Declare and bind preview, capture and analysis use cases */
private fun bindCameraUseCases() {

// Get screen metrics used to setup camera for full screen resolution
val metrics = DisplayMetrics().also { viewFinder.display.getRealMetrics(it) }
Log.d(TAG, "Screen metrics: ${metrics.widthPixels} x ${metrics.heightPixels}")

val screenAspectRatio = aspectRatio(metrics.widthPixels, metrics.heightPixels)
Log.d(TAG, "Preview aspect ratio: $screenAspectRatio")

val rotation = viewFinder.display.rotation

// Bind the CameraProvider to the LifeCycleOwner

val cameraSelector = CameraSelector.Builder().requireLensFacing(lensFacing).build()

val cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext())
cameraProviderFuture.addListener(Runnable {

// CameraProvider
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()

// Preview
preview = Preview.Builder()
// We request aspect ratio but no resolution
.setTargetAspectRatio(screenAspectRatio)
// Set initial target rotation
.setTargetRotation(rotation)
.build()

// ImageCapture
imageCapture = ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
// We request aspect ratio but no resolution to match preview config, but letting
// CameraX optimize for whatever specific resolution best fits 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(rotation)
.build()

// Must unbind the use-cases before rebinding them
cameraProvider.unbindAll()

try {
// A variable number of use-cases can be passed here -
// camera provides access to CameraControl & CameraInfo
camera = cameraProvider.bindToLifecycle(
this, cameraSelector, preview, imageCapture /**, imageAnalyzer*/)

// Attach the viewfinder's surface provider to preview use case
preview?.setSurfaceProvider(viewFinder.createSurfaceProvider(camera?.cameraInfo))
} catch(exc: Exception) {
Log.e(TAG, "Use case binding failed", exc)
}

}, ContextCompat.getMainExecutor(requireContext()))
}

最佳答案

使用当前的 CameraX API,我认为您无法选择要使用的特定相机,您只能选择镜头朝向 ( CameraSelector.Builder().requireLensFacing(int) ),可以是正面或背面。当您绑定(bind)用例时,CameraX 会选择第一个满足用例要求的相机。例如,在将预览用例与启用或禁用扩展绑定(bind)时,可能会使用 2 个不同的后置摄像头。
CameraSelector API 似乎仍有增长空间,因此在 future ,您可能对选择哪个相机有更多的控制权。如果您现在需要这种控制,您可能需要使用 Camera2。

关于Android-CameraX:在多个前置摄像头之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61748470/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com