Add configurable high-resolution capture option

Add useHighResCapture toggle to CameraManager that switches between
CameraX default resolution and HIGHEST_AVAILABLE_STRATEGY. Default
is off to avoid OOM from processing very large bitmaps (e.g. 50MP).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Ole-Morten Duesund 2026-02-27 15:38:33 +01:00
commit 5cba2fefc9

View file

@ -12,6 +12,7 @@ import androidx.camera.core.Preview
import androidx.camera.core.SurfaceRequest import androidx.camera.core.SurfaceRequest
import androidx.camera.core.resolutionselector.AspectRatioStrategy import androidx.camera.core.resolutionselector.AspectRatioStrategy
import androidx.camera.core.resolutionselector.ResolutionSelector import androidx.camera.core.resolutionselector.ResolutionSelector
import androidx.camera.core.resolutionselector.ResolutionStrategy
import androidx.camera.lifecycle.ProcessCameraProvider import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
@ -37,6 +38,20 @@ class CameraManager(private val context: Context) {
val lensController = LensController() val lensController = LensController()
private val _useHighResCapture = MutableStateFlow(false)
val useHighResCapture: StateFlow<Boolean> = _useHighResCapture.asStateFlow()
/**
* Toggles between CameraX default resolution and highest available.
* Rebinds camera use cases to apply the change.
*/
fun setHighResCapture(enabled: Boolean) {
if (_useHighResCapture.value != enabled) {
_useHighResCapture.value = enabled
lifecycleOwnerRef?.let { bindCameraUseCases(it) }
}
}
private val _error = MutableStateFlow<String?>(null) private val _error = MutableStateFlow<String?>(null)
val error: StateFlow<String?> = _error.asStateFlow() val error: StateFlow<String?> = _error.asStateFlow()
@ -94,10 +109,18 @@ class CameraManager(private val context: Context) {
.setResolutionSelector(resolutionSelector) .setResolutionSelector(resolutionSelector)
.build() .build()
// Image capture use case for high-res photos // Image capture use case
imageCapture = ImageCapture.Builder() val captureBuilder = ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY) .setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY)
.build()
if (_useHighResCapture.value) {
val captureResolutionSelector = ResolutionSelector.Builder()
.setResolutionStrategy(ResolutionStrategy.HIGHEST_AVAILABLE_STRATEGY)
.build()
captureBuilder.setResolutionSelector(captureResolutionSelector)
}
imageCapture = captureBuilder.build()
// Select camera based on front/back preference // Select camera based on front/back preference
val cameraSelector = if (_isFrontCamera.value) { val cameraSelector = if (_isFrontCamera.value) {