Search code examples
javaandroidkotlincameraandroid-camerax

CameraX: setTargetResolution() does not work on some devices


The code below (which works fine on my old Samsung S4) does not work on my Samsung A7 :

val imageAnalysis = ImageAnalysis.Builder()
    .setTargetResolution(Size(176, 144))
    .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
    .build()

imageAnalysis.setAnalyzer(cameraExecutor, { image ->
    Log.i("LOG", "${image.width}x${image.height}")
    image.close()
})

val cameraProvider = cameraProviderList.get()
cameraProvider.unbindAll()
cameraProvider.bindToLifecycle(mainActivity, CameraSelector.DEFAULT_BACK_CAMERA, imageAnalysis)

Indeed, instead of returning a resolution of 176x144, it returns a resolution of 720x720

The resolution 176x144 is however one of the resolutions supported by this device (got from the StreamConfigurationMap.getOutputSizes() method) :

3264x2448
3264x1836
2160x2160
1920x1440
1920x1080
1440x1440
1440x1080
1280x720
1088x1088
1056x704
1024x768
960x720
800x450
720x720
720x480
640x480
480x320
352x288
320x240
256x144
**176x144**

If I don't set a target resolution, the returned resolution is 640x480 (which is in accordance with the documentation).

Do you see anything that would explain this problem?


Solution

  • Replacing

    .setTargetResolution(Size(176, 144))
    

    with

    .setTargetResolution(Size(144, 176))
    

    fixed the problem.