CameraX: как отобразить предварительный просмотр с соотношением сторон 16: 9? - PullRequest
2 голосов
/ 25 октября 2019

В настоящее время я пытаюсь разработать сканер QRCode с новой библиотекой cameraX от Android Jetpack. Я использую последнюю доступную версию: 1.0.0-alpha06

В примере CameraXBasic (https://github.com/android/camera-samples/tree/master/CameraXBasic), после обновления версии библиотеки cameraX с 1.0.0-alpha05 до 1.0.0-alpha06, япытаюсь отобразить предпросмотр с соотношением 16: 9 вместо полноэкранного.

Чтобы сделать это, я обновил макет fragment_camera.xml, чтобы «принудительно» настроить соотношение с 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>

Предварительный просмотр выглядит хорошо:

enter image description here

Теперь я обновил код до класса CameraFragment, чтобыиспользовать новое перечисление AspectRatio.

Итак, конфигурация предварительного просмотра камеры выглядит следующим образом:

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()

К сожалению, результат выглядит как соотношение 1: 1, а не как 16:9 one:

enter image description here

Если я использую значение AspectRatio.RATIO_4_3 вместо AspectRatio.RATIO_16_9, результат выглядит хорошо:

enter image description here

Это проблема с библиотекой или проблема с моей реализацией, использующей значение AspectRatio.RATIO_16_9?

Заранее спасибо за помощь!

...