我正在尝试编写我的第一个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()))
}发布于 2020-05-13 00:56:38
使用当前的CameraX API,我不认为您可以选择使用哪个特定的相机,您只能选择镜头面对(CameraSelector.Builder().requireLensFacing(int),它可以是正面或背面。当您绑定用例时,CameraX会选择满足用例需求的第一个摄像机。例如,当将预览用例绑定到已启用和禁用的扩展时,可能会使用两个不同的背靠背摄像机。
CameraSelector API似乎仍然有发展的空间,所以在未来,您可能对选择哪个摄像机有更多的控制。如果您现在需要这种控制,您可能需要使用Camera2。
发布于 2022-09-21 12:28:38
你可以通过设置cameraSelector来改变相机。例如,要用index=2设置相机,请使用以下代码。
List<CameraInfo> availableCameraInfos = cameraProvider.getAvailableCameraInfos();
Log.i(TAG, "[startCamera] available cameras:"+availableCameraInfos);
// Select back camera as a default
//CameraSelector cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA;
CameraSelector cameraSelector = availableCameraInfos.get(2).getCameraSelector();发布于 2020-05-29 15:19:24
我也有类似的问题,但后面的摄像头。我检查了一些设备,发现在所有测试的设备上,我可以选择相机id并获得正确的照相机:
因此,我创建了一个自定义的CameraFilter,在其中筛选相机id "0":
class CameraIdFilter(private val cameraId: String) : CameraFilter {
override fun filterCameras(cameras: Set<CameraInternal>): Set<CameraInternal> {
val resultCameras: MutableSet<CameraInternal> = LinkedHashSet()
for (camera in cameras) {
val cameraId = camera.cameraInfoInternal.cameraId
if (cameraId == this.cameraId) {
resultCameras.add(camera)
}
}
return resultCameras
}}
并将过滤器附加到相机选择器:
val cameraSelectorBuilder = CameraSelector.Builder()
.requireLensFacing(lensFacing)
.appendFilter(CameraIdFilter("0"))我不建议在生产应用程序中使用这个,因为我确信有些设备不遵循这种约定/模式。但是,您可以在内部应用程序中使用它,在该应用程序中,您可以知道用户使用的设备。
https://stackoverflow.com/questions/61748470
复制相似问题