Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get available resolutions using CameraX on Android?

Is there a way to query the available camera resolutions using CameraX? With Camera2 this is possible using StreamConfigurationMap.getOutputSizes(). However, I can't find a way to do this using CameraX.

It doesn't help that the documentation is quite outdated. Currently it references version 1.0.0-alpha06 and many APIs have changed in the latest 1.0.0-beta01.

EDIT:

There is a way to get the available resolutions using Camera2 APIs (thanks to Wasim's answer below). However, that's only possible after the camera is bound to the lifecycle and therefore the target resolution cannot be changed anymore, which makes it quite useless.

Sure I can specify the target resolution without knowing the available ones but this way I have no control over the resulted aspect ratio. In my case, I end up with a 16:9 Preview and a 4:3 ImageAnalysis although the targetResolution for my ImageAnalysis is in 16:9 (224x126).

For the record, this is how you could get the output sizes:

val camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageAnalyzer)
val cameraId = Camera2CameraInfo.extractCameraId(camera.cameraInfo)
val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
val characteristics = cameraManager.getCameraCharacteristics(cameraId)
val streamConfigurationMap = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
val outputSizes = streamConfigurationMap.getOutputSizes(format)

Still looking for an actual solution. Thanks in advance.

like image 609
Georgios Avatar asked Mar 28 '20 13:03

Georgios


3 Answers

This could help anyone googling here.

Android CameraX, get camera characteristics such as CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE.

For 2021 the syntax is like ...

So, you've gone through the process of getting the camera ...

theCamera = cameraProvider.bindToLifecycle((LifecycleOwner)this,
              yourCameraSelector, yourImageAnalysis, yourPreview);

the code nowadays is:

CameraCharacteristics camChars = Camera2CameraInfo
   .extractCameraCharacteristics(theCamera.getCameraInfo());
float discoveredMinFocusDistance = camChars
   .get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE);

You'll probably need a

@SuppressLint("UnsafeExperimentalUsageError")
CameraCharacteristics camChars =  ...

for that first line.

like image 153
Fattie Avatar answered Nov 18 '22 21:11

Fattie


This worked for me:

val camera = cameraProvider.bindToLifecycle(mainActivity, cameraSelector, CameraManager.imageAnalysis)

val cameraId = Camera2CameraInfo.from(camera.cameraInfo).cameraId
val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
val characteristics = cameraManager.getCameraCharacteristics(cameraId)
val configs: StreamConfigurationMap? = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)

val imageAnalysisSizes = configs?.getOutputSizes(ImageFormat.YUV_420_888)
imageAnalysisSizes?.forEach {
    Log.i("LOG", "Image capturing YUV_420_888 available output size: $it")
}

val previewSizes = configs?.getOutputSizes(SurfaceTexture::class.java)
previewSizes?.forEach {
    Log.i("LOG", "Preview available output size: $it")
}
like image 44
Denis Avatar answered Nov 18 '22 20:11

Denis


Because RecommendedStreamConfigurationMap is not compatable for older platform, so I find below code from camerax

val camera = cameraProvider.bindToLifecycle(...)
val characteristics = CameraCharacteristicsCompat.toCameraCharacteristicsCompat(Camera2CameraInfo.extractCameraCharacteristics(camera.cameraInfo))
val previewSizes = CamcorderProfileResolutionQuirk(characteristics).supportedResolutions

it works for me, copied from camerax CamcorderProfileResolutionQuirk.java

like image 1
KittenBall Avatar answered Nov 18 '22 19:11

KittenBall