Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- Fixed wrongly oriented photos and videos when opening in landscape ([#294])

## [1.5.0] - 2026-01-30
### Added
- Added support for custom fonts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class CameraXPreview(
) : MyPreview, DefaultLifecycleObserver {

companion object {
// Auto focus is 1/6 of the area.
// Autofocus is 1/6 of the area.
private const val AF_SIZE = 1.0f / 6.0f
private const val AE_SIZE = AF_SIZE * 1.5f
private const val CAMERA_MODE_SWITCH_WAIT_TIME = 500L
Expand Down Expand Up @@ -136,11 +136,9 @@ class CameraXPreview(
else -> Surface.ROTATION_0
}

if (lastRotation != rotation) {
preview?.targetRotation = rotation
imageCapture?.targetRotation = rotation
videoCapture?.targetRotation = rotation
lastRotation = rotation
if (targetRotation != rotation) {
targetRotation = rotation
applyCaptureRotation(rotation)
}
}
}
Expand Down Expand Up @@ -178,7 +176,7 @@ class CameraXPreview(
private var cameraSelector = config.lastUsedCameraLens.toCameraSelector()
private var flashMode = FLASH_MODE_OFF
private var isPhotoCapture = initInPhotoMode
private var lastRotation = 0
private var targetRotation: Int? = null
private var lastCameraStartTime = 0L
private var simpleLocationManager: SimpleLocationManager? = null

Expand All @@ -200,7 +198,7 @@ class CameraXPreview(
videoQualityManager.initSupportedQualities(provider)
bindCameraUseCases()
setupCameraObservers()
} catch (e: Exception) {
} catch (_: Exception) {
val errorMessage =
if (switching) R.string.camera_switch_error else R.string.camera_open_error
activity.toast(errorMessage)
Expand Down Expand Up @@ -260,16 +258,22 @@ class CameraXPreview(
)
}
preview = previewUseCase
applyCaptureRotation(targetRotation ?: rotation)
setupZoomAndFocus()
setFlashlightState(config.flashlightState)
}

private fun applyCaptureRotation(rotation: Int) {
imageCapture?.targetRotation = rotation
videoCapture?.targetRotation = rotation
}

private fun buildPreview(resolution: Size, rotation: Int): Preview {
return Preview.Builder()
.setTargetRotation(rotation)
.setResolutionSelector(getResolutionSelector(resolution))
.build().apply {
setSurfaceProvider(previewView.surfaceProvider)
surfaceProvider = previewView.surfaceProvider
}
}

Expand Down Expand Up @@ -307,7 +311,7 @@ class CameraXPreview(
private fun getResolutionSelector(resolution: Size): ResolutionSelector {
return ResolutionSelector.Builder()
.setResolutionStrategy(ResolutionStrategy(resolution, FALLBACK_RULE_CLOSEST_LOWER_THEN_HIGHER))
.setResolutionFilter { supportedSizes, rotationDegrees ->
.setResolutionFilter { supportedSizes, _ ->
// Sort by closest image ratio
supportedSizes.sortedBy {
size -> abs(size.width / size.height.toFloat() - resolution.width / resolution.height.toFloat())
Expand All @@ -317,6 +321,7 @@ class CameraXPreview(
.build()
}


private fun getAllowedResolutionMode(): Int {
return when (config.captureMode) {
CaptureMode.MINIMIZE_LATENCY -> PREFER_CAPTURE_RATE_OVER_HIGHER_RESOLUTION
Expand Down
Loading