在录制时无法切换CameraX镜头的朝向。使用jetpack CameraX库甚至可以做到这一点吗?
在不录制时我可以切换镜头朝向,但是然后我必须取消绑定所有()并重新初始化,所以在录制时这将不起作用。
override fun onCreate(savedInstanceState: Bundle?) {
iv_switch_lens?.setOnClickListener {
lensFacing = if (CameraX.LensFacing.FRONT == lensFacing) {
CameraX.LensFacing.BACK
} else {
CameraX.LensFacing.FRONT
}
try {
CameraX.getCameraWithLensFacing(lensFacing)
CameraX.unbindAll()
startCamera()
} catch (exc: Exception) {
}
}
}
private fun startCamera() {
val metrics = DisplayMetrics().also { viewFinder.display.getRealMetrics(it) }
val screenAspectRatio = Rational(metrics.widthPixels, metrics.heightPixels)
Log.d(TAG, "Screen metrics: ${metrics.widthPixels} x ${metrics.heightPixels}")
val viewFinderConfig = PreviewConfig.Builder().apply {
setLensFacing(lensFacing)
setTargetAspectRatio(screenAspectRatio)
setTargetRotation(viewFinder.display.rotation)
}.build()
preview = AutoFitPreviewBuilder.build(viewFinderConfig, viewFinder)
val videoCaptureConfig = VideoCaptureConfig.Builder().apply {
setTargetAspectRatio(screenAspectRatio)
setLensFacing(lensFacing)
setTargetRotation(viewFinder.display.rotation)
}.build()
videoCapture = VideoCapture(videoCaptureConfig)
fab_take_action.setSafeOnClickListener {
cameraViewModel.onActionClicked()
}
CameraX.bindToLifecycle(
this, preview, videoCapture)
}