Вот мой файл макета:
Код: Выделить всё
Код: Выделить всё
class CameraPreviewFragment : Fragment() {
private lateinit var binding: CameraPreviewFragmentBinding
private lateinit var viewModel: CameraPreviewViewModel
private val previewConfig = PreviewConfig.Builder().build()
private val preview = Preview(previewConfig)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = CameraPreviewFragmentBinding.inflate(inflater, container, false)
binding.lifecycleOwner = this
val viewModelFactory = CameraPreviewViewModelFactory((activity as MainActivity).faceIDUtils)
viewModel = ViewModelProviders.of(this, viewModelFactory).get(CameraPreviewViewModel::class.java)
binding.viewModel = viewModel
startCamera()
return binding.root
}
private fun startCamera() {
preview.setOnPreviewOutputUpdateListener {
val parent = binding.cameraPreview.parent as ViewGroup
parent.removeView(binding.cameraPreview)
parent.addView(binding.cameraPreview, 0)
binding.cameraPreview.surfaceTexture = it.surfaceTexture
updateTransform()
}
CameraX.bindToLifecycle(this, preview, viewModel.analyzerUseCase)
}
private fun updateTransform() {
val matrix = Matrix()
val centerX = binding.cameraPreview.width / 2f
val centerY = binding.cameraPreview.height / 2f
val rotationDegrees = when(binding.cameraPreview.display.rotation) {
Surface.ROTATION_0 -> 0
Surface.ROTATION_90 -> 90
Surface.ROTATION_180 -> 180
Surface.ROTATION_270 -> 270
else -> return
}
matrix.postRotate(-rotationDegrees.toFloat(), centerX, centerY)
binding.cameraPreview.setTransform(matrix)
}
}
Вот ошибка: java.lang.IllegalStateException: привязка.cameraPreview.display не должна быть нулевой
Подробнее здесь: https://stackoverflow.com/questions/572 ... turns-null