feat: HYP

This commit is contained in:
zhoubinjia committed 2026-06-18 14:28:20 +08:00
1 parent d55fe08c70
commit 5414a321eb
10 files changed
+342 -17

No files matched your search

@@ -96,8 +96,20 @@ class Camera2Controller(private val context: Context) {
private const val SCENE_CHANGE_CONFIRM_FRAMES = 3 // 连续 N 帧检测到变化才确认
private const val AI_SUBJECT_RECENT_MS = 1800L
private const val AI_FOCUS_FALLBACK_FRAMES = 6
private const val DEFAULT_HYPERFOCAL_FOCAL_LENGTH_MM = 4.0f
private const val DEFAULT_HYPERFOCAL_APERTURE = 1.8f
private const val HYPERFOCAL_COC_DIAGONAL_DIVISOR = 1500.0
}
private data class HyperfocalFocusResult(
val cameraId: String,
val focalLengthMm: Float,
val aperture: Float,
val circleOfConfusionMm: Float,
val distanceMeters: Float,
val focusDistanceDiopters: Float
)
private val cameraManager: CameraManager by lazy {
context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
}
@@ -225,6 +237,8 @@ class Camera2Controller(private val context: Context) {
private var aiSubjectLastSeenElapsedMs: Long = 0L
private var aiSubjectLastSeenX: Float = -1f
private var aiSubjectLastSeenY: Float = -1f
private var focusModeBeforeHyperfocal: Boolean? = null
private var focusDistanceBeforeHyperfocal: Float? = null
// 高光优先测光:最亮区域坐标(归一化 0-1)及平滑状态
@Volatile
@@ -704,6 +718,43 @@ class Camera2Controller(private val context: Context) {
return state.getCurrentCameraInfo()?.getOpenCameraId() ?: state.currentCameraId
}
private fun resolveActiveFocusCharacteristics(
fallbackCharacteristics: CameraCharacteristics? = cachedCharacteristics
): Pair<String, CameraCharacteristics>? {
val state = _state.value
val camera = state.getCurrentCameraInfo()
val targetZoomRatioByMain = getTargetZoomRatioByMain(state, camera)
val candidateIds = buildList {
activeOutputPhysicalCameraId?.let(::add)
camera?.getBoundPhysicalCameraId(targetZoomRatioByMain)?.let(::add)
camera?.baseCameraId?.let(::add)
camera?.cameraId?.takeUnless { camera.isVirtualIszLens }?.let(::add)
activeOpenCameraId.takeIf { it.isNotEmpty() }?.let(::add)
state.currentCameraId.takeIf { it.isNotEmpty() }?.let(::add)
}.distinct()
for (cameraId in candidateIds) {
try {
return cameraId to cameraManager.getCameraCharacteristics(cameraId)
} catch (e: Exception) {
PLog.v(TAG, "Failed to load focus characteristics for camera $cameraId: ${e.message}")
}
}
return fallbackCharacteristics?.let {
val fallbackId = activeOpenCameraId.takeIf { id -> id.isNotEmpty() }
?: state.currentCameraId
fallbackId to it
}
}
private fun refreshActiveFocusLimit() {
val focusCharacteristics = resolveActiveFocusCharacteristics()?.second
val minimumFocusDistance =
focusCharacteristics?.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE) ?: 0f
_state.value = _state.value.copy(minimumFocusDistance = minimumFocusDistance)
}
private fun refreshVideoCapabilities(characteristics: CameraCharacteristics? = null): Size {
val openCameraId = getCurrentOpenCameraId()
if (openCameraId.isEmpty()) {
@@ -900,6 +951,11 @@ class Camera2Controller(private val context: Context) {
val selectableNrModes = buildSelectableNoiseReductionModes(availableNoiseReductionModes)
val focusCharacteristics = resolveActiveFocusCharacteristics(cachedCharacteristics)?.second
?: cachedCharacteristics
val minimumFocusDistance =
focusCharacteristics?.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE) ?: 0f
_state.value = _state.value.copy(
isRawSupported = isRawSupported,
isP010Supported = isP010Supported,
@@ -907,8 +963,9 @@ class Camera2Controller(private val context: Context) {
availableNrModes = selectableNrModes,
currentPreviewSize = previewSize,
currentCaptureSize = if (captureMode == CaptureMode.VIDEO) previewSize else _state.value.currentCaptureSize,
minimumFocusDistance = cachedCharacteristics?.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE) ?: 0f
minimumFocusDistance = minimumFocusDistance
)
refreshHyperfocalFocusDistanceIfEnabled(updatePreview = false)
} catch (e: Exception) {
PLog.e(TAG, "Failed to cache camera characteristics", e)
cachedCharacteristics = null
@@ -1522,8 +1579,8 @@ class Camera2Controller(private val context: Context) {
// 4. 变焦设置
applyZoomSettings(builder, currentState)
// 5. 自动对焦设置
applyAutoFocusSettings(builder, currentState)
// 5. 对焦设置
applyFocusSettings(builder, currentState)
if (!isRawCapture) {
// 6. 图像质量设置(锐化、降噪)
@@ -1627,6 +1684,28 @@ class Camera2Controller(private val context: Context) {
}
}
private fun applyFocusSettings(builder: CaptureRequest.Builder, state: CameraState) {
if (state.isAutoFocus) {
applyAutoFocusSettings(builder, state)
return
}
val clampedDistance = if (state.minimumFocusDistance > 0f) {
state.focusDistance.coerceIn(0f, state.minimumFocusDistance)
} else {
0f
}
builder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_AUTO)
builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_IDLE)
builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF)
builder.set(CaptureRequest.LENS_FOCUS_DISTANCE, clampedDistance)
if (_state.value.currentAfMode != CaptureRequest.CONTROL_AF_MODE_OFF) {
_state.value = _state.value.copy(currentAfMode = CaptureRequest.CONTROL_AF_MODE_OFF)
}
}
private fun resolveAutoFocusMode(captureMode: CaptureMode): Int {
if (availableAfModes.isEmpty()) {
return CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE
@@ -3056,6 +3135,8 @@ class Camera2Controller(private val context: Context) {
)
activeOutputPhysicalCameraId = desiredPhysicalCameraId
refreshActiveFocusLimit()
refreshHyperfocalFocusDistanceIfEnabled(updatePreview = false)
safeCloseCaptureSession(captureSession, "physical zoom output changed")
captureSession = null
createPreviewSession(openGeneration = cameraOpenGeneration)
@@ -3066,15 +3147,14 @@ class Camera2Controller(private val context: Context) {
* 设置自动对焦开关
*/
fun setAutoFocus(auto: Boolean) {
_state.value = _state.value.copy(isAutoFocus = auto)
clearHyperfocalFocusMemory()
_state.value = _state.value.copy(
isAutoFocus = auto,
isHyperfocalFocusEnabled = false,
hyperfocalDistanceMeters = 0f
)
previewRequestBuilder?.apply {
if (auto) {
set(CaptureRequest.CONTROL_AF_MODE, resolveAutoFocusMode(_state.value.captureMode))
set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_IDLE)
} else {
set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF)
set(CaptureRequest.LENS_FOCUS_DISTANCE, _state.value.focusDistance)
}
applyFocusSettings(this, _state.value)
updatePreview()
}
}
@@ -3087,17 +3167,158 @@ class Camera2Controller(private val context: Context) {
if (minFocusDistance <= 0) return
val clampedDistance = distance.coerceIn(0f, minFocusDistance)
_state.value = _state.value.copy(focusDistance = clampedDistance)
clearHyperfocalFocusMemory()
_state.value = _state.value.copy(
focusDistance = clampedDistance,
isHyperfocalFocusEnabled = false,
hyperfocalDistanceMeters = 0f
)
if (!_state.value.isAutoFocus) {
previewRequestBuilder?.apply {
set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF)
set(CaptureRequest.LENS_FOCUS_DISTANCE, clampedDistance)
applyFocusSettings(this, _state.value)
updatePreview()
}
}
}
fun setHyperfocalFocusEnabled(enabled: Boolean) {
if (enabled) {
applyHyperfocalFocus(storePreviousFocus = true, updatePreview = true)
} else {
restoreFocusBeforeHyperfocal(updatePreview = true)
}
}
private fun refreshHyperfocalFocusDistanceIfEnabled(updatePreview: Boolean) {
if (!_state.value.isHyperfocalFocusEnabled) return
applyHyperfocalFocus(storePreviousFocus = false, updatePreview = updatePreview)
}
private fun applyHyperfocalFocus(
storePreviousFocus: Boolean,
updatePreview: Boolean
): Boolean {
refreshActiveFocusLimit()
val minFocusDistance = _state.value.minimumFocusDistance
if (minFocusDistance <= 0f) {
PLog.w(TAG, "Hyperfocal focus unavailable: manual focus is not supported")
if (_state.value.isHyperfocalFocusEnabled) {
restoreFocusBeforeHyperfocal(updatePreview = updatePreview)
}
return false
}
val result = calculateHyperfocalFocusResult()
if (result == null) {
PLog.w(TAG, "Hyperfocal focus unavailable: unable to resolve physical camera optics")
if (_state.value.isHyperfocalFocusEnabled) {
restoreFocusBeforeHyperfocal(updatePreview = updatePreview)
}
return false
}
if (storePreviousFocus && !_state.value.isHyperfocalFocusEnabled) {
focusModeBeforeHyperfocal = _state.value.isAutoFocus
focusDistanceBeforeHyperfocal = _state.value.focusDistance
}
val clampedFocusDistance = result.focusDistanceDiopters.coerceIn(0f, minFocusDistance)
_state.value = _state.value.copy(
isAutoFocus = false,
focusDistance = clampedFocusDistance,
isHyperfocalFocusEnabled = true,
hyperfocalDistanceMeters = result.distanceMeters
)
if (updatePreview) {
previewRequestBuilder?.apply {
applyFocusSettings(this, _state.value)
updatePreview()
}
}
PLog.i(
TAG,
"Hyperfocal focus enabled: camera=${result.cameraId}, " +
"f=${result.focalLengthMm}mm, N=${result.aperture}, " +
"c=${result.circleOfConfusionMm}mm, H=${result.distanceMeters}m, " +
"focus=${clampedFocusDistance}D"
)
return true
}
private fun restoreFocusBeforeHyperfocal(updatePreview: Boolean) {
val restoreAutoFocus = focusModeBeforeHyperfocal ?: true
val restoreFocusDistance = focusDistanceBeforeHyperfocal
?.coerceIn(0f, _state.value.minimumFocusDistance.takeIf { it > 0f } ?: Float.MAX_VALUE)
?: _state.value.focusDistance
clearHyperfocalFocusMemory()
_state.value = _state.value.copy(
isAutoFocus = restoreAutoFocus,
focusDistance = restoreFocusDistance,
isHyperfocalFocusEnabled = false,
hyperfocalDistanceMeters = 0f
)
if (updatePreview) {
previewRequestBuilder?.apply {
applyFocusSettings(this, _state.value)
updatePreview()
}
}
PLog.i(TAG, "Hyperfocal focus disabled: restoreAutoFocus=$restoreAutoFocus")
}
private fun clearHyperfocalFocusMemory() {
focusModeBeforeHyperfocal = null
focusDistanceBeforeHyperfocal = null
}
private fun calculateHyperfocalFocusResult(): HyperfocalFocusResult? {
val (cameraId, characteristics) = resolveActiveFocusCharacteristics() ?: return null
val focalLengthMm = characteristics
.get(CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS)
?.firstOrNull()
?.takeIf { it.isFinite() && it > 0f }
?: DEFAULT_HYPERFOCAL_FOCAL_LENGTH_MM
val aperture = characteristics
.get(CameraCharacteristics.LENS_INFO_AVAILABLE_APERTURES)
?.firstOrNull()
?.takeIf { it.isFinite() && it > 0f }
?: DEFAULT_HYPERFOCAL_APERTURE
val physicalSize = characteristics.get(CameraCharacteristics.SENSOR_INFO_PHYSICAL_SIZE)
?: return null
val diagonal = hypot(
physicalSize.width.toDouble(),
physicalSize.height.toDouble()
)
if (!diagonal.isFinite() || diagonal <= 0.0) return null
val circleOfConfusionMm = (diagonal / HYPERFOCAL_COC_DIAGONAL_DIVISOR).toFloat()
if (!circleOfConfusionMm.isFinite() || circleOfConfusionMm <= 0f) return null
val hyperfocalDistanceMm =
(focalLengthMm * focalLengthMm) / (aperture * circleOfConfusionMm) + focalLengthMm
val hyperfocalDistanceMeters = hyperfocalDistanceMm / 1000f
if (!hyperfocalDistanceMeters.isFinite() || hyperfocalDistanceMeters <= 0f) return null
return HyperfocalFocusResult(
cameraId = cameraId,
focalLengthMm = focalLengthMm,
aperture = aperture,
circleOfConfusionMm = circleOfConfusionMm,
distanceMeters = hyperfocalDistanceMeters,
focusDistanceDiopters = 1f / hyperfocalDistanceMeters
)
}
// ==================== 对焦控制 ====================
private fun recordFocusLockExposure(result: CaptureResult) {
@@ -275,6 +275,8 @@ data class CameraState(
// 对焦
val isAutoFocus: Boolean = true,
val focusDistance: Float = 0f, // 当前对焦距离 (0.0 - minimumFocusDistance)
val isHyperfocalFocusEnabled: Boolean = false,
val hyperfocalDistanceMeters: Float = 0f,
val minimumFocusDistance: Float = 0f, // 最小对焦距离
val focusPoint: Pair<Float, Float>? = null, // normalized coordinates (0-1)
val focusPointSource: FocusPointSource = FocusPointSource.MANUAL,
@@ -61,7 +61,11 @@ fun CameraParameterBar(
)
ParameterItem(
label = "AF",
value = if (state.isAutoFocus) "AUTO" else formatFocusDistance(state.focusDistance),
value = when {
state.isAutoFocus -> stringResource(R.string.camera_focus_auto)
state.isHyperfocalFocusEnabled -> stringResource(R.string.camera_hyperfocal_label)
else -> formatFocusDistance(state.focusDistance)
},
labelColor = yellow,
isSelected = selectedParameter == CameraParameter.FOCUS,
isEnabled = true,
@@ -10,9 +10,11 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.hinnka.mycamera.R
import com.hinnka.mycamera.camera.CameraState
@Composable
@@ -61,7 +63,11 @@ fun CameraParameterBarVerticel(
)
ParameterItem(
label = "AF",
value = if (state.isAutoFocus) "AUTO" else formatFocusDistance(state.focusDistance),
value = when {
state.isAutoFocus -> stringResource(R.string.camera_focus_auto)
state.isHyperfocalFocusEnabled -> stringResource(R.string.camera_hyperfocal_label)
else -> formatFocusDistance(state.focusDistance)
},
labelColor = yellow,
isSelected = selectedParameter == CameraParameter.FOCUS,
isEnabled = true,
@@ -740,6 +740,12 @@ fun CameraScreen(
else -> false
},
resetValue = selectedParameter.defaultResetValue(),
showHyperfocalButton = selectedParameter == CameraParameter.FOCUS && state.minimumFocusDistance > 0f,
hyperfocalEnabled = state.isHyperfocalFocusEnabled,
hyperfocalDistanceMeters = state.hyperfocalDistanceMeters,
onHyperfocalToggle = { enabled ->
viewModel.setHyperfocalFocusEnabled(enabled)
},
onValueChange = { value ->
when (selectedParameter) {
CameraParameter.EXPOSURE_COMPENSATION -> viewModel.setExposureCompensation((value / state.getExposureCompensationStep()).roundToInt())
@@ -924,7 +930,7 @@ fun CameraScreen(
isHlgInput = if (hlgHardwareCompatibilityEnabled) state.isHLG else false,
aperture = if (state.isVirtualApertureEnabled) state.virtualAperture else 0f,
isAutoFocus = state.isAutoFocus,
focusPeakingEnabled = focusPeakingEnabled,
focusPeakingEnabled = focusPeakingEnabled && !state.isHyperfocalFocusEnabled,
modifier = Modifier.fillMaxSize()
)
@@ -18,6 +18,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextMeasurer
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.drawText
@@ -27,6 +28,7 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.hinnka.mycamera.R
import kotlin.math.abs
import kotlin.math.roundToInt
@@ -53,6 +55,10 @@ fun ParameterRuler(
isAdjustable: Boolean,
showAutoButton: Boolean,
resetValue: Float? = null,
showHyperfocalButton: Boolean = false,
hyperfocalEnabled: Boolean = false,
hyperfocalDistanceMeters: Float = 0f,
onHyperfocalToggle: ((Boolean) -> Unit)? = null,
onValueChange: (Float) -> Unit,
onAutoModeToggle: () -> Unit,
modifier: Modifier = Modifier
@@ -103,6 +109,14 @@ fun ParameterRuler(
}
}
if (showHyperfocalButton && onHyperfocalToggle != null) {
HyperfocalFocusButton(
enabled = hyperfocalEnabled,
distanceMeters = hyperfocalDistanceMeters,
onToggle = { onHyperfocalToggle(!hyperfocalEnabled) }
)
}
// Ruler scale area
Box(
modifier = Modifier
@@ -160,6 +174,62 @@ fun ParameterRuler(
}
}
@Composable
private fun HyperfocalFocusButton(
enabled: Boolean,
distanceMeters: Float,
onToggle: () -> Unit
) {
val yellow = Color(0xFFFFD700)
Button(
onClick = onToggle,
modifier = Modifier
.padding(start = 6.dp)
.width(58.dp)
.height(34.dp),
colors = ButtonDefaults.buttonColors(
containerColor = if (enabled) yellow else Color.Gray.copy(alpha = 0.45f),
contentColor = if (enabled) Color.Black else Color.White
),
shape = RoundedCornerShape(17.dp),
contentPadding = PaddingValues(horizontal = 0.dp, vertical = 0.dp)
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(
text = stringResource(R.string.camera_hyperfocal_label),
fontSize = 9.sp,
lineHeight = 9.sp,
fontWeight = FontWeight.Bold,
maxLines = 1
)
if (enabled && distanceMeters > 0f) {
Text(
text = formatHyperfocalDistance(distanceMeters),
fontSize = 8.sp,
lineHeight = 8.sp,
fontWeight = FontWeight.Bold,
maxLines = 1
)
}
}
}
}
@Composable
private fun formatHyperfocalDistance(distanceMeters: Float): String {
return if (distanceMeters >= 1.0f) {
stringResource(R.string.camera_hyperfocal_distance_meters, distanceMeters)
} else {
stringResource(
R.string.camera_hyperfocal_distance_centimeters,
(distanceMeters * 100).roundToInt()
)
}
}
/**
* Ruler scale with tick marks and labels
*/
@@ -2297,6 +2297,10 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
cameraController.setFocusDistance(distance)
}
fun setHyperfocalFocusEnabled(enabled: Boolean) {
cameraController.setHyperfocalFocusEnabled(enabled)
}
/**
* 设置变焦倍数
*/
@@ -26,6 +26,10 @@
<!-- Exposure Controls -->
<!-- Focus -->
<string name="camera_focus_auto">AUTO</string>
<string name="camera_hyperfocal_label">超焦距</string>
<string name="camera_hyperfocal_distance_meters">%.1fm</string>
<string name="camera_hyperfocal_distance_centimeters">%dcm</string>
<!-- Aspect Ratio -->
<string name="aspect_ratio">画面比例</string>
@@ -12,6 +12,10 @@
<!-- Exposure Controls -->
<!-- Focus -->
<string name="camera_focus_auto">AUTO</string>
<string name="camera_hyperfocal_label">超焦距</string>
<string name="camera_hyperfocal_distance_meters">%.1fm</string>
<string name="camera_hyperfocal_distance_centimeters">%dcm</string>
<!-- Aspect Ratio -->
<string name="aspect_ratio">畫面比例</string>
+4
View File
@@ -26,6 +26,10 @@
<!-- Exposure Controls -->
<!-- Focus -->
<string name="camera_focus_auto">AUTO</string>
<string name="camera_hyperfocal_label">HYP</string>
<string name="camera_hyperfocal_distance_meters">%.1fm</string>
<string name="camera_hyperfocal_distance_centimeters">%dcm</string>
<!-- Aspect Ratio -->
<string name="aspect_ratio">Aspect Ratio</string>