0

I am relatively new to developing on android and I am trying to implement a camera feature. I am using this page as a guide https://www.journaldev.com/30132/android-camerax-overview.

The section of code below is what I am having trouble with.

Rational aspectRatio = new Rational(textureView.getWidth(), textureView.getHeight());
Size screen = new Size(textureView.getWidth(), textureView.getHeight()); //size of the screen

PreviewConfig pConfig = new PreviewConfig.Builder().setTargetAspectRatio(aspectRatio).setTargetResolution(screen).build();

When a Rational is passed into setTargetAspectRatio, Android Studio gives the error 'Rational cannot be converted into AspectRatio.

According to the android developer page setTargetAspectRatio accepts the object AspectRatio, so I tried this:

AspectRatio aspectRatio = new AspectRatio(textureView.getWidth(), textureView.getHeight());

However I then get the error, on this line saying 'AspectRatio() has private access in androidx.camera.core.AspectRatio'. I am not sure what this error means as I have imported androidx.camera.core.AspectRatio normally.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

Seems like the tutorial you're following is using a pretty old version (alpha02) of the camerax library. The current version is beta01, and many things have changed between these versions.

'AspectRatio() has private access in androidx.camera.core.AspectRatio'. I am not sure what this error means as I have imported androidx.camera.core.AspectRatio normally.

AspectRatio does indeed have a private constructor at the moment (and since a couple of versions ago), this restriction is to push you to only use one of the supported ratios RATIO_4_3 or RATIO_16_9, meaning that configuring the preview's aspect ratio looks like this:

final Preview preview = new Preview.Builder()
        .setTargetAspectRatio(RATIO_16_9)
        .build()

Note that if you're only looking to add a basic view finder in your app, it is recommended to use cameraX's PreviewView, it makes setting up a camera preview easier, you can checkout how to do this from the official cameraX docs.

Husayn Hakeem
  • 4,184
  • 1
  • 16
  • 31