feat: add capture-specific rotation and reverse portrait support

This commit is contained in:
Hinnka committed 2026-05-03 19:44:08 +08:00
1 parent 2c5d8aa1bd
commit 0d964f6b46
2 files changed
+21 -5

No files matched your search

@@ -28,6 +28,10 @@ object OrientationObserver {
var rotationDegrees by mutableStateOf(0f)
private set
// 存储拍摄方向,包含反向竖屏,用于照片保存/处理
var captureRotationDegrees by mutableStateOf(0f)
private set
// 更新方向,只在横竖屏切换时才更新状态
fun updateOrientation(orientation: Int) {
if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
@@ -41,6 +45,7 @@ object OrientationObserver {
// 右侧朝上(手机顺时针旋转90°)
when (orientation) {
in 45..135 -> {
captureRotationDegrees = 90f
if (!isLandscape || rotationDegrees != 90f) {
isLandscape = true
rotationDegrees = 90f
@@ -49,14 +54,25 @@ object OrientationObserver {
}
// 左侧朝上(手机逆时针旋转90°)
in 225..315 -> {
captureRotationDegrees = 270f
if (!isLandscape || rotationDegrees != 270f) {
isLandscape = true
rotationDegrees = 270f
PLog.d(TAG, "Orientation locked to landscape-left, orientation=$orientation")
}
}
// 反向竖屏
in 135..225 -> {
captureRotationDegrees = 180f
if (isLandscape || rotationDegrees != 0f) {
isLandscape = false
rotationDegrees = 0f
PLog.d(TAG, "Orientation locked to reverse-portrait, orientation=$orientation")
}
}
// 竖屏
else -> {
captureRotationDegrees = 0f
if (isLandscape || rotationDegrees != 0f) {
isLandscape = false
rotationDegrees = 0f
@@ -844,7 +844,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
val sensorOrientation = cameraController.getSensorOrientation()
val lensFacing = cameraController.getLensFacing()
val deviceRotation = OrientationObserver.rotationDegrees.toInt()
val deviceRotation = OrientationObserver.captureRotationDegrees.toInt()
val baseRotation = if (lensFacing == CameraCharacteristics.LENS_FACING_FRONT) {
(sensorOrientation - deviceRotation + 360) % 360
} else {
@@ -1838,7 +1838,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
}
private fun capturePreviewThumbnailRotation(): Float {
val deviceRotation = OrientationObserver.rotationDegrees.toInt()
val deviceRotation = OrientationObserver.captureRotationDegrees.toInt()
val lensFacing = cameraController.getLensFacing()
val baseRotation = if (lensFacing == CameraCharacteristics.LENS_FACING_FRONT) {
(360 - deviceRotation) % 360
@@ -2516,7 +2516,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
// 计算旋转角度
val sensorOrientation = cameraController.getSensorOrientation()
val lensFacing = cameraController.getLensFacing()
val deviceRotation = OrientationObserver.rotationDegrees.toInt()
val deviceRotation = OrientationObserver.captureRotationDegrees.toInt()
// 基础旋转角度计算
val baseRotation = if (lensFacing == CameraCharacteristics.LENS_FACING_FRONT) {
@@ -2779,7 +2779,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
// 计算旋转角度
val sensorOrientation = cameraController.getSensorOrientation()
val lensFacing = cameraController.getLensFacing()
val deviceRotation = OrientationObserver.rotationDegrees.toInt()
val deviceRotation = OrientationObserver.captureRotationDegrees.toInt()
// 基础旋转角度计算
val baseRotation = if (lensFacing == CameraCharacteristics.LENS_FACING_FRONT) {
@@ -2933,7 +2933,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
// 计算旋转角度
val sensorOrientation = cameraController.getSensorOrientation()
val lensFacing = cameraController.getLensFacing()
val deviceRotation = OrientationObserver.rotationDegrees.toInt()
val deviceRotation = OrientationObserver.captureRotationDegrees.toInt()
// 基础旋转角度计算
val baseRotation = if (lensFacing == CameraCharacteristics.LENS_FACING_FRONT) {