我很高兴在我的应用程序上使用 CameraX。我在 github 上参考了谷歌官方的 CameraX,但是出了点问题。请帮助我。,
这是我设置纵横比的代码
private fun aspectRatio(width: Int, height: Int): Int {
val previewRatio = max(width, height).toDouble() / min(width, height)
if (abs(previewRatio - RATIO_4_3_VALUE) <= abs(previewRatio - RATIO_16_9_VALUE)) {
return AspectRatio.RATIO_4_3
}
return AspectRatio.RATIO_16_9
}
这是我的预览代码
@SuppressLint("UnsafeExperimentalUsageError")private fun startCamera() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
viewFinder=binding.surfaceView
cameraProviderFuture.addListener({
// Used to bind the lifecycle of cameras to the lifecycle owner
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
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")
Size(metrics.widthPixels, metrics.heightPixels)
val rotation = viewFinder.display.rotation
// Preview
val preview = Preview.Builder()
.setTargetRotation(rotation)
.setTargetAspectRatio(screenAspectRatio)
.build()
imageCapture = if (flashStatus) {
ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY)
.setTargetRotation(rotationimage)
.setTargetAspectRatio(screenAspectRatio)
.build()
} else {
ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY)
.setTargetRotation(rotationimage)
.build()
}
// Select back camera as a default
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
// Unbind use cases before rebinding
cameraProvider.unbindAll()
viewFinder.scaleType=PreviewView.ScaleType.FILL_CENTER
try {
// Bind use cases to camera
val cam = cameraProvider.bindToLifecycle(
this, cameraSelector, preview, imageCapture)
if (!flashStatus) {
if (cam.cameraInfo.hasFlashUnit()) {
cam.cameraControl.enableTorch(true)
}
} else {
cam.cameraControl.enableTorch(false)
}
// Attach the viewfinder's surface provider to preview use case
preview.setSurfaceProvider(viewFinder.surfaceProvider)
} catch (exc: Exception) {
Log.e(TAG, "Use case binding failed", exc)
}
}, ContextCompat.getMainExecutor(this))
}
并且此输出预览 输出预览被拉伸
我的代码有什么问题?
谢谢你的帮助我伙计们。,
更新 当我的设备处于黑暗模式时,预览是正常的。 在黑暗模式下预览正常 如何在正常模式下修复?