如何在Fotoapparat Camera中自定义设置pictureResolution?
Hey there! I’ve worked with Fotoapparat before, so let me walk you through how to set a custom picture resolution—you’re not limited to just the highestResolution() and lowestResolution() shortcuts.
Step 1: Use the resolution() configuration method
The library lets you define a custom resolution by using the resolution() method in your camera configuration, which accepts a Size object with your desired width and height.
Step 2: Pick a supported resolution (critical for compatibility)
Not all resolutions work with every camera hardware, so it’s smart to first fetch the list of resolutions your device’s camera supports, then pick from that list. Here’s a code example in Kotlin:
// First, get all resolutions supported by your target camera (back/front) val supportedResolutions = Fotoapparat.getAvailableResolutions( context = yourAppContext, lensPosition = back() // swap with front() if using front camera ) // Choose a resolution from the list, or define one if you know it's supported val desiredResolution = Size(1920, 1080) // Example: 1080p resolution // Configure Fotoapparat with your custom resolution val fotoapparat = Fotoapparat( context = yourAppContext, view = yourCameraView, lensPosition = back(), cameraConfiguration = CameraConfiguration().copy( pictureResolution = resolution(desiredResolution) ) )
Step 3: Handle fallback cases
If the resolution you specify isn’t supported, Fotoapparat will automatically fall back to the closest matching supported resolution. To avoid surprises, you can even find the closest match to your target resolution programmatically:
val targetWidth = 2560 val targetHeight = 1440 // 2K target val closestResolution = supportedResolutions.minByOrNull { // Calculate the difference between target and candidate resolution Math.abs(it.width - targetWidth) + Math.abs(it.height - targetHeight) } ?: highestResolution() // Fallback to highest if no match found // Use this closest resolution in your camera config
That’s all you need! Now you can set the exact picture resolution you need instead of relying on the default high/low options.
内容的提问来源于stack exchange,提问作者Jay Patel

