Disclaimer: I'm aware of the existence of this question, but it currently stands unresolved and I'm trying to provide extra information without polluting that one with useless answers that won't solve the problem anyway.
I have a custom device with a front camera that is mirrored by default, so I want to display the preview normally and I need to horizontally flip the content of PreviewView, but I'm stuck. Other people in the past have suggested using PreviewView#setScaleX(-1)
but it either doesn't work at all or it needs to be called at a very specific point in the code, which I haven't found yet.
The code below is a simplified version of CameraFragment.kt
in the official CameraXBasic example; I've added comments where I've already tired calling viewFinder.scaleX = -1f
with no success. Honestly I don't really think that the place makes a difference because if I call it with any value other than 1 it works fine with both scaleX
and scaleY
, but it always ignores the negative sign so it never flips.
private lateinit var viewFinder: PreviewView
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
...
viewFinder = view.findViewById(R.id.view_finder)
// HERE
viewFinder.post {
// HERE
setupCamera()
}
}
private fun setupCamera() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext())
cameraProviderFuture.addListener(Runnable {
val cameraProvider = cameraProviderFuture.get()
val cameraSelector = CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_FRONT)
.build()
val preview = Preview.Builder()
.build()
.also {
// HERE
it.setSurfaceProvider(viewFinder.surfaceProvider)
}
cameraProvider.unbindAll()
try {
cameraProvider.bindToLifecycle(this, cameraSelector, preview)
// HERE
} catch (exc: Exception) {
Log.e(TAG, "Use case binding failed", exc)
}
// HERE
}, ContextCompat.getMainExecutor(requireContext()))
// HERE
}