Search code examples
androidandroid-jetpackandroid-camerax

CameraX : how to display a 16:9 ratio preview?


I am currently trying to develop a QRCode scanner with the new cameraX livrary from Android Jetpack. I use the latest available version : 1.0.0-alpha06

In the CameraXBasic sample (https://github.com/android/camera-samples/tree/master/CameraXBasic), after updated the cameraX library version from 1.0.0-alpha05 to 1.0.0-alpha06, I am trying to display a 16:9 ratio preview instead of the fullscreen one.

In order to do that, I have updated the fragment_camera.xml layout in order to "force" the ratio aspect of the TextureView :

<androidx.constraintlayout.widget.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/camera_container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@android:color/black"
  >
    <TextureView
      android:id="@+id/view_finder"
      android:layout_width="0dp"
      android:layout_height="0dp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintDimensionRatio="16:9"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      />
</androidx.constraintlayout.widget.ConstraintLayout>

The preview looks good :

enter image description here

Now, I have updated the code into the CameraFragment class in order to use the new AspectRatio enum.

So now, the camera preview config looks like :

val viewFinderConfig = PreviewConfig.Builder().apply {
  setLensFacing(lensFacing)
  // We request aspect ratio but no resolution to let CameraX optimize our use cases
  setTargetAspectRatio(AspectRatio.RATIO_16_9)
  // Set initial target rotation, we will have to call this again if rotation changes
  // during the lifecycle of this use case
  setTargetRotation(viewFinder.display.rotation)
}.build()

Unfortunately, the result looks like a 1:1 ratio and not a 16:9 one :

enter image description here

if I use AspectRatio.RATIO_4_3 value instead of the AspectRatio.RATIO_16_9 the result looks good :

enter image description here

Is it an issue with the library or an issue with my implementation using the AspectRatio.RATIO_16_9 value ?

Thank you in advance for your help!


Solution

  • The issue was into the library directly. The latest alpha (alpha-08) does not have the issue anymore.