我试图通过cameraX实现获得高分辨率图像。通过下面的代码,我可以看到对于设备的后置摄像头,我测试了分辨率为4000x3000的条目。
@RequiresApi(Build.VERSION_CODES.M)
fun getPossibleOutputSizes(id: String) {
val cameraManager = context?.getSystemService(Context.CAMERA_SERVICE) as CameraManager
val sizes = cameraManager?.getCameraCharacteristics(id)?.get(SCALER_STREAM_CONFIGURATION_MAP)?.getHighResolutionOutputSizes(ImageFormat.JPEG)
if (sizes != null) {
println("got possible resolutions:")
println(sizes.joinToString("\n"))
}
}
下面是设置相机的代码。这是一个示例应用程序,我用来检查由cameraX takePhoto调用提供的输出分辨率。
/** Declare and bind preview, capture and analysis use cases */
@SuppressLint("RestrictedApi", "ServiceCast", "UnsafeOptInUsageError")
private fun bindCameraUseCases() {
// Get screen metrics used to setup camera for full screen resolution
val metrics = windowManager.getCurrentWindowMetrics().bounds
Log.d(TAG, "Screen metrics: ${metrics.width()} x ${metrics.height()}")
val screenAspectRatio = aspectRatio(metrics.width(), metrics.height())
Log.d(TAG, "Preview aspect ratio: $screenAspectRatio")
val rotation = fragmentCameraBinding.viewFinder.display.rotation
// CameraProvider
val cameraProvider = cameraProvider
?: throw IllegalStateException("Camera initialization failed.")
// CameraSelector
var cameraSelector = CameraSelector.Builder().requireLensFacing(lensFacing).build()
selectExternalOrBestCamera(cameraProvider)?.let { returnedCameraSelector ->
cameraSelector = returnedCameraSelector
}
// Preview
preview = Preview.Builder()
// We request aspect ratio but no resolution
.setTargetAspectRatio(screenAspectRatio)//either RATIO_4_3 or RATIO_16_9
// Set initial target rotation
.setTargetRotation(rotation)
.build()
// ImageCapture
imageCapture = ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_ZERO_SHUTTER_LAG)
// We request aspect ratio but no resolution to match preview config, but letting
// CameraX optimize for whatever specific resolution best fits our use cases
// Set initial target rotation, we will have to call this again if rotation changes
// during the lifecycle of this use case
//.setTargetAspectRatio(AspectRatio.RATIO_4_3)//tried both RATIO_4_3 and RATIO_16_9
.setTargetResolution(Size(3000, 4000))//tried 4000 * 3000 too..
.setTargetRotation(rotation)
//.setDefaultCaptureConfig(config)
.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)
// Attach the viewfinder's surface provider to preview use case
preview?.setSurfaceProvider(fragmentCameraBinding.viewFinder.surfaceProvider)
observeCameraState(camera?.cameraInfo!!)
} catch (exc: Exception) {
Log.e(TAG, "Use case binding failed", exc)
}
}
我尝试使用cameraX中的可用配置,但无法获得与我在
getPossibleOutputSizes
中获得的相同分辨率的高分辨率输出。我测试的所有设备似乎都是这种情况。但设备相机能够提供该分辨率的图像,基于camera2的实现也是如此。
这是cameraX的限制还是我错过了什么?
不幸的是,截至2023年1月,使用
getHighResolutionOutputSizes
返回的高分辨率无法与CameraX一起使用。我不知道有什么解决方法。错误报告和当前状态可以在这里找到:https://issuetracker.google.com/issues/162121234
目前预计该功能应该在 CameraX 1.3 版本中附带。
编辑 20.04.23: 随着昨天发布的版本 1.3.0-alpha06 ,现在应该可用:https://developer.android.com/jetpack/androidx/releases/camera#1.3.0-alpha06
从 Camerax 1.3.0 开始,您现在可以使用
setAllowedResolutionMode
类的 ResolutionSelector.Builder
。示例:
val resolutionSelector = ResolutionSelector.Builder()
.setAllowedResolutionMode(ResolutionSelector.PREFER_HIGHER_RESOLUTION_OVER_CAPTURE_RATE)
.setAspectRatioStrategy(AspectRatioStrategy.RATIO_4_3_FALLBACK_AUTO_STRATEGY)
.setResolutionStrategy(ResolutionStrategy.HIGHEST_AVAILABLE_STRATEGY)
.build()
然后使用:
imageCapture = ImageCapture.Builder()
.setTargetRotation(viewBinding.viewFinder.display.rotation)
.setResolutionSelector(resolutionSelector)
.build()
这应该可以在文档中提供高分辨率捕获:
自 Android 12 起,某些设备可能支持最大分辨率 传感器像素模式,这使他们能够捕捉额外的超高 决议检索自 android.hardware.camera2.CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP_MAXIMUM_RESOLUTION 。该模式不允许应用程序选择那些超高 决议。
更多信息请访问此处