Search code examples
androidkotlinandroid-camerax

How to setImageCaptureResolutionSelector() in CameraController?


How do I set resolution, setImageCaptureResolutionSelector()? I am using CameraController but getting very confused between its documentation and ProcessCameraProvider documentation. Here is my code:

     val controller = remember {
            LifecycleCameraController(applicationContext).apply {
                setEnabledUseCases(CameraController.IMAGE_CAPTURE or CameraController.VIDEO_CAPTURE)
                setImageCaptureMode(CAPTURE_MODE_MAXIMIZE_QUALITY)
                //setImageCaptureResolutionSelector() // is this where it goes? what value gets passed to it? - struggling to find examples.
            }
        }

I've seen that there is a ResolutionStrategy.HIGHEST_AVAILABLE_STRATEGY which allows defaulting to the highest camera resolution. How do I use this? Should I be doing something like this and if so how do I apply it?

ResolutionSelector.Builder().setResolutionStrategy(ResolutionStrategy.HIGHEST_AVAILABLE_STRATEGY).build()

*** UPDATE ***

This functions without errors:

        val resolutionSelector = ResolutionSelector.Builder().setResolutionStrategy(ResolutionStrategy.HIGHEST_AVAILABLE_STRATEGY).build()

        val controller = remember {
            LifecycleCameraController(applicationContext).apply {
                setEnabledUseCases(CameraController.IMAGE_CAPTURE or CameraController.VIDEO_CAPTURE)
                setImageCaptureMode(CAPTURE_MODE_MAXIMIZE_QUALITY)
                setImageCaptureResolutionSelector(resolutionSelector)
            }
        }

When I query the ResolutionSelector like so:

        val res = controller.getImageCaptureResolutionSelector()
        Log.w("Resolution", "*** RESOLUTION ***: " + res.toString())

I get:

*** RESOLUTION ***: androidx.camera.core.resolutionselector.ResolutionSelector@7b11fc9

...no details about what resolution has been set and the resolution is not changing in any way - the images created are 1088x1440 (my camera goes to 3120x4160). Why is this not working?


Solution

  • This is how I resolved the issue:

    class MainActivity : ComponentActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            if(!hasRequiredPermissions()) {
                ActivityCompat.requestPermissions(
                    this, CAMERAX_PERMISSIONS, 0
                )
            }
    
            setContent {
                CameraApp1Theme {
                    val scope = rememberCoroutineScope()
                    val scaffoldState = rememberBottomSheetScaffoldState()
    
                    // set a size variable
                    val screenSize = Size(3120, 4160)
                    
                    // build a resolutionselector using that size
                    val resolutionSelector = ResolutionSelector.Builder().setResolutionStrategy(ResolutionStrategy(screenSize, ResolutionStrategy.FALLBACK_RULE_CLOSEST_HIGHER_THEN_LOWER)).build()
    
                    val controller = remember {
                        LifecycleCameraController(applicationContext).apply {
                            // The 'or' here seemed to be the issue
                            //setEnabledUseCases(CameraController.IMAGE_CAPTURE or CameraController.VIDEO_CAPTURE)
                            setEnabledUseCases(CameraController.IMAGE_CAPTURE)
                            setImageCaptureMode(CAPTURE_MODE_MAXIMIZE_QUALITY)
                            setImageCaptureResolutionSelector(resolutionSelector)
                        }
                    }
    
                 }
            }
        }
    }
    

    The 'or' and setting two enabled use cases at the same time seemed to be the issue. I have seem comments on forums that setting both at once can sometimes not work and there is a workaround to set one and then another if you need image and video. I don't at the moment so I just set mine to image.

    EDIT: I've blocked out where I've put the code in the mainactivity class. This should clarify where you need to add it to your own project.