feat: aperture

This commit is contained in:
Hinnka committed 2026-03-08 19:48:09 +08:00
1 parent 8c68b77f80
commit e95cb4dfd1
13 files changed
+99 -26

No files matched your search

+1 -1
View File
@@ -13,7 +13,7 @@ android {
applicationId = "com.hinnka.mycamera"
minSdk = 30
targetSdk = 36
versionCode = 39
versionCode = 40
versionName = "1.9.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
+9
View File
@@ -100,6 +100,15 @@
},
"path": "ricoh_yellow.plut",
"isVip": true
},
{
"id": "fl3",
"name": {
"en": "FL3",
"zh": "FL3"
},
"path": "fl3.plut",
"isVip": true
}
]
}
Binary file not shown.
@@ -83,7 +83,8 @@ data class UserPreferences(
val openAIBaseUrl: String? = null,
val openAIModel: String? = null,
val useBuiltInAiService: Boolean = false,
val phantomSaveAsNew: Boolean = false
val phantomSaveAsNew: Boolean = false,
val defaultVirtualAperture: Float = 0f // 默认虚化光圈,0表示关闭
)
/**
@@ -148,6 +149,7 @@ class UserPreferencesRepository(private val context: Context) {
private val OPENAI_MODEL = stringPreferencesKey("openai_model")
private val USE_BUILT_IN_AI_SERVICE = booleanPreferencesKey("use_built_in_ai_service")
private val PHANTOM_SAVE_AS_NEW = booleanPreferencesKey("phantom_save_as_new")
private val DEFAULT_VIRTUAL_APERTURE = floatPreferencesKey("default_virtual_aperture")
}
/**
@@ -205,7 +207,8 @@ class UserPreferencesRepository(private val context: Context) {
openAIBaseUrl = preferences[OPENAI_BASE_URL],
openAIModel = preferences[OPENAI_MODEL],
useBuiltInAiService = preferences[USE_BUILT_IN_AI_SERVICE] ?: false,
phantomSaveAsNew = preferences[PHANTOM_SAVE_AS_NEW] ?: false
phantomSaveAsNew = preferences[PHANTOM_SAVE_AS_NEW] ?: false,
defaultVirtualAperture = preferences[DEFAULT_VIRTUAL_APERTURE] ?: 0f
)
}
@@ -688,4 +691,13 @@ class UserPreferencesRepository(private val context: Context) {
preferences[PHANTOM_SAVE_AS_NEW] = enabled
}
}
/**
* 保存默认虚拟光圈
*/
suspend fun saveDefaultVirtualAperture(aperture: Float) {
context.dataStore.edit { preferences ->
preferences[DEFAULT_VIRTUAL_APERTURE] = aperture
}
}
}
@@ -390,20 +390,19 @@ object PhotoManager {
}
suspend fun generateBokehPhoto(context: Context, photoId: String, metadata: PhotoMetadata, bitmap: Bitmap) {
val aperture = metadata.computationalAperture
val aperture = metadata.computationalAperture ?: return
if (aperture <= 0f) return
val focusPointX = metadata.focusPointX
val focusPointY = metadata.focusPointX
val bokeh = aperture?.let {
ContentRepository.getInstance(context).depthBokehProcessor.applyHighQualityBokeh(
context,
photoId,
bitmap,
focusPointX,
focusPointY,
it
)
}
bokeh?.let { bokeh -> saveBokehPhoto(context, photoId, bokeh) }
val focusPointY = metadata.focusPointY
val bokeh = ContentRepository.getInstance(context).depthBokehProcessor.applyHighQualityBokeh(
context,
photoId,
bitmap,
focusPointX,
focusPointY,
aperture
)
saveBokehPhoto(context, photoId, bokeh)
}
suspend fun saveYuvPhoto(
@@ -1249,14 +1248,14 @@ object PhotoManager {
}
fun loadBitmap(context: Context, photoId: String, maxEdge: Int? = null): Bitmap? {
val photoFile = getPhotoFile(context, photoId)
val bokehFile = getBokehFile(context, photoId)
if (!photoFile.exists()) {
return null
}
if (bokehFile.exists()) {
return loadBitmap(context, Uri.fromFile(bokehFile), maxEdge)
}
val photoFile = getPhotoFile(context, photoId)
if (!photoFile.exists()) {
return null
}
return loadBitmap(context, Uri.fromFile(photoFile), maxEdge)
}
@@ -1353,6 +1352,7 @@ object PhotoManager {
context: Context,
uri: Uri,
lutId: String?,
computationalAperture: Float? = null,
photoId: String? = null,
): String? {
return withContext(Dispatchers.IO) {
@@ -1377,6 +1377,7 @@ object PhotoManager {
// 2. 读取元数据以获取旋转信息
val metadata = PhotoMetadata.fromUri(context, uri).copy(
lutId = lutId,
computationalAperture = computationalAperture,
sourceUri = uri.toString()
)
@@ -1423,6 +1424,9 @@ object PhotoManager {
rotation = 0,
)
metadataFile.writeText(updatedMetadata.toJson())
if (updatedMetadata.computationalAperture != null) {
generateBokehPhoto(context, photoId, updatedMetadata, processedBitmap)
}
processedBitmap.recycle()
} else {
@@ -1434,6 +1438,13 @@ object PhotoManager {
// --- 常规 JPEG 处理逻辑 ---
// 传递元数据确保旋转信息被正确处理
tempImportJpeg(uri, context, metadata, photoFile, metadataFile, thumbnailFile)
if (metadata.computationalAperture != null) {
val bitmap = BitmapFactory.decodeFile(photoFile.absolutePath)
if (bitmap != null) {
generateBokehPhoto(context, photoId, metadata, bitmap)
bitmap.recycle()
}
}
}
// Check for Motion Photo after import
@@ -330,9 +330,10 @@ class PhantomService(val context: Context) : LifecycleOwner, SavedStateRegistryO
val preferences = userPreferencesRepository.userPreferences.firstOrNull()
val lutId = preferences?.lutId ?: availableLutList.firstOrNull { it.isDefault }?.id
val saveAsNew = preferences?.phantomSaveAsNew ?: false
val computationalAperture = preferences?.defaultVirtualAperture?.let { if (it > 0f) it else null }
val existingPhotoId = if (processingInfo?.uri == uri) processingInfo?.photoId else null
val photoId =
PhotoManager.importPhoto(context, uri, lutId, existingPhotoId) ?: run {
PhotoManager.importPhoto(context, uri, lutId, computationalAperture, existingPhotoId) ?: run {
return@withContext
}
val metadata = PhotoManager.loadMetadata(context, photoId) ?: run {
@@ -447,6 +447,7 @@ private fun getScaleValues(parameter: CameraParameter, minValue: Float, maxValue
listOf(
minValue,
1f,
1.2f,
1.4f,
1.8f,
2f,
@@ -595,12 +595,13 @@ fun PhotoEditScreen(
} else if (editTab == 1) {
Spacer(modifier = Modifier.height(16.dp))
// 细节处理调整 (锐化, 降噪, 杂色降噪)
val aperture = editComputationalAperture
SliderSettingItem(
title = stringResource(R.string.virtual_aperture),
value = editComputationalAperture ?: 2.8f,
valueRange = 1.0f..16.0f,
onValueChange = { viewModel.setComputationalAperture(it) },
toggleValue = editComputationalAperture != null,
toggleValue = aperture != null && aperture > 0f,
onToggleChange = { checked ->
if (checked) {
viewModel.setComputationalAperture(2.8f)
@@ -153,6 +153,7 @@ fun SettingsScreen(
val availableOpenAIModels by viewModel.availableOpenAIModels.collectAsState()
val isFetchingAIModels by viewModel.isFetchingAIModels.collectAsState()
val phantomSaveAsNew by viewModel.phantomSaveAsNew.collectAsState()
val defaultVirtualAperture by viewModel.defaultVirtualAperture.collectAsState(initial = 0f)
var selectedTab by remember { mutableStateOf(SettingsTab.GENERAL) }
@@ -544,6 +545,21 @@ fun SettingsScreen(
currentFocalLength = defaultFocalLength,
onFocalLengthSelected = { viewModel.setDefaultFocalLength(it) }
)
HorizontalDivider(
color = Color.White.copy(alpha = 0.1f),
modifier = Modifier.padding(vertical = 12.dp)
)
QualityLevelSetting(
title = stringResource(R.string.settings_default_virtual_aperture),
description = stringResource(R.string.settings_default_virtual_aperture_description),
levels = listOf(0f to stringResource(R.string.settings_nr_level_off)) + listOf(
1.0f, 1.2f, 1.4f, 1.8f, 2.0f, 2.8f, 4.0f, 5.6f, 8.0f, 11f, 16f
).map { it to "f/${if (it % 1f == 0f) it.toInt() else it}" },
currentLevel = defaultVirtualAperture,
onLevelSelected = { viewModel.setDefaultVirtualAperture(it) }
)
}
}
@@ -196,6 +196,8 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
val phantomSaveAsNew: StateFlow<Boolean> = userPreferencesRepository.userPreferences
.map { it.phantomSaveAsNew }
.stateIn(viewModelScope, SharingStarted.Eagerly, false)
val defaultVirtualAperture: Flow<Float> =
userPreferencesRepository.userPreferences.map { it.defaultVirtualAperture }
val mirrorFrontCamera: Flow<Boolean> = userPreferencesRepository.userPreferences.map { it.mirrorFrontCamera }
val widgetTheme = userPreferencesRepository.userPreferences.map { it.widgetTheme }
@@ -398,6 +400,12 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
cameraController.setUseMultiFrame(prefs.useMultiFrame, prefs.multiFrameCount)
cameraController.setUseSuperResolution(prefs.useSuperResolution)
cameraController.setUseLivePhoto(prefs.useLivePhoto)
// 应用保存的虚拟光圈
if (prefs.defaultVirtualAperture > 0f) {
setVirtualApertureAuto(true)
setAperture(prefs.defaultVirtualAperture)
}
} else {
// 如果没有任何偏好设置,使用配置文件中的默认 LUT(第一个)
val defaultLut = availableLutList.firstOrNull { it.isDefault }
@@ -1988,6 +1996,12 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
}
}
fun setDefaultVirtualAperture(aperture: Float) {
viewModelScope.launch {
userPreferencesRepository.saveDefaultVirtualAperture(aperture)
}
}
/**
* 设置是否启用自拍镜像
*/
@@ -78,6 +78,8 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
val photoQuality: Flow<Int> = userPreferencesRepository.userPreferences.map { it.photoQuality }
val defaultLutId: Flow<String?> = userPreferencesRepository.userPreferences.map { it.lutId }
val defaultVirtualAperture: StateFlow<Float> = userPreferencesRepository.userPreferences.map { it.defaultVirtualAperture }
.stateIn(viewModelScope, SharingStarted.Eagerly, 0f)
// 计费管理器
private val billingManager = com.hinnka.mycamera.billing.BillingManagerImpl(application)
@@ -251,7 +253,8 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
val focusPointX = editFocusPointX.value
val focusPointY = editFocusPointY.value
val photoData = getCurrentPhoto() ?: return@launch
val bitmap = PhotoManager.loadOriginalBitmap(context, photoData.id) ?: return@launch
val bitmap = PhotoManager.loadOriginalBitmap(context, photoData.id)
?: PhotoManager.loadBitmap(context, photoData.uri) ?: return@launch
if (!isActive) return@launch
val bokeh = aperture?.let {
contentRepository.depthBokehProcessor.applyHighQualityBokeh(
@@ -1164,7 +1167,6 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
editSharpening.value = sharpening.value
editNoiseReduction.value = noiseReduction.value
editChromaNoiseReduction.value = chromaNoiseReduction.value
editComputationalAperture.value = null
editFocusPointX.value = null
editFocusPointY.value = null
@@ -1374,7 +1376,8 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
val currentBitmap = bitmap ?: if (selectedTab == GalleryTab.PHOTON) {
PhotoManager.loadBitmap(context, photo.id, 4096)
} else {
PhotoManager.loadBitmap(context, photo.uri, 4096)
PhotoManager.loadBitmap(context, photo.id, 4096)
?: PhotoManager.loadBitmap(context, photo.uri, 4096)
} ?: return@withContext null
previewBitmapCache.put(previewCacheKey(photo, finalMetadata, true), currentBitmap)
@@ -1608,8 +1611,9 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
withContext(Dispatchers.IO) {
val lutId = defaultLutId.firstOrNull() ?: contentRepository.getAvailableLuts()
.firstOrNull { it.isDefault }?.id
val computationalAperture = defaultVirtualAperture.firstOrNull()?.let { if (it > 0f) it else null }
uris.forEach { uri ->
val photoId = PhotoManager.importPhoto(context, uri, lutId)
val photoId = PhotoManager.importPhoto(context, uri, lutId, computationalAperture)
if (photoId != null) {
successCount++
}
+2
View File
@@ -392,4 +392,6 @@
<string name="about">关于</string>
<string name="aperture">光圈</string>
<string name="virtual_aperture">虚拟光圈</string>
<string name="settings_default_virtual_aperture">默认虚拟光圈</string>
<string name="settings_default_virtual_aperture_description">开启后,拍摄及导入照片将自动应用所选光圈值的虚化效果。</string>
</resources>
+2
View File
@@ -392,4 +392,6 @@
<string name="about">About</string>
<string name="aperture">Aperture</string>
<string name="virtual_aperture">Virtual Aperture</string>
<string name="settings_default_virtual_aperture">Default Virtual Aperture</string>
<string name="settings_default_virtual_aperture_description">Automatically enable virtual aperture with the selected f-stop value for new photos.</string>
</resources>