feat: font camera mirror

This commit is contained in:
Hinnka committed 2026-03-01 13:29:14 +08:00
1 parent 7b41eddee2
commit bbf9ff7010
9 files changed
+96 -10

No files matched your search

@@ -2470,8 +2470,8 @@ class Camera2Controller(private val context: Context) {
*/
fun stopBurstCapture() {
PLog.d(TAG, "Stop Burst Capture")
_state.value = _state.value.copy(burstCapturing = false, isCapturing = false)
captureSession?.abortCaptures()
resetPreviewAfterCapture()
_state.value = _state.value.copy(burstCapturing = false, isCapturing = false)
}
}
@@ -70,6 +70,7 @@ data class UserPreferences(
val phantomMode: Boolean = false,
val phantomButtonHidden: Boolean = false,
val launchCameraOnPhantomMode: Boolean = false,
val mirrorFrontCamera: Boolean = true,
)
/**
@@ -126,6 +127,7 @@ class UserPreferencesRepository(private val context: Context) {
private val PHANTOM_MODE = booleanPreferencesKey("phantom_mode")
private val PHANTOM_BUTTON_HIDDEN = booleanPreferencesKey("phantom_button_hidden")
private val LAUNCH_CAMERA_ON_PHANTOM_MODE = booleanPreferencesKey("launch_camera_on_phantom_mode")
private val MIRROR_FRONT_CAMERA = booleanPreferencesKey("mirror_front_camera")
}
/**
@@ -176,6 +178,7 @@ class UserPreferencesRepository(private val context: Context) {
phantomMode = preferences[PHANTOM_MODE] ?: false,
phantomButtonHidden = preferences[PHANTOM_BUTTON_HIDDEN] ?: false,
launchCameraOnPhantomMode = preferences[LAUNCH_CAMERA_ON_PHANTOM_MODE] ?: false,
mirrorFrontCamera = preferences[MIRROR_FRONT_CAMERA] ?: true,
)
}
@@ -586,4 +589,13 @@ class UserPreferencesRepository(private val context: Context) {
preferences[LAUNCH_CAMERA_ON_PHANTOM_MODE] = launch
}
}
/**
* 保存是否启用自拍镜像
*/
suspend fun saveMirrorFrontCamera(enabled: Boolean) {
context.dataStore.edit { preferences ->
preferences[MIRROR_FRONT_CAMERA] = enabled
}
}
}
@@ -527,7 +527,7 @@ object PhotoManager {
val metadata = loadMetadata(context, photoId) ?: return@withContext
// 创建预览用的 Bitmap
val previewBitmap = createBitmap(metadata.width, metadata.height)
var previewBitmap = createBitmap(metadata.width, metadata.height)
PLog.d(TAG, "saveYuvPhoto: ${metadata.width} ${metadata.height}")
@@ -538,6 +538,11 @@ object PhotoManager {
yuvFile.absolutePath, previewBitmap
)
}
if (metadata.isMirrored) {
previewBitmap = BitmapUtils.flipHorizontal(previewBitmap)
}
if (success) {
FileOutputStream(tempFile).use { outputStream ->
previewBitmap.compress(Bitmap.CompressFormat.JPEG, photoQuality, outputStream)
@@ -609,7 +614,7 @@ object PhotoManager {
exportDng(context, photoId, array, metadata)
}
val bitmap = RawDemosaicProcessor.getInstance().process(
var bitmap = RawDemosaicProcessor.getInstance().process(
context,
dngFile.absolutePath,
aspectRatio,
@@ -619,6 +624,11 @@ object PhotoManager {
sharpeningValue = 0.4f,
droMode = droMode
) ?: return@withContext
if (metadata.isMirrored) {
bitmap = BitmapUtils.flipHorizontal(bitmap)
}
FileOutputStream(tempFile).use { outputStream ->
bitmap.compress(Bitmap.CompressFormat.JPEG, photoQuality, outputStream)
}
@@ -727,13 +737,14 @@ object PhotoManager {
) = withContext(Dispatchers.IO) {
try {
val photoDir = getPhotoDir(context, photoId)
val metadata = loadMetadata(context, photoId) ?: return@withContext
// 预先准备所有文件路径
val photoFile = File(photoDir, PHOTO_FILE)
val tempFile = File(photoDir, "temp.jpg")
val yuvFile = File(photoDir, YUV_FILE)
val result = MultiFrameStacker.processBurst(
var result = MultiFrameStacker.processBurst(
images,
rotation,
aspectRatio,
@@ -741,6 +752,11 @@ object PhotoManager {
useSuperResolution,
useGpuAcceleration
) ?: return@withContext
if (metadata.isMirrored) {
result = BitmapUtils.flipHorizontal(result)
}
// Save Original (Stacked Result)
FileOutputStream(tempFile).use { outputStream ->
result.compress(Bitmap.CompressFormat.JPEG, photoQuality, outputStream)
@@ -837,7 +853,7 @@ object PhotoManager {
// Keep original CCM and other params
)
val bitmap = RawDemosaicProcessor.getInstance().process(
var bitmap = RawDemosaicProcessor.getInstance().process(
context,
byteBuffer,
firstImageWidth * if (useSuperResolution) 2 else 1,
@@ -850,6 +866,10 @@ object PhotoManager {
sharpeningValue = 0.4f
)
if (metadata.isMirrored && bitmap != null) {
bitmap = BitmapUtils.flipHorizontal(bitmap)
}
bitmap?.let {
val buffer = ByteBuffer.allocateDirect(it.width * it.height * 8)
it.copyPixelsToBuffer(buffer)
@@ -57,7 +57,8 @@ data class PhotoMetadata(
val presentationTimestampUs: Long? = null,
// DRO 模式
val droMode: String? = null,
val software: String? = null
val software: String? = null,
val isMirrored: Boolean = false
) {
/**
* 将元数据转换为 CaptureInfo,用于写入 EXIF
@@ -180,6 +181,7 @@ data class PhotoMetadata(
// DRO 模式
put("droMode", droMode ?: JSONObject.NULL)
put("software", software ?: JSONObject.NULL)
put("isMirrored", isMirrored)
}.toString(2)
}
@@ -266,7 +268,8 @@ data class PhotoMetadata(
exportedUris = exportedUris,
presentationTimestampUs = if (obj.isNull("presentationTimestampUs")) null else obj.optLong("presentationTimestampUs"),
droMode = if (obj.isNull("droMode")) null else obj.optString("droMode"),
software = if (obj.isNull("software")) null else obj.optString("software")
software = if (obj.isNull("software")) null else obj.optString("software"),
isMirrored = obj.optBoolean("isMirrored", false)
)
} catch (e: Exception) {
PLog.e(TAG, "Failed to parse JSON", e)
@@ -121,6 +121,7 @@ fun SettingsScreen(
val phantomMode by viewModel.phantomMode.collectAsState()
val phantomButtonHidden by viewModel.phantomButtonHidden.collectAsState()
val launchCameraOnPhantomMode by viewModel.launchCameraOnPhantomMode.collectAsState()
val mirrorFrontCamera by viewModel.mirrorFrontCamera.collectAsState(initial = true)
val context = androidx.compose.ui.platform.LocalContext.current
@@ -465,6 +466,18 @@ fun SettingsScreen(
modifier = Modifier.padding(vertical = 8.dp)
)
SwitchSettingItem(
title = stringResource(R.string.settings_mirror_front_camera),
description = stringResource(R.string.settings_mirror_front_camera_description),
checked = mirrorFrontCamera,
onCheckedChange = { viewModel.setMirrorFrontCamera(it) }
)
HorizontalDivider(
color = Color.White.copy(alpha = 0.1f),
modifier = Modifier.padding(vertical = 8.dp)
)
SwitchSettingItem(
title = stringResource(R.string.settings_vibration),
description = stringResource(R.string.settings_vibration_description),
@@ -148,6 +148,18 @@ object BitmapUtils {
}
}
/**
* 水平翻转 Bitmap
*/
fun flipHorizontal(bitmap: Bitmap): Bitmap {
val matrix = Matrix().apply { postScale(-1f, 1f) }
val flipped = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
if (flipped != bitmap) {
bitmap.recycle()
}
return flipped
}
fun Bitmap.toByteArray(): ByteArray {
val stream = ByteArrayOutputStream()
this.compress(Bitmap.CompressFormat.JPEG, 100, stream)
@@ -186,6 +186,8 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
.map { it.launchCameraOnPhantomMode }
.stateIn(viewModelScope, SharingStarted.Eagerly, false)
val mirrorFrontCamera: Flow<Boolean> = userPreferencesRepository.userPreferences.map { it.mirrorFrontCamera }
// 软件处理参数 Flow
val sharpening: Flow<Float> = userPreferencesRepository.userPreferences.map { it.sharpening }
val noiseReduction: Flow<Float> = userPreferencesRepository.userPreferences.map { it.noiseReduction }
@@ -1381,6 +1383,9 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
// 应用方向偏移
val rotation = (baseRotation + orientationOffset) % 360
val shouldMirror = lensFacing == CameraCharacteristics.LENS_FACING_FRONT &&
(userPreferencesRepository.userPreferences.firstOrNull()?.mirrorFrontCamera ?: true)
// 创建统一的 PhotoMetadata,包含编辑配置和拍摄信息
val metadata = PhotoMetadata(
lutId = lutIdToSave,
@@ -1402,7 +1407,8 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
focalLength35mm = captureInfo.formatFocalLength35mm(),
aperture = captureInfo.formatAperture(),
exposureBias = state.value.exposureBias,
droMode = droModeString
droMode = droModeString,
isMirrored = shouldMirror
)
val livePhotoVideoDeferred = if (useLivePhoto.value) {
@@ -1438,7 +1444,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
chromaNoiseReductionValue,
photoQualityValue,
exposureBias = state.value.exposureBias,
droMode = droModeForProcessing
droMode = droModeForProcessing,
)
}
PLog.d(TAG, "Image saved: $photoId, LUT: $lutIdToSave, Frame: $frameIdToSave")
@@ -1487,6 +1493,9 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
(sensorOrientation + deviceRotation) % 360
}
val shouldMirror = lensFacing == CameraCharacteristics.LENS_FACING_FRONT &&
(userPreferencesRepository.userPreferences.firstOrNull()?.mirrorFrontCamera ?: true)
// 获取用户配置的摄像头方向偏移
val userPrefs = userPreferencesRepository.userPreferences.firstOrNull()
val orientationOffset = userPrefs?.cameraOrientationOffsets?.get(currentCameraId) ?: 0
@@ -1517,7 +1526,8 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
focalLength35mm = captureInfo.formatFocalLength35mm(),
aperture = captureInfo.formatAperture(),
exposureBias = state.value.exposureBias,
droMode = droModeString
droMode = droModeString,
isMirrored = shouldMirror
)
val livePhotoVideoDeferred = if (useLivePhoto.value) {
@@ -1599,6 +1609,9 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
// 应用方向偏移
val rotation = (baseRotation + orientationOffset) % 360
val shouldMirror = lensFacing == CameraCharacteristics.LENS_FACING_FRONT &&
(userPreferencesRepository.userPreferences.firstOrNull()?.mirrorFrontCamera ?: true)
// 创建统一的 PhotoMetadata,包含编辑配置和拍摄信息
val metadata = PhotoMetadata(
lutId = lutIdToSave,
@@ -1619,6 +1632,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
focalLength = captureInfo.formatFocalLength(),
focalLength35mm = captureInfo.formatFocalLength35mm(),
aperture = captureInfo.formatAperture(),
isMirrored = shouldMirror
)
PhotoManager.preparePhoto(
@@ -1784,4 +1798,12 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
userPreferencesRepository.saveLaunchCameraOnPhantomMode(launch)
}
}
/**
* 设置是否启用自拍镜像
*/
fun setMirrorFrontCamera(enabled: Boolean) {
viewModelScope.launch {
userPreferencesRepository.saveMirrorFrontCamera(enabled)
}
}
}
+2
View File
@@ -134,6 +134,8 @@
<string name="settings_vibration_description">拍照时产生震动反馈</string>
<string name="settings_volume_key_action">音量键功能</string>
<string name="settings_volume_key_action_description">设置音量键在拍摄时的操作</string>
<string name="settings_mirror_front_camera">自拍镜像</string>
<string name="settings_mirror_front_camera_description">镜像前置摄像头的照片以匹配预览画面</string>
<string name="settings_volume_key_action_none">系统音量</string>
<string name="settings_volume_key_action_capture">快门/拍摄</string>
<string name="settings_volume_key_action_exposure">曝光补偿</string>
+2
View File
@@ -136,6 +136,8 @@
<string name="settings_vibration_description">Vibrate when taking a photo</string>
<string name="settings_volume_key_action">Volume Key Action</string>
<string name="settings_volume_key_action_description">Choose what volume keys do during capture</string>
<string name="settings_mirror_front_camera">Selfie Mirror</string>
<string name="settings_mirror_front_camera_description">Mirror front camera photos to match preview</string>
<string name="settings_volume_key_action_none">Volume Control</string>
<string name="settings_volume_key_action_capture">Shutter</string>
<string name="settings_volume_key_action_exposure">Exposure Comp.</string>