feat: auto nr level
This commit is contained in:
1 parent
b912a549cf
commit
125cd3bb3e
9 files changed
+70
-16
No files matched your search
@@ -87,8 +87,8 @@ class Camera2Controller(private val context: Context) {
|
||||
val previewDepthProcessor = com.hinnka.mycamera.preview.PreviewDepthProcessor(context)
|
||||
|
||||
|
||||
// 降噪等级 (0=Off, 1=Fast, 2=High Quality, 3=Real-time)
|
||||
private var nrLevel = 1
|
||||
// 降噪等级 (0=Off, 1=Fast, 2=High Quality, 3=ZSL, 4=Minimal, 5=Auto)
|
||||
private var nrLevel = 5
|
||||
|
||||
// 锐化等级 (0=Off, 1=Fast, 2=High Quality, 3=Zero Shutter Lag/Real-time)
|
||||
private var edgeLevel = 1
|
||||
@@ -482,12 +482,14 @@ class Camera2Controller(private val context: Context) {
|
||||
"ManualSensor: $isManualSensorSupported, ManualPost: $isManualPostProcessingSupported, RAW: $isRawSupported, P010: $isP010Supported"
|
||||
)
|
||||
|
||||
val selectableNrModes = buildSelectableNoiseReductionModes(availableNoiseReductionModes)
|
||||
|
||||
_state.value = _state.value.copy(
|
||||
isRawSupported = isRawSupported,
|
||||
isP010Supported = isP010Supported,
|
||||
isHlg10Supported = isHlg10Supported,
|
||||
currentDynamicRangeProfile = if (shouldUseHlgCapture()) "HLG10" else "STANDARD",
|
||||
availableNrModes = availableNoiseReductionModes
|
||||
availableNrModes = selectableNrModes
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to cache camera characteristics", e)
|
||||
@@ -1102,7 +1104,8 @@ class Camera2Controller(private val context: Context) {
|
||||
*/
|
||||
private fun applyImageQualitySettings(builder: CaptureRequest.Builder, isCapture: Boolean) {
|
||||
try {
|
||||
val isBurst = _state.value.useMFNR || _state.value.useMFSR
|
||||
val currentState = _state.value
|
||||
val isBurst = currentState.useMFNR || currentState.useMFSR
|
||||
val effectiveEdgeLevel = if (isBurst && edgeLevel == 2) 1 else edgeLevel
|
||||
val edgeMode = when (effectiveEdgeLevel) {
|
||||
0 -> CaptureRequest.EDGE_MODE_OFF
|
||||
@@ -1121,7 +1124,8 @@ class Camera2Controller(private val context: Context) {
|
||||
} else if (availableEdgeModes.contains(CaptureRequest.EDGE_MODE_FAST)) {
|
||||
builder.set(CaptureRequest.EDGE_MODE, CaptureRequest.EDGE_MODE_FAST)
|
||||
}
|
||||
val effectiveNrLevel = if (isBurst && nrLevel == 2) 1 else nrLevel
|
||||
val resolvedNrLevel = resolveAutoNoiseReductionLevel(currentState, isCapture)
|
||||
val effectiveNrLevel = if (isBurst && resolvedNrLevel == 2) 1 else resolvedNrLevel
|
||||
val noiseReductionMode = when (effectiveNrLevel) {
|
||||
0 -> CaptureRequest.NOISE_REDUCTION_MODE_OFF
|
||||
4 -> if (availableNoiseReductionModes.contains(CaptureRequest.NOISE_REDUCTION_MODE_MINIMAL)) {
|
||||
@@ -1151,6 +1155,51 @@ class Camera2Controller(private val context: Context) {
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildSelectableNoiseReductionModes(hardwareModes: IntArray): IntArray {
|
||||
val orderedModes = mutableListOf(5)
|
||||
val preferredOrder = listOf(
|
||||
CaptureRequest.NOISE_REDUCTION_MODE_OFF,
|
||||
CaptureRequest.NOISE_REDUCTION_MODE_FAST,
|
||||
CaptureRequest.NOISE_REDUCTION_MODE_HIGH_QUALITY,
|
||||
CaptureRequest.NOISE_REDUCTION_MODE_ZERO_SHUTTER_LAG,
|
||||
CaptureRequest.NOISE_REDUCTION_MODE_MINIMAL
|
||||
)
|
||||
preferredOrder.forEach { mode ->
|
||||
if (hardwareModes.contains(mode)) {
|
||||
orderedModes += mode
|
||||
}
|
||||
}
|
||||
return orderedModes.toIntArray()
|
||||
}
|
||||
|
||||
private fun resolveAutoNoiseReductionLevel(state: CameraState, isCapture: Boolean): Int {
|
||||
if (nrLevel != 5) {
|
||||
return nrLevel
|
||||
}
|
||||
val lightValue = calculateCaptureLightValue(state, isCapture)
|
||||
val resolvedLevel = when {
|
||||
lightValue >= 9.0 -> 0
|
||||
lightValue >= 6.0 -> 4
|
||||
lightValue >= 4.0 -> 1
|
||||
else -> 2
|
||||
}
|
||||
PLog.d(TAG, "Auto NR resolved by LV=$lightValue to level=$resolvedLevel")
|
||||
return resolvedLevel
|
||||
}
|
||||
|
||||
private fun calculateCaptureLightValue(state: CameraState, isCapture: Boolean): Double {
|
||||
val aperture = state.physicalAperture.takeIf { it > 0f }?.toDouble() ?: 2.0
|
||||
val exposureTimeNs = if (isCapture) {
|
||||
state.shutterSpeed
|
||||
} else {
|
||||
state.shutterSpeed.coerceAtMost(MAX_PREVIEW_EXPOSURE_TIME)
|
||||
}
|
||||
val exposureTimeSeconds = exposureTimeNs / 1_000_000_000.0
|
||||
val iso = state.iso.coerceAtLeast(1).toDouble()
|
||||
val ev100 = ln((aperture * aperture / exposureTimeSeconds) * (100.0 / iso)) / ln(2.0)
|
||||
return (ev100 * 10.0).roundToInt() / 10.0
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置锐化等级
|
||||
*/
|
||||
|
||||
@@ -171,8 +171,8 @@ data class CameraState(
|
||||
// 网格线
|
||||
val showGrid: Boolean = false, // 是否显示网格线
|
||||
|
||||
// 降噪等级 (0=Off, 1=Fast, 2=High Quality, 3=Real-time, 4=Minimal)
|
||||
val nrLevel: Int = 1,
|
||||
// 降噪等级 (0=Off, 1=Fast, 2=High Quality, 3=ZSL, 4=Minimal, 5=Auto)
|
||||
val nrLevel: Int = 5,
|
||||
val availableNrModes: IntArray = intArrayOf(),
|
||||
|
||||
val isRawSupported: Boolean = false,
|
||||
|
||||
@@ -48,7 +48,7 @@ data class UserPreferences(
|
||||
val vibrationEnabled: Boolean = true, // 拍摄震动
|
||||
val volumeKeyAction: VolumeKeyAction = VolumeKeyAction.CAPTURE, // 音量键操作
|
||||
val autoSaveAfterCapture: Boolean = true, // 自动保存
|
||||
val nrLevel: Int = 1, // 降噪等级:0=Off, 1=Fast, 2=High Quality, 3=Real-time
|
||||
val nrLevel: Int = 5, // 降噪等级:0=Off, 1=Fast, 2=High Quality, 3=ZSL, 4=Minimal, 5=Auto
|
||||
val edgeLevel: Int = 1, // 锐化等级:0=Off, 1=Fast, 2=High Quality, 3=Real-time
|
||||
val useRaw: Boolean = false, // 使用 RAW 格式拍摄
|
||||
val sharpening: Float = 0f, // 0.0 ~ 1.0 锐化强度
|
||||
@@ -185,7 +185,7 @@ class UserPreferencesRepository(private val context: Context) {
|
||||
preferences[VOLUME_KEY_ACTION] ?: VolumeKeyAction.CAPTURE.name
|
||||
),
|
||||
autoSaveAfterCapture = preferences[AUTO_SAVE_AFTER_CAPTURE] ?: true,
|
||||
nrLevel = preferences[NR_LEVEL] ?: 1,
|
||||
nrLevel = preferences[NR_LEVEL] ?: 5,
|
||||
edgeLevel = preferences[EDGE_LEVEL] ?: 1,
|
||||
useRaw = preferences[USE_RAW] ?: false,
|
||||
// 软件处理参数
|
||||
|
||||
@@ -136,17 +136,18 @@ fun CameraTopSheet(
|
||||
|
||||
val nrLevelNames = availableNrLevels.map {
|
||||
when (it) {
|
||||
5 -> stringResource(R.string.settings_nr_level_auto)
|
||||
0 -> stringResource(R.string.settings_nr_level_off)
|
||||
1 -> stringResource(R.string.settings_nr_level_fast)
|
||||
2 -> stringResource(R.string.settings_nr_level_high_quality)
|
||||
3 -> stringResource(R.string.settings_nr_level_minimal)
|
||||
4 -> stringResource(R.string.settings_nr_level_zsl)
|
||||
3 -> stringResource(R.string.settings_nr_level_zsl)
|
||||
4 -> stringResource(R.string.settings_nr_level_minimal)
|
||||
else -> "Unknown"
|
||||
}
|
||||
}
|
||||
QuickSettingValue(
|
||||
title = stringResource(R.string.settings_nr_level),
|
||||
value = nrLevelNames.getOrElse(nrLevel) { "Unknown" },
|
||||
value = nrLevelNames.getOrElse(availableNrLevels.indexOf(nrLevel)) { "Unknown" },
|
||||
onClick = {
|
||||
val nextIndex = (availableNrLevels.indexOf(nrLevel) + 1) % availableNrLevels.size
|
||||
val nextLevel = availableNrLevels[nextIndex]
|
||||
|
||||
@@ -123,7 +123,7 @@ fun SettingsScreen(
|
||||
val vibrationEnabled by viewModel.vibrationEnabled.collectAsState(initial = true)
|
||||
val volumeKeyAction by viewModel.volumeKeyAction.collectAsState()
|
||||
val autoSaveAfterCapture by viewModel.autoSaveAfterCapture.collectAsState(initial = true)
|
||||
val nrLevel by viewModel.nrLevel.collectAsState(initial = 1)
|
||||
val nrLevel by viewModel.nrLevel.collectAsState(initial = 5)
|
||||
val edgeLevel by viewModel.edgeLevel.collectAsState(initial = 1)
|
||||
val useRaw by viewModel.useRaw.collectAsState(initial = false)
|
||||
val useSuperResolution by viewModel.useMFSR.collectAsState(initial = false)
|
||||
@@ -653,11 +653,12 @@ fun SettingsScreen(
|
||||
title = stringResource(R.string.settings_nr_level),
|
||||
description = stringResource(R.string.settings_nr_level_description),
|
||||
levels = listOf(
|
||||
5 to stringResource(R.string.settings_nr_level_auto),
|
||||
0 to stringResource(R.string.settings_nr_level_off),
|
||||
1 to stringResource(R.string.settings_nr_level_fast),
|
||||
2 to stringResource(R.string.settings_nr_level_high_quality),
|
||||
3 to stringResource(R.string.settings_nr_level_minimal),
|
||||
4 to stringResource(R.string.settings_nr_level_zsl)
|
||||
3 to stringResource(R.string.settings_nr_level_zsl),
|
||||
4 to stringResource(R.string.settings_nr_level_minimal)
|
||||
),
|
||||
currentLevel = nrLevel,
|
||||
onLevelSelected = { viewModel.setNRLevel(it) }
|
||||
|
||||
@@ -151,7 +151,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
val autoSaveAfterCapture: Flow<Boolean> = userPreferencesRepository.userPreferences.map { it.autoSaveAfterCapture }
|
||||
val nrLevel: StateFlow<Int> = userPreferencesRepository.userPreferences
|
||||
.map { it.nrLevel }
|
||||
.stateIn(viewModelScope, SharingStarted.Eagerly, 1)
|
||||
.stateIn(viewModelScope, SharingStarted.Eagerly, 5)
|
||||
val useRaw: StateFlow<Boolean> = userPreferencesRepository.userPreferences
|
||||
.map { it.useRaw }
|
||||
.stateIn(viewModelScope, SharingStarted.Eagerly, false)
|
||||
|
||||
@@ -172,6 +172,7 @@
|
||||
<string name="settings_nr_level_fast">快速</string>
|
||||
<string name="settings_nr_level_high_quality">高质量</string>
|
||||
<string name="settings_nr_level_zsl">零延迟</string>
|
||||
<string name="settings_nr_level_auto">自动</string>
|
||||
<string name="settings_edge_level">锐化等级</string>
|
||||
<string name="settings_edge_level_description">调整系统硬件边缘增强(锐化)模式。</string>
|
||||
<string name="settings_sharpening">锐化</string>
|
||||
|
||||
@@ -170,6 +170,7 @@
|
||||
<string name="settings_nr_level_fast">快速</string>
|
||||
<string name="settings_nr_level_high_quality">高品質</string>
|
||||
<string name="settings_nr_level_zsl">零延遲</string>
|
||||
<string name="settings_nr_level_auto">自動</string>
|
||||
<string name="settings_edge_level">銳化等級</string>
|
||||
<string name="settings_edge_level_description">調整系統硬體邊緣增強(銳化)模式。</string>
|
||||
<string name="settings_sharpening">銳化</string>
|
||||
|
||||
@@ -174,6 +174,7 @@
|
||||
<string name="settings_nr_level_fast">Fast</string>
|
||||
<string name="settings_nr_level_high_quality">HQ</string>
|
||||
<string name="settings_nr_level_zsl">ZSL</string>
|
||||
<string name="settings_nr_level_auto">Auto</string>
|
||||
<string name="settings_edge_level">Sharpening Level</string>
|
||||
<string name="settings_edge_level_description">Adjust the hardware edge enhancement (sharpening) mode.</string>
|
||||
<string name="settings_sharpening">Sharpening</string>
|
||||
|
||||
Reference in new issue
Block a user