feat: physical aperture control for xiaomi 14u
This commit is contained in:
1 parent
61458c3d63
commit
4e74d6eb45
10 files changed
+632
-145
No files matched your search
@@ -306,6 +306,11 @@ class Camera2Controller(private val context: Context) {
|
||||
private var cachedCharacteristicsCameraId: String = ""
|
||||
private var activeOpenCameraId: String = ""
|
||||
private var activeOutputPhysicalCameraId: String? = null
|
||||
private var apertureSelectedCameraId: String = ""
|
||||
private var apertureCameraId: String? = null
|
||||
private var lensAperture = LensApertureCapabilities()
|
||||
private var defaultPreviewAperture: Float? = null
|
||||
private val requestedAperturesByCamera = mutableMapOf<String, Float>()
|
||||
private val failedPhysicalOutputProfiles = mutableSetOf<PhysicalOutputFailureKey>()
|
||||
// "streamCameraId:format" profiles whose high-resolution-only capture size failed session configuration
|
||||
private val failedHighResolutionCaptureProfiles = mutableSetOf<String>()
|
||||
@@ -347,6 +352,8 @@ class Camera2Controller(private val context: Context) {
|
||||
private var availableCaptureRequestKeyNames: Set<String>? = null
|
||||
private var availableAeModes: IntArray = intArrayOf()
|
||||
private var availableAwbModes: IntArray = intArrayOf()
|
||||
private val supportsManualSensorExposure: Boolean
|
||||
get() = isManualSensorSupported && CaptureRequest.CONTROL_AE_MODE_OFF in availableAeModes
|
||||
private val videoTimingDiagnosticsEnabled = BuildConfig.DEBUG &&
|
||||
com.hinnka.mycamera.utils.SystemPropertiesUtil.get("debug.photon.video.timing") == "true"
|
||||
private var videoCaptureStatsWindowStartMs: Long = 0L
|
||||
@@ -1300,15 +1307,21 @@ class Camera2Controller(private val context: Context) {
|
||||
} else {
|
||||
canAdjustManualWhiteBalance(whiteBalanceResult)
|
||||
}
|
||||
val aperture = result.get(CaptureResult.LENS_APERTURE)
|
||||
if (session === captureSession && apertureSelectedCameraId == _state.value.currentCameraId) {
|
||||
updatePhysicalApertureFromResult(result)
|
||||
}
|
||||
val focusDistance = result.get(CaptureResult.LENS_FOCUS_DISTANCE) ?: 0f
|
||||
|
||||
// 关键修复:只在自动曝光模式下更新 ISO 和快门速度
|
||||
// 手动模式下保持用户设置不变(因为预览使用的是限制后的曝光时间,不是用户设置的值)
|
||||
_state.value = _state.value.copy(
|
||||
iso = if (isAutoExposure && _state.value.isIsoAuto) actualIso ?: _state.value.iso else _state.value.iso,
|
||||
shutterSpeed = if (isAutoExposure && _state.value.isShutterSpeedAuto) actualExposureTimeNs
|
||||
?: _state.value.shutterSpeed else _state.value.shutterSpeed,
|
||||
val currentState = _state.value
|
||||
val readAutoExposure = session === captureSession && isAutoExposure &&
|
||||
currentState.currentCameraId == apertureSelectedCameraId &&
|
||||
usesCameraAutoExposure(currentState)
|
||||
_state.value = currentState.copy(
|
||||
iso = if (readAutoExposure && currentState.isIsoAuto) actualIso ?: currentState.iso else currentState.iso,
|
||||
shutterSpeed = if (readAutoExposure && currentState.isShutterSpeedAuto) actualExposureTimeNs
|
||||
?: currentState.shutterSpeed else currentState.shutterSpeed,
|
||||
// In-flight results must not undo a user's WB mode change. A session without
|
||||
// a manual anchor still reports the automatic fallback used by the request.
|
||||
awbMode = if (_state.value.awbMode == CameraMetadata.CONTROL_AWB_MODE_OFF &&
|
||||
@@ -1321,7 +1334,6 @@ class Camera2Controller(private val context: Context) {
|
||||
supportsCctWhiteBalance = supportsCctWhiteBalance(),
|
||||
awbTemperatureMin = awbRange.lower,
|
||||
awbTemperatureMax = awbRange.upper,
|
||||
physicalAperture = aperture ?: _state.value.physicalAperture,
|
||||
// 手动对焦保留用户目标值,避免镜头移动中的回报值改变后续拍摄和预览请求。
|
||||
focusDistance = if (_state.value.isAutoFocus) focusDistance else _state.value.focusDistance
|
||||
)
|
||||
@@ -1680,7 +1692,7 @@ class Camera2Controller(private val context: Context) {
|
||||
CaptureRequest.CONTROL_CAPTURE_INTENT_PREVIEW
|
||||
)
|
||||
applyBaseCameraSettings(this, isCapture = false)
|
||||
if (_state.value.isAutoExposure) {
|
||||
if (usesCameraAutoExposure(_state.value)) {
|
||||
set(
|
||||
CaptureRequest.CONTROL_AE_MODE,
|
||||
resolveSupportedAeMode(CaptureRequest.CONTROL_AE_MODE_ON)
|
||||
@@ -1823,7 +1835,7 @@ class Camera2Controller(private val context: Context) {
|
||||
CaptureRequest.CONTROL_CAPTURE_INTENT_PREVIEW
|
||||
)
|
||||
applyBaseCameraSettings(this, isCapture = false)
|
||||
if (_state.value.isAutoExposure) {
|
||||
if (usesCameraAutoExposure(_state.value)) {
|
||||
set(
|
||||
CaptureRequest.CONTROL_AE_MODE,
|
||||
resolveSupportedAeMode(CaptureRequest.CONTROL_AE_MODE_ON)
|
||||
@@ -1841,7 +1853,7 @@ class Camera2Controller(private val context: Context) {
|
||||
val torchRequest = torchRequestBuilder.build()
|
||||
|
||||
session.setRepeatingRequest(torchRequest, previewCallback, handler)
|
||||
multiFrameTorchWarmupNeedsAePrecapture = _state.value.isAutoExposure &&
|
||||
multiFrameTorchWarmupNeedsAePrecapture = usesCameraAutoExposure(_state.value) &&
|
||||
torchRequest.get(CaptureRequest.CONTROL_AE_MODE) != CaptureRequest.CONTROL_AE_MODE_OFF &&
|
||||
isAePrecaptureSupported()
|
||||
val timeout = Runnable {
|
||||
@@ -2042,9 +2054,12 @@ class Camera2Controller(private val context: Context) {
|
||||
|
||||
PLog.i(TAG, "选择默认摄像头: ${defaultCamera?.cameraId}, 类型: ${defaultCamera?.lensType}")
|
||||
|
||||
_state.value = _state.value.copy(
|
||||
val state = _state.value
|
||||
val selectedId = defaultCamera?.cameraId.orEmpty()
|
||||
val selectedState = if (state.currentCameraId == selectedId) state else state.withoutPhysicalAperture()
|
||||
_state.value = selectedState.copy(
|
||||
availableCameras = cameras,
|
||||
currentCameraId = defaultCamera?.cameraId ?: "",
|
||||
currentCameraId = selectedId,
|
||||
currentLensType = defaultCamera?.lensType ?: LensType.BACK_MAIN
|
||||
)
|
||||
}
|
||||
@@ -2113,6 +2128,11 @@ class Camera2Controller(private val context: Context) {
|
||||
cachedCharacteristicsCameraId = ""
|
||||
activeOpenCameraId = ""
|
||||
activeOutputPhysicalCameraId = null
|
||||
apertureCameraId = null
|
||||
apertureSelectedCameraId = ""
|
||||
lensAperture = LensApertureCapabilities()
|
||||
defaultPreviewAperture = null
|
||||
_state.update { it.withoutPhysicalAperture() }
|
||||
cachedSensorOrientation = 0
|
||||
cachedLensFacing = CameraCharacteristics.LENS_FACING_BACK
|
||||
cachedHardwareLevel = CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED
|
||||
@@ -3090,8 +3110,11 @@ class Camera2Controller(private val context: Context) {
|
||||
handler.post { calculateAutoMetering(totalWeight, weightedSumLuminance) }
|
||||
return
|
||||
}
|
||||
if (captureSession == null || stillFlashCaptureState != null || activeMultiFrameTorchWarmupGeneration != 0L) {
|
||||
return
|
||||
}
|
||||
val currentState = _state.value
|
||||
if (!currentState.isAutoExposure && (currentState.isIsoAuto || currentState.isShutterSpeedAuto)) {
|
||||
if (!usesCameraAutoExposure(currentState) && (currentState.isIsoAuto || currentState.isShutterSpeedAuto)) {
|
||||
|
||||
// --- 1. 计算亮度 ---
|
||||
val rawAvgLuminance = if (totalWeight > 0) weightedSumLuminance / totalWeight else 0.0
|
||||
@@ -3102,11 +3125,12 @@ class Camera2Controller(private val context: Context) {
|
||||
// --- 2. 按自动调整的参数选择测光域 ---
|
||||
val currentShutter = currentState.shutterSpeed
|
||||
val clampedPreviewTime = currentShutter.coerceAtMost(getMaxPreviewExposureTime(currentState))
|
||||
val aperturePriority = currentState.isIsoAuto && currentState.isShutterSpeedAuto
|
||||
// 手动快门、自动 ISO:以实际预览亮度为目标。超过预览快门上限后,
|
||||
// 不能用拍摄快门放大测光亮度,否则会持续降低 ISO,反而压暗取景器。
|
||||
// 手动 ISO、自动快门:仍需换算拍摄亮度,才能在预览曝光不再变化时
|
||||
// 求出正确的长曝光快门,而不是一直增加到传感器上限。
|
||||
val exposureRatio = if (currentState.isShutterSpeedAuto) {
|
||||
val exposureRatio = if (currentState.isShutterSpeedAuto && !aperturePriority) {
|
||||
currentShutter.toDouble() / clampedPreviewTime.toDouble()
|
||||
} else {
|
||||
1.0
|
||||
@@ -3139,7 +3163,12 @@ class Camera2Controller(private val context: Context) {
|
||||
var newShutter = currentState.shutterSpeed
|
||||
var needsUpdate = false
|
||||
|
||||
if (currentState.isIsoAuto) {
|
||||
if (aperturePriority) {
|
||||
val (iso, shutter) = meterAperturePriority(currentState, clampedPreviewTime, correctionFactor)
|
||||
newIso = iso
|
||||
newShutter = shutter
|
||||
needsUpdate = newIso != currentState.iso || newShutter != currentShutter
|
||||
} else if (currentState.isIsoAuto) {
|
||||
// 快门优先模式:快门固定,调 ISO
|
||||
val calculatedIso = (currentState.iso * correctionFactor).toInt()
|
||||
val range = currentState.getIsoRange()
|
||||
@@ -3166,7 +3195,8 @@ class Camera2Controller(private val context: Context) {
|
||||
if (needsUpdate) {
|
||||
PLog.d(
|
||||
TAG,
|
||||
"SEMI_AUTO_METERING isoAuto=${currentState.isIsoAuto} " +
|
||||
"SEMI_AUTO_METERING aperture=${currentState.requestedPhysicalAperture} " +
|
||||
"isoAuto=${currentState.isIsoAuto} shutterAuto=${currentState.isShutterSpeedAuto} " +
|
||||
"previewLuma=$rawAvgLuminance meteredLuma=$meteredLuminance " +
|
||||
"exposureRatio=$exposureRatio evError=$evErrorStops " +
|
||||
"previewShutterNs=$clampedPreviewTime " +
|
||||
@@ -3176,34 +3206,48 @@ class Camera2Controller(private val context: Context) {
|
||||
// 更新状态
|
||||
_state.value = currentState.copy(iso = newIso, shutterSpeed = newShutter)
|
||||
|
||||
// 关键修复:检查相机和会话是否仍然有效
|
||||
val device = cameraDevice
|
||||
val session = captureSession
|
||||
val builder = previewRequestBuilder
|
||||
|
||||
if (device == null || session == null || builder == null) {
|
||||
PLog.v(TAG, "calculateAutoMetering: camera not ready, skipping update")
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
applyExposureSettings(builder, _state.value, false)
|
||||
session.setRepeatingRequest(
|
||||
builder.build(),
|
||||
previewCallback,
|
||||
cameraHandler
|
||||
)
|
||||
} catch (e: CameraAccessException) {
|
||||
PLog.e(TAG, "Failed to update exposure: ${e.message}")
|
||||
} catch (e: IllegalStateException) {
|
||||
PLog.w(TAG, "Failed to update exposure - camera closed: ${e.message}")
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to update exposure: ${e.message}")
|
||||
previewRequestBuilder?.let { builder ->
|
||||
applyExposureSettings(builder, _state.value, isCapture = false)
|
||||
updatePreview()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun meterAperturePriority(
|
||||
state: CameraState,
|
||||
previewShutter: Long,
|
||||
correctionFactor: Double,
|
||||
): Pair<Int, Long> {
|
||||
val isoRange = state.getIsoRange()
|
||||
val shutterRange = getAperturePriorityShutterRange(state)
|
||||
val targetExposure = state.iso.toDouble() * previewShutter * correctionFactor
|
||||
|
||||
// Split the EV change between the automatic axes, then let the other axis
|
||||
// absorb the remainder when a sensor or frame-rate limit is reached.
|
||||
val balancedShutter = (previewShutter * correctionFactor.pow(0.5)).roundToLong()
|
||||
.coerceIn(shutterRange.lower, shutterRange.upper)
|
||||
val iso = (targetExposure / balancedShutter).roundToInt().coerceIn(isoRange.lower, isoRange.upper)
|
||||
val shutter = (targetExposure / iso).roundToLong().coerceIn(shutterRange.lower, shutterRange.upper)
|
||||
return iso to shutter
|
||||
}
|
||||
|
||||
private fun getAperturePriorityShutterRange(state: CameraState): Range<Long> {
|
||||
val range = state.getManualShutterSpeedRange()
|
||||
val limit = if (state.captureMode == CaptureMode.VIDEO) {
|
||||
1_000_000_000L / state.videoConfig.fps.fps
|
||||
} else {
|
||||
getMaxPreviewExposureTime(state)
|
||||
}
|
||||
return Range(range.lower, limit.coerceIn(range.lower, range.upper))
|
||||
}
|
||||
|
||||
private fun CameraState.withAperturePriorityShutterLimit(): CameraState {
|
||||
if (isApertureAuto || !isIsoAuto || !isShutterSpeedAuto || !supportsManualSensorExposure) return this
|
||||
val shutter = getAperturePriorityShutterRange(this).clamp(shutterSpeed)
|
||||
return if (shutter != shutterSpeed) copy(shutterSpeed = shutter) else this
|
||||
}
|
||||
|
||||
/** Recreate the consumer when its format or size changes, including EIS+ transitions. */
|
||||
private fun configureVideoPreviewSurface() {
|
||||
if (_state.value.captureMode != CaptureMode.VIDEO) return
|
||||
@@ -3282,6 +3326,14 @@ class Camera2Controller(private val context: Context) {
|
||||
|
||||
try {
|
||||
previewRequestBuilder = device.createCaptureRequest(templateType).apply {
|
||||
val selectedCameraId = _state.value.currentCameraId
|
||||
val previousLensId = apertureCameraId.takeIf { apertureSelectedCameraId == selectedCameraId }
|
||||
apertureSelectedCameraId = selectedCameraId
|
||||
refreshLensApertureCapabilities(
|
||||
activeOutputPhysicalCameraId ?: previousLensId ?: activeOpenCameraId,
|
||||
this,
|
||||
)
|
||||
defaultPreviewAperture = get(CaptureRequest.LENS_APERTURE)
|
||||
addTarget(surface)
|
||||
videoSurface?.takeIf { _state.value.videoRecordingState.shouldAttachCameraInput() }
|
||||
?.let(::addTarget)
|
||||
@@ -3956,6 +4008,7 @@ class Camera2Controller(private val context: Context) {
|
||||
// 开始预览
|
||||
// 关键修复: 不再动态添加 surface,因为已经在创建 builder 时添加了
|
||||
previewRequestBuilder?.let { builder ->
|
||||
applyPhysicalApertureSettings(builder, _state.value)
|
||||
session.setRepeatingRequest(builder.build(), previewCallback, cameraHandler)
|
||||
}
|
||||
if (shouldUseAlgorithmicStabilization()) {
|
||||
@@ -4137,6 +4190,7 @@ class Camera2Controller(private val context: Context) {
|
||||
}
|
||||
applyVendorCaptureSettings(builder, isCapture)
|
||||
applyCustomVendorCaptureRequestKeys(builder, isCapture)
|
||||
applyPhysicalApertureSettings(builder, currentState)
|
||||
if (!isCapture) {
|
||||
// Preview NR is a pipeline constraint, including custom request keys and RAW preview.
|
||||
applyNoiseReductionSettings(builder, currentState, isCapture = false)
|
||||
@@ -4154,6 +4208,85 @@ class Camera2Controller(private val context: Context) {
|
||||
}
|
||||
}
|
||||
|
||||
private fun refreshLensApertureCapabilities(cameraId: String, builder: CaptureRequest.Builder) {
|
||||
val characteristics = getCameraCharacteristicsOrNull(cameraId, "lens aperture")
|
||||
val logicalCharacteristics = getActiveOpenCameraCharacteristics()
|
||||
?.takeIf { cameraId != activeOpenCameraId && cameraId in it.physicalCameraIds }
|
||||
val supportsStandardControl = supportsManualSensorExposure &&
|
||||
isCaptureRequestKeyAvailable(CaptureRequest.LENS_APERTURE.name)
|
||||
val capabilities = LensApertureControl.readCapabilities(
|
||||
activeOpenCameraId, characteristics, logicalCharacteristics, builder, supportsStandardControl,
|
||||
)
|
||||
val sameLens = apertureCameraId == cameraId
|
||||
lensAperture = capabilities
|
||||
apertureCameraId = cameraId
|
||||
val control = capabilities.control
|
||||
val apertures = control?.apertures.orEmpty()
|
||||
_state.update {
|
||||
if (it.currentCameraId != apertureSelectedCameraId) return@update it
|
||||
it.copy(
|
||||
physicalAperture = it.physicalAperture.takeIf { sameLens } ?: capabilities.fixedAperture,
|
||||
availableApertures = apertures,
|
||||
requestedPhysicalAperture = requestedAperturesByCamera[cameraId]?.takeIf { it in apertures },
|
||||
).withAperturePriorityShutterLimit()
|
||||
}
|
||||
if (!sameLens) {
|
||||
PLog.d(TAG, "Aperture capabilities: camera=$cameraId, fixed=${capabilities.fixedAperture}, " +
|
||||
"available=$apertures, requiresManualExposure=${control?.requiresManualExposure}")
|
||||
}
|
||||
}
|
||||
|
||||
private fun updatePhysicalApertureFromResult(result: TotalCaptureResult) {
|
||||
val outputPhysicalId = activeOutputPhysicalCameraId
|
||||
val logicalPhysicalId = result.get(CaptureResult.LOGICAL_MULTI_CAMERA_ACTIVE_PHYSICAL_ID)
|
||||
val physicalId = outputPhysicalId ?: logicalPhysicalId
|
||||
val cameraId = physicalId ?: apertureCameraId ?: activeOpenCameraId
|
||||
val builder = previewRequestBuilder ?: return
|
||||
val lensChanged = cameraId != apertureCameraId
|
||||
val previousControl = lensAperture.control?.takeIf {
|
||||
lensChanged && (!it.requiresManualExposure || !_state.value.isApertureAuto)
|
||||
}
|
||||
if (lensChanged) {
|
||||
refreshLensApertureCapabilities(cameraId, builder)
|
||||
}
|
||||
|
||||
val logicalAperture = result.readPhysicalAperture(cameraId)
|
||||
val actualAperture = if (outputPhysicalId == null && logicalAperture != null) {
|
||||
logicalAperture
|
||||
} else {
|
||||
physicalId?.let { result.physicalCameraResults[it]?.readPhysicalAperture(it) }
|
||||
?: logicalAperture.takeIf {
|
||||
logicalPhysicalId == outputPhysicalId ||
|
||||
(logicalPhysicalId == null && lensAperture.fixedAperture == null)
|
||||
}
|
||||
}
|
||||
val state = _state.value
|
||||
if (actualAperture != null && actualAperture != state.physicalAperture) {
|
||||
_state.update {
|
||||
if (it.currentCameraId == apertureSelectedCameraId) {
|
||||
it.copy(physicalAperture = actualAperture)
|
||||
} else it
|
||||
}
|
||||
}
|
||||
|
||||
val hasApertureRequest = !state.isApertureAuto || lensAperture.control?.requiresManualExposure == false
|
||||
if (lensChanged && (previousControl != null || hasApertureRequest)) {
|
||||
previousControl?.clearFrom(builder, defaultPreviewAperture)
|
||||
if (previousControl != null) applyCustomVendorCaptureRequestKeys(builder, isCapture = false)
|
||||
updatePreviewExposureAndFlash()
|
||||
}
|
||||
}
|
||||
|
||||
private fun applyPhysicalApertureSettings(builder: CaptureRequest.Builder, state: CameraState) {
|
||||
if (state.currentCameraId != apertureSelectedCameraId) return
|
||||
val control = lensAperture.control ?: return
|
||||
val aperture = state.requestedPhysicalAperture?.takeIf { it in control.apertures }
|
||||
if (control.requiresManualExposure &&
|
||||
builder.get(CaptureRequest.CONTROL_AE_MODE) != CaptureRequest.CONTROL_AE_MODE_OFF
|
||||
) return
|
||||
control.applyTo(builder, aperture)
|
||||
}
|
||||
|
||||
private fun forcedVendorSessionParameterValues(state: CameraState): Map<VendorCaptureKey, Int> {
|
||||
val lensId = state.currentCameraId
|
||||
val settings = state.vendorCaptureSettingsByLens.settingsFor(lensId)
|
||||
@@ -4603,11 +4736,13 @@ class Camera2Controller(private val context: Context) {
|
||||
return isFlashSupported &&
|
||||
state.captureMode == CaptureMode.PHOTO &&
|
||||
state.flashMode == CameraMetadata.FLASH_MODE_SINGLE &&
|
||||
state.isIsoAuto &&
|
||||
state.isShutterSpeedAuto &&
|
||||
usesCameraAutoExposure(state) &&
|
||||
availableAeModes.contains(CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH)
|
||||
}
|
||||
|
||||
private fun usesCameraAutoExposure(state: CameraState): Boolean =
|
||||
state.isAutoExposure || !supportsManualSensorExposure
|
||||
|
||||
private fun applyExposureSettings(
|
||||
builder: CaptureRequest.Builder,
|
||||
state: CameraState,
|
||||
@@ -4621,52 +4756,22 @@ class Camera2Controller(private val context: Context) {
|
||||
val effectiveUseStillFlashAeMode = useStillFlashAeMode ||
|
||||
(!isCapture && shouldUsePreviewStillFlashAeMode(state))
|
||||
|
||||
// 根据曝光模式和闪光灯模式联合决定 AE_MODE
|
||||
val useCameraAe = usesCameraAutoExposure(state)
|
||||
val aeMode = when {
|
||||
// 1. 全自动曝光:静态闪光拍摄及其预览会话使用闪光 AE 模式;真正的预闪仍由
|
||||
// CONTROL_AE_PRECAPTURE_TRIGGER 或静态拍摄请求触发
|
||||
state.isIsoAuto && state.isShutterSpeedAuto -> {
|
||||
if (state.captureMode == CaptureMode.VIDEO) {
|
||||
resolveSupportedAeMode(CaptureRequest.CONTROL_AE_MODE_ON)
|
||||
} else {
|
||||
when {
|
||||
state.flashMode == CameraMetadata.FLASH_MODE_SINGLE &&
|
||||
effectiveUseStillFlashAeMode ->
|
||||
resolveStillFlashAeMode()
|
||||
|
||||
else -> resolveSupportedAeMode(CaptureRequest.CONTROL_AE_MODE_ON)
|
||||
}
|
||||
}
|
||||
}
|
||||
// 2. 手动曝光或半自动曝光:尝试使用 OFF 模式,如果设备不支持则退而求其次使用 ON
|
||||
else -> {
|
||||
if (isManualSensorSupported && availableAeModes.contains(CaptureRequest.CONTROL_AE_MODE_OFF)) {
|
||||
CaptureRequest.CONTROL_AE_MODE_OFF
|
||||
} else {
|
||||
resolveSupportedAeMode(CaptureRequest.CONTROL_AE_MODE_ON)
|
||||
}
|
||||
}
|
||||
!useCameraAe -> CaptureRequest.CONTROL_AE_MODE_OFF
|
||||
state.captureMode == CaptureMode.PHOTO && state.flashMode == CameraMetadata.FLASH_MODE_SINGLE &&
|
||||
effectiveUseStillFlashAeMode -> resolveStillFlashAeMode()
|
||||
else -> resolveSupportedAeMode(CaptureRequest.CONTROL_AE_MODE_ON)
|
||||
}
|
||||
|
||||
builder.set(CaptureRequest.CONTROL_AE_MODE, aeMode)
|
||||
|
||||
// 如果是全自动曝光,设置曝光补偿
|
||||
if (state.isIsoAuto && state.isShutterSpeedAuto) {
|
||||
if (useCameraAe) {
|
||||
builder.set(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, state.exposureCompensation)
|
||||
} else {
|
||||
// 手动曝光 / 半自动曝光:手动设置 ISO 和快门
|
||||
// 只有在支持 MANUAL_SENSOR 的设备上才设置,否则保持自动
|
||||
if (isManualSensorSupported) {
|
||||
builder.set(CaptureRequest.SENSOR_SENSITIVITY, state.iso)
|
||||
|
||||
// 预览时限制曝光时间,防止画面卡死;拍摄时使用完整的用户设置
|
||||
val exposureTime = if (isCapture) {
|
||||
state.shutterSpeed
|
||||
} else {
|
||||
coercePreviewExposureTime(state)
|
||||
}
|
||||
builder.set(CaptureRequest.SENSOR_EXPOSURE_TIME, exposureTime)
|
||||
}
|
||||
builder.set(CaptureRequest.SENSOR_SENSITIVITY, state.iso)
|
||||
val exposureTime = if (isCapture) state.shutterSpeed else coercePreviewExposureTime(state)
|
||||
builder.set(CaptureRequest.SENSOR_EXPOSURE_TIME, exposureTime)
|
||||
|
||||
// 拍摄时设置低帧率范围以支持长曝光
|
||||
if (isCapture && state.captureMode == CaptureMode.PHOTO) {
|
||||
@@ -4737,7 +4842,8 @@ class Camera2Controller(private val context: Context) {
|
||||
}
|
||||
|
||||
private fun shouldApplyRawMinShutterLimit(state: CameraState): Boolean {
|
||||
return state.captureMode == CaptureMode.PHOTO &&
|
||||
return supportsManualSensorExposure &&
|
||||
state.captureMode == CaptureMode.PHOTO &&
|
||||
state.rawMinShutterSpeedNs > 0L &&
|
||||
state.isIsoAuto &&
|
||||
state.isShutterSpeedAuto &&
|
||||
@@ -4984,7 +5090,7 @@ class Camera2Controller(private val context: Context) {
|
||||
|
||||
when (state.flashMode) {
|
||||
CameraMetadata.FLASH_MODE_SINGLE -> {
|
||||
if (!state.isIsoAuto || !state.isShutterSpeedAuto) {
|
||||
if (!usesCameraAutoExposure(state)) {
|
||||
// Manual/semi-manual exposure has no AE precapture convergence.
|
||||
// Fire a single flash only for the still request.
|
||||
builder.set(
|
||||
@@ -5638,11 +5744,7 @@ class Camera2Controller(private val context: Context) {
|
||||
|
||||
targetCamera?.let { cam ->
|
||||
PLog.d(TAG, "Switching to lens: $lensType, cameraId: ${cam.cameraId}")
|
||||
_state.value = _state.value.copy(
|
||||
currentCameraId = cam.cameraId,
|
||||
currentLensType = cam.lensType,
|
||||
zoomRatio = 1f
|
||||
)
|
||||
selectCamera(cam)
|
||||
} ?: PLog.w(TAG, "Camera with lens type $lensType not found")
|
||||
}
|
||||
|
||||
@@ -5655,16 +5757,91 @@ class Camera2Controller(private val context: Context) {
|
||||
|
||||
targetCamera?.let { cam ->
|
||||
PLog.d(TAG, "Switching to camera ID: $cameraId")
|
||||
_state.value = _state.value.copy(
|
||||
currentCameraId = cam.cameraId,
|
||||
currentLensType = cam.lensType,
|
||||
zoomRatio = 1f
|
||||
)
|
||||
selectCamera(cam)
|
||||
} ?: PLog.w(TAG, "Camera with ID $cameraId not found")
|
||||
}
|
||||
|
||||
private fun selectCamera(camera: CameraInfo) {
|
||||
_state.update { state ->
|
||||
val selected = if (state.currentCameraId == camera.cameraId) state else state.withoutPhysicalAperture()
|
||||
selected.copy(currentCameraId = camera.cameraId, currentLensType = camera.lensType, zoomRatio = 1f)
|
||||
}
|
||||
}
|
||||
|
||||
private fun CameraState.withoutPhysicalAperture(): CameraState = copy(
|
||||
physicalAperture = null,
|
||||
availableApertures = emptyList(),
|
||||
requestedPhysicalAperture = null,
|
||||
)
|
||||
|
||||
// ==================== 曝光控制 ====================
|
||||
|
||||
fun setPhysicalAperture(aperture: Float) = changePhysicalAperture { aperture }
|
||||
|
||||
fun setPhysicalApertureAuto(enabled: Boolean) = changePhysicalAperture { state ->
|
||||
if (enabled) {
|
||||
null
|
||||
} else {
|
||||
state.requestedPhysicalAperture ?: state.availableApertures.minByOrNull { aperture ->
|
||||
abs(aperture - (state.physicalAperture ?: aperture))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun changePhysicalAperture(select: (CameraState) -> Float?) {
|
||||
val handler = cameraHandler ?: return
|
||||
if (Looper.myLooper() != handler.looper) {
|
||||
handler.post { changePhysicalAperture(select) }
|
||||
return
|
||||
}
|
||||
val state = _state.value
|
||||
if (apertureSelectedCameraId != state.currentCameraId || !state.isApertureAdjustable) return
|
||||
val aperture = select(state)
|
||||
if (aperture == state.requestedPhysicalAperture ||
|
||||
(aperture != null && aperture !in state.availableApertures)
|
||||
) return
|
||||
|
||||
val previousAperture = state.requestedPhysicalAperture ?: state.physicalAperture
|
||||
val exposureBias = if (!state.isIsoAuto && !state.isShutterSpeedAuto &&
|
||||
aperture != null && previousAperture != null
|
||||
) {
|
||||
state.exposureBias + (2.0 * ln(previousAperture.toDouble() / aperture) / ln(2.0)).toFloat()
|
||||
} else {
|
||||
state.exposureCompensation * state.getExposureCompensationStep()
|
||||
}
|
||||
rememberPhysicalAperture(aperture)
|
||||
_state.update {
|
||||
if (it.currentCameraId != state.currentCameraId) return@update it
|
||||
it.copy(
|
||||
requestedPhysicalAperture = aperture,
|
||||
exposureBias = exposureBias,
|
||||
).withAperturePriorityShutterLimit()
|
||||
}
|
||||
PLog.d(TAG, "Aperture selected: camera=$apertureCameraId, requested=$aperture, actual=${state.physicalAperture}")
|
||||
updatePreviewExposureAndFlash()
|
||||
}
|
||||
|
||||
private fun rememberPhysicalAperture(aperture: Float?) {
|
||||
val cameraId = apertureCameraId ?: return
|
||||
if (aperture != null) {
|
||||
requestedAperturesByCamera[cameraId] = aperture
|
||||
} else {
|
||||
requestedAperturesByCamera.remove(cameraId)
|
||||
previewRequestBuilder?.let { builder ->
|
||||
lensAperture.control?.clearFrom(builder, defaultPreviewAperture)
|
||||
applyCustomVendorCaptureRequestKeys(builder, isCapture = false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun updatePreviewExposureAndFlash() {
|
||||
val builder = previewRequestBuilder ?: return
|
||||
val state = _state.value
|
||||
applyExposureSettings(builder, state, isCapture = false)
|
||||
applyFlashSettings(builder, state, isCapture = false, useStillFlashTrigger = false)
|
||||
updatePreview()
|
||||
}
|
||||
|
||||
/** Serialize user changes and their snapshots with the asynchronous focus controls. */
|
||||
fun updateUserCaptureSettings(
|
||||
update: Camera2Controller.() -> Unit,
|
||||
@@ -5722,10 +5899,8 @@ class Camera2Controller(private val context: Context) {
|
||||
(settings.exposureCompensationEv / evStep).roundToInt()
|
||||
.coerceIn(evRange.lower, evRange.upper)
|
||||
} else 0
|
||||
val supportsManualExposure = isManualSensorSupported &&
|
||||
availableAeModes.contains(CaptureRequest.CONTROL_AE_MODE_OFF)
|
||||
val isoAuto = settings.isIsoAuto || !supportsManualExposure
|
||||
val shutterAuto = settings.isShutterSpeedAuto || !supportsManualExposure
|
||||
val isoAuto = settings.isIsoAuto || !supportsManualSensorExposure
|
||||
val shutterAuto = settings.isShutterSpeedAuto || !supportsManualSensorExposure
|
||||
val isoRange = current.getIsoRange()
|
||||
val restoredIso = settings.iso.coerceIn(isoRange.lower, isoRange.upper)
|
||||
val restoredShutter = coerceManualShutterSpeed(current, settings.shutterSpeedNs)
|
||||
@@ -5870,16 +6045,16 @@ class Camera2Controller(private val context: Context) {
|
||||
val evStep = currentState.getExposureCompensationStep()
|
||||
val sliderBias = currentState.exposureCompensation * evStep
|
||||
|
||||
if (enabled && !currentState.isApertureAuto && apertureSelectedCameraId == currentState.currentCameraId) {
|
||||
rememberPhysicalAperture(null)
|
||||
}
|
||||
_state.value = currentState.copy(
|
||||
isIsoAuto = enabled,
|
||||
isShutterSpeedAuto = enabled,
|
||||
requestedPhysicalAperture = currentState.requestedPhysicalAperture.takeUnless { enabled },
|
||||
exposureBias = if (enabled) sliderBias else currentState.exposureBias
|
||||
)
|
||||
|
||||
previewRequestBuilder?.apply {
|
||||
applyExposureSettings(this, _state.value, false)
|
||||
updatePreview()
|
||||
}
|
||||
updatePreviewExposureAndFlash()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -5897,7 +6072,7 @@ class Camera2Controller(private val context: Context) {
|
||||
_state.value = currentState.copy(
|
||||
isIsoAuto = enabled,
|
||||
exposureBias = exposureBias
|
||||
)
|
||||
).withAperturePriorityShutterLimit()
|
||||
previewRequestBuilder?.apply {
|
||||
applyExposureSettings(this, _state.value, false)
|
||||
updatePreview()
|
||||
@@ -5919,7 +6094,7 @@ class Camera2Controller(private val context: Context) {
|
||||
_state.value = currentState.copy(
|
||||
isShutterSpeedAuto = enabled,
|
||||
exposureBias = exposureBias
|
||||
)
|
||||
).withAperturePriorityShutterLimit()
|
||||
previewRequestBuilder?.apply {
|
||||
applyExposureSettings(this, _state.value, false)
|
||||
updatePreview()
|
||||
@@ -6345,9 +6520,9 @@ class Camera2Controller(private val context: Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// The flash sequence owns repeating requests until the still capture is finished.
|
||||
// Flash preparation owns repeating requests until the sequence is finished.
|
||||
// Parameter setters may update the preview builder; apply them when restoring preview.
|
||||
if (stillFlashCaptureState != null) return
|
||||
if (stillFlashCaptureState != null || activeMultiFrameTorchWarmupGeneration != 0L) return
|
||||
|
||||
// 检查相机和会话是否仍然有效,避免在相机关闭后的回调中调用 setRepeatingRequest。
|
||||
val device = cameraDevice
|
||||
@@ -6360,6 +6535,7 @@ class Camera2Controller(private val context: Context) {
|
||||
}
|
||||
|
||||
try {
|
||||
applyPhysicalApertureSettings(builder, _state.value)
|
||||
applyFrozenCaptureFocus(builder)
|
||||
session.setRepeatingRequest(builder.build(), previewCallback, cameraHandler)
|
||||
} catch (e: CameraAccessException) {
|
||||
@@ -7029,7 +7205,7 @@ class Camera2Controller(private val context: Context) {
|
||||
}
|
||||
|
||||
fun setVideoFps(fps: VideoFpsPreset) {
|
||||
_state.update { it.copy(videoConfig = it.videoConfig.copy(fps = fps)) }
|
||||
_state.update { it.copy(videoConfig = it.videoConfig.copy(fps = fps)).withAperturePriorityShutterLimit() }
|
||||
refreshVideoCapabilities()
|
||||
previewRequestBuilder?.apply {
|
||||
applyBaseCameraSettings(this, isCapture = false)
|
||||
@@ -7482,13 +7658,16 @@ class Camera2Controller(private val context: Context) {
|
||||
val videoTarget = videoRecorder.cameraInputSurface
|
||||
if (includeVideoSurface && videoTarget == null) return false
|
||||
return try {
|
||||
val builder = device.createCaptureRequest(CameraDevice.TEMPLATE_RECORD).apply {
|
||||
val builder = device.createCaptureRequest(CameraDevice.TEMPLATE_RECORD)
|
||||
val defaultAperture = builder.get(CaptureRequest.LENS_APERTURE)
|
||||
builder.apply {
|
||||
addTarget(previewTarget)
|
||||
if (includeVideoSurface) videoTarget?.let(::addTarget)
|
||||
applyBaseCameraSettings(this, isCapture = false)
|
||||
}
|
||||
session.setRepeatingRequest(builder.build(), previewCallback, cameraHandler)
|
||||
previewRequestBuilder = builder
|
||||
defaultPreviewAperture = defaultAperture
|
||||
true
|
||||
} catch (error: Exception) {
|
||||
PLog.e(TAG, "Failed to update OES recording target", error)
|
||||
@@ -7677,8 +7856,7 @@ class Camera2Controller(private val context: Context) {
|
||||
isFlashSupported &&
|
||||
isAePrecaptureSupported() &&
|
||||
currentState.flashMode == CameraMetadata.FLASH_MODE_SINGLE &&
|
||||
currentState.isIsoAuto &&
|
||||
currentState.isShutterSpeedAuto &&
|
||||
usesCameraAutoExposure(currentState) &&
|
||||
resolveStillFlashAeMode() != CaptureRequest.CONTROL_AE_MODE_OFF
|
||||
|
||||
if (useMultiFrameTorch) {
|
||||
@@ -7808,7 +7986,8 @@ class Camera2Controller(private val context: Context) {
|
||||
builder = this,
|
||||
isCapture = true,
|
||||
isRawCapture = false,
|
||||
disableZslForHdrCapture = true
|
||||
disableZslForHdrCapture = true,
|
||||
currentState = currentState,
|
||||
)
|
||||
applyHdrBracketExposure(this, currentState, evOffset, manualBaseExposure)
|
||||
setAePrecaptureTriggerIfSupported(
|
||||
@@ -7869,12 +8048,12 @@ class Camera2Controller(private val context: Context) {
|
||||
if (!isManualSensorSupported || !availableAeModes.contains(CaptureRequest.CONTROL_AE_MODE_OFF)) {
|
||||
return null
|
||||
}
|
||||
val baseIso = if (state.isAutoExposure) {
|
||||
val baseIso = if (usesCameraAutoExposure(state)) {
|
||||
baseExposureResult?.get(CaptureResult.SENSOR_SENSITIVITY)
|
||||
} else {
|
||||
state.iso
|
||||
} ?: return null
|
||||
val baseShutter = if (state.isAutoExposure) {
|
||||
val baseShutter = if (usesCameraAutoExposure(state)) {
|
||||
baseExposureResult?.get(CaptureResult.SENSOR_EXPOSURE_TIME)
|
||||
} else {
|
||||
state.shutterSpeed
|
||||
@@ -8063,7 +8242,7 @@ class Camera2Controller(private val context: Context) {
|
||||
): CaptureRequest {
|
||||
return device.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE).apply {
|
||||
addTarget(reader.surface)
|
||||
applyBaseCameraSettings(this, isCapture = true, isRawCapture = isRawCapture)
|
||||
applyBaseCameraSettings(this, isCapture = true, isRawCapture = isRawCapture, currentState = state)
|
||||
setAePrecaptureTriggerIfSupported(
|
||||
this,
|
||||
CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_IDLE,
|
||||
@@ -8167,7 +8346,7 @@ class Camera2Controller(private val context: Context) {
|
||||
): CaptureRequest? {
|
||||
val builder = device.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE).apply {
|
||||
addTarget(reader.surface)
|
||||
applyBaseCameraSettings(this, isCapture = true, isRawCapture = isRawCapture)
|
||||
applyBaseCameraSettings(this, isCapture = true, isRawCapture = isRawCapture, currentState = state)
|
||||
setAePrecaptureTriggerIfSupported(
|
||||
this,
|
||||
CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_IDLE,
|
||||
@@ -8362,7 +8541,7 @@ class Camera2Controller(private val context: Context) {
|
||||
}
|
||||
|
||||
if (useMultiFrameTorch) {
|
||||
if (currentState.isAutoExposure) {
|
||||
if (usesCameraAutoExposure(currentState)) {
|
||||
set(
|
||||
CaptureRequest.CONTROL_AE_MODE,
|
||||
resolveSupportedAeMode(CaptureRequest.CONTROL_AE_MODE_ON)
|
||||
@@ -8617,17 +8796,17 @@ class Camera2Controller(private val context: Context) {
|
||||
val openCameraId = getCurrentOpenCameraId()
|
||||
val zoomRatio = captureState.zoomRatio
|
||||
|
||||
// 从 CameraCharacteristics 获取镜头固定信息
|
||||
var aperture: Float? = null
|
||||
var fixedAperture: Float? = null
|
||||
var characteristicsForMetadata: CameraCharacteristics? = null
|
||||
|
||||
try {
|
||||
val characteristics = effectiveCharacteristics ?: getCameraCharacteristicsCached(openCameraId)
|
||||
characteristicsForMetadata = characteristics
|
||||
|
||||
// 光圈值(取第一个可用光圈)
|
||||
val apertures = characteristics.get(CameraCharacteristics.LENS_INFO_AVAILABLE_APERTURES)
|
||||
aperture = apertures?.firstOrNull()
|
||||
fixedAperture = characteristics
|
||||
.readMetadataOrNull(CameraCharacteristics.LENS_INFO_AVAILABLE_APERTURES)
|
||||
?.singleOrNull()
|
||||
?.takeIf { it.isFinite() && it > 0f }
|
||||
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to get camera characteristics for EXIF", e)
|
||||
@@ -8640,8 +8819,11 @@ class Camera2Controller(private val context: Context) {
|
||||
val whiteBalance = if (awbModeForExif == CameraMetadata.CONTROL_AWB_MODE_AUTO) 0 else 1
|
||||
val flashState = result?.get(CaptureResult.FLASH_STATE) ?: captureState.flashMode
|
||||
|
||||
// 如果有实时的光圈/焦距,使用实时值
|
||||
result?.get(CaptureResult.LENS_APERTURE)?.let { aperture = it }
|
||||
val aperture = result?.readPhysicalAperture() ?: when {
|
||||
effectiveCharacteristics == null -> captureState.physicalAperture ?: fixedAperture
|
||||
captureState.isApertureAdjustable -> null
|
||||
else -> fixedAperture
|
||||
}
|
||||
val resolvedFocalLength = resolveCaptureFocalLength(
|
||||
characteristics = characteristicsForMetadata,
|
||||
captureResultFocalLength = result?.get(CaptureResult.LENS_FOCAL_LENGTH),
|
||||
@@ -9212,7 +9394,7 @@ class Camera2Controller(private val context: Context) {
|
||||
applyBaseCameraSettings(this, isCapture = true, isRawCapture = isRawCapture)
|
||||
|
||||
if (isContinuousBurstTorchActive) {
|
||||
if (_state.value.isAutoExposure) {
|
||||
if (usesCameraAutoExposure(_state.value)) {
|
||||
set(
|
||||
CaptureRequest.CONTROL_AE_MODE,
|
||||
resolveSupportedAeMode(CaptureRequest.CONTROL_AE_MODE_ON)
|
||||
|
||||
@@ -307,8 +307,10 @@ data class CameraState(
|
||||
// 测光模式
|
||||
val meteringMode: MeteringMode = MeteringMode.SYSTEM_DEFAULT,
|
||||
|
||||
val physicalAperture: Float? = null,
|
||||
val requestedPhysicalAperture: Float? = null,
|
||||
val availableApertures: List<Float> = emptyList(),
|
||||
val isVirtualApertureEnabled: Boolean = false,
|
||||
val physicalAperture: Float = 2.0f, // 物理光圈值
|
||||
val virtualAperture: Float = 2.0f, // 虚拟光圈值 (f-number)
|
||||
// 对焦
|
||||
val isAutoFocus: Boolean = true,
|
||||
@@ -410,6 +412,12 @@ data class CameraState(
|
||||
val isProfessionalMode: Boolean
|
||||
get() = captureMode == CaptureMode.PHOTO && useRaw && isRawSupported
|
||||
|
||||
val isApertureAdjustable: Boolean
|
||||
get() = availableApertures.size > 1
|
||||
|
||||
val isApertureAuto: Boolean
|
||||
get() = requestedPhysicalAperture == null
|
||||
|
||||
val meteringReferencePoint: Pair<Float, Float>?
|
||||
get() = meteringPoint ?: focusPoint?.takeIf { focusPointSource == FocusPointSource.MANUAL }
|
||||
|
||||
@@ -422,7 +430,7 @@ data class CameraState(
|
||||
* 是否全自动曝光
|
||||
*/
|
||||
val isAutoExposure: Boolean
|
||||
get() = isIsoAuto && isShutterSpeedAuto
|
||||
get() = isIsoAuto && isShutterSpeedAuto && isApertureAuto
|
||||
|
||||
val isMultiFrameEnabled: Boolean
|
||||
get() = multiFrameOutputScale != null &&
|
||||
|
||||
@@ -0,0 +1,202 @@
|
||||
package com.hinnka.mycamera.camera
|
||||
|
||||
import android.hardware.camera2.CameraCharacteristics
|
||||
import android.hardware.camera2.CaptureRequest
|
||||
import android.hardware.camera2.CaptureResult
|
||||
import android.os.Build
|
||||
import com.hinnka.mycamera.utils.PLog
|
||||
|
||||
internal fun CaptureResult.readPhysicalAperture(cameraId: String? = null): Float? =
|
||||
readMetadataOrNull(CaptureResult.LENS_APERTURE, cameraId)?.takeIf { it.isFinite() && it > 0f }
|
||||
|
||||
internal data class LensApertureCapabilities(
|
||||
val fixedAperture: Float? = null,
|
||||
val control: LensApertureControl? = null,
|
||||
)
|
||||
|
||||
internal class LensApertureControl private constructor(
|
||||
val apertures: List<Float>,
|
||||
private val writesStandardAperture: Boolean,
|
||||
private val xiaomi: XiaomiApertureKeys? = null,
|
||||
) {
|
||||
val requiresManualExposure: Boolean
|
||||
get() = xiaomi == null
|
||||
|
||||
fun applyTo(builder: CaptureRequest.Builder, aperture: Float?) {
|
||||
if (writesStandardAperture && aperture != null) {
|
||||
builder.set(CaptureRequest.LENS_APERTURE, aperture)
|
||||
}
|
||||
xiaomi?.let { keys ->
|
||||
if (keys.hasClientName) builder.set(XIAOMI_CLIENT_NAME, XIAOMI_CAMERA_CLIENT)
|
||||
keys.mode.set(builder, if (aperture == null) XIAOMI_APERTURE_AUTO else XIAOMI_APERTURE_MANUAL)
|
||||
if (keys.hasFrameAperture) builder.set(XIAOMI_FRAME_APERTURE, aperture ?: keys.autoFrameAperture)
|
||||
if (keys.hasInitialAperture) builder.set(XIAOMI_INIT_APERTURE, aperture)
|
||||
}
|
||||
}
|
||||
|
||||
fun clearFrom(builder: CaptureRequest.Builder, defaultAperture: Float?) {
|
||||
if (writesStandardAperture) {
|
||||
builder.set(CaptureRequest.LENS_APERTURE, defaultAperture)
|
||||
}
|
||||
xiaomi?.let { keys ->
|
||||
if (keys.hasFrameAperture) builder.set(XIAOMI_FRAME_APERTURE, null)
|
||||
if (keys.hasInitialAperture) builder.set(XIAOMI_INIT_APERTURE, null)
|
||||
keys.mode.set(builder, null)
|
||||
if (keys.hasClientName) builder.set(XIAOMI_CLIENT_NAME, null)
|
||||
}
|
||||
}
|
||||
|
||||
private data class XiaomiApertureKeys(
|
||||
val mode: XiaomiApertureMode,
|
||||
val hasFrameAperture: Boolean,
|
||||
val hasInitialAperture: Boolean,
|
||||
val hasClientName: Boolean,
|
||||
val autoFrameAperture: Float?,
|
||||
)
|
||||
|
||||
private enum class XiaomiApertureMode {
|
||||
BYTE,
|
||||
INT;
|
||||
|
||||
fun set(builder: CaptureRequest.Builder, value: Int?) {
|
||||
when (this) {
|
||||
BYTE -> builder.set(XIAOMI_APERTURE_MODE_BYTE, value?.toByte())
|
||||
INT -> builder.set(XIAOMI_APERTURE_MODE_INT, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "LensApertureControl"
|
||||
private const val XIAOMI_APERTURE_AUTO = 0
|
||||
private const val XIAOMI_APERTURE_MANUAL = 1
|
||||
private const val XIAOMI_CAMERA_CLIENT = "com.android.camera"
|
||||
private val XIAOMI_14_ULTRA_MODELS = listOf("Xiaomi 14 Ultra", "24030PN60G", "24030PN60C", "24031PN0DC")
|
||||
private val XIAOMI_AVAILABLE_APERTURES = CameraCharacteristics.Key(
|
||||
"com.xiaomi.lens.info.availableApertures", Array<Float>::class.java,
|
||||
)
|
||||
private val XIAOMI_INIT_APERTURE = CaptureRequest.Key(
|
||||
"com.xiaomi.sessionparams.initAperture", Float::class.javaObjectType,
|
||||
)
|
||||
private val XIAOMI_FRAME_APERTURE = CaptureRequest.Key(
|
||||
"com.xiaomi.lens.aperture", Float::class.javaObjectType,
|
||||
)
|
||||
private val XIAOMI_APERTURE_MODE_BYTE = CaptureRequest.Key(
|
||||
"com.xiaomi.lens.apertureMode", Byte::class.javaObjectType,
|
||||
)
|
||||
private val XIAOMI_APERTURE_MODE_INT = CaptureRequest.Key(
|
||||
"com.xiaomi.lens.apertureMode", Int::class.javaObjectType,
|
||||
)
|
||||
private val XIAOMI_CLIENT_NAME = CaptureRequest.Key(
|
||||
"com.xiaomi.sessionparams.clientName", String::class.java,
|
||||
)
|
||||
|
||||
fun readCapabilities(
|
||||
openCameraId: String,
|
||||
characteristics: CameraCharacteristics?,
|
||||
logicalCharacteristics: CameraCharacteristics?,
|
||||
requestBuilder: CaptureRequest.Builder,
|
||||
supportsStandardControl: Boolean,
|
||||
): LensApertureCapabilities {
|
||||
if (characteristics == null) return LensApertureCapabilities()
|
||||
val standardApertures = characteristics
|
||||
.readMetadataOrNull(CameraCharacteristics.LENS_INFO_AVAILABLE_APERTURES)
|
||||
?.asIterable()
|
||||
.validApertures()
|
||||
|
||||
if (Build.BRAND.equals("xiaomi", ignoreCase = true) ||
|
||||
Build.MANUFACTURER.equals("xiaomi", ignoreCase = true)
|
||||
) {
|
||||
val vendorApertures = characteristics.readXiaomiApertures().ifEmpty {
|
||||
// Some HALs expose the primary lens's vendor metadata only on the logical ID.
|
||||
logicalCharacteristics
|
||||
?.takeIf { characteristics.sharesPrimaryLensWith(it) }
|
||||
?.readXiaomiApertures().orEmpty()
|
||||
}
|
||||
if (vendorApertures.size > 1) {
|
||||
readXiaomiControl(openCameraId, requestBuilder, vendorApertures)?.let {
|
||||
return LensApertureCapabilities(control = it)
|
||||
}
|
||||
if (standardApertures.size <= 1) return LensApertureCapabilities()
|
||||
}
|
||||
}
|
||||
|
||||
return LensApertureCapabilities(
|
||||
fixedAperture = standardApertures.singleOrNull(),
|
||||
control = if (supportsStandardControl && standardApertures.size > 1) {
|
||||
LensApertureControl(standardApertures, writesStandardAperture = true)
|
||||
} else {
|
||||
null
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
private fun readXiaomiControl(
|
||||
openCameraId: String,
|
||||
builder: CaptureRequest.Builder,
|
||||
apertures: List<Float>,
|
||||
): LensApertureControl? {
|
||||
val firstAperture = apertures.first()
|
||||
val is14UltraMainCamera = (openCameraId == "0" || openCameraId == "2") &&
|
||||
(Build.DEVICE.equals("aurora", ignoreCase = true) ||
|
||||
XIAOMI_14_ULTRA_MODELS.any { Build.MODEL.equals(it, ignoreCase = true) })
|
||||
val hasFrameAperture = builder.supportsApertureKey(XIAOMI_FRAME_APERTURE, firstAperture)
|
||||
val mode = when {
|
||||
builder.supportsApertureKey(XIAOMI_APERTURE_MODE_BYTE, XIAOMI_APERTURE_MANUAL.toByte()) ->
|
||||
XiaomiApertureMode.BYTE
|
||||
builder.supportsApertureKey(XIAOMI_APERTURE_MODE_INT, XIAOMI_APERTURE_MANUAL) ->
|
||||
XiaomiApertureMode.INT
|
||||
else -> return null
|
||||
}
|
||||
val hasClientName = builder.supportsApertureKey(XIAOMI_CLIENT_NAME, XIAOMI_CAMERA_CLIENT)
|
||||
// mcpro24fps 043de drives the iris on ID 0 with the per-frame Float key,
|
||||
// Byte mode and client name. Auto is mode 0 with an explicit Float 0.
|
||||
val use14UltraFrameAperture = is14UltraMainCamera && openCameraId == "0" &&
|
||||
hasFrameAperture && mode == XiaomiApertureMode.BYTE && hasClientName
|
||||
val hasInitialAperture = !use14UltraFrameAperture && (is14UltraMainCamera || !hasFrameAperture) &&
|
||||
builder.supportsApertureKey(XIAOMI_INIT_APERTURE, firstAperture)
|
||||
if (!hasFrameAperture && !hasInitialAperture) return null
|
||||
|
||||
val writesStandardAperture = !use14UltraFrameAperture && (is14UltraMainCamera || hasInitialAperture) &&
|
||||
builder.supportsApertureKey(CaptureRequest.LENS_APERTURE, firstAperture)
|
||||
val autoFrameAperture = if (use14UltraFrameAperture) 0.0f else null
|
||||
PLog.i(TAG, "Xiaomi aperture request: camera=$openCameraId, mode=$mode, " +
|
||||
"frame=$hasFrameAperture, init=$hasInitialAperture, standard=$writesStandardAperture, " +
|
||||
"clientName=$hasClientName, autoFrame=$autoFrameAperture")
|
||||
return LensApertureControl(
|
||||
apertures, writesStandardAperture,
|
||||
XiaomiApertureKeys(mode, hasFrameAperture, hasInitialAperture, hasClientName, autoFrameAperture),
|
||||
)
|
||||
}
|
||||
|
||||
private fun Iterable<Float>?.validApertures(): List<Float> =
|
||||
this?.filter { it.isFinite() && it > 0f }?.distinct()?.sorted().orEmpty()
|
||||
|
||||
private fun CameraCharacteristics.readXiaomiApertures(): List<Float> = try {
|
||||
readMetadataOrNull(XIAOMI_AVAILABLE_APERTURES)?.asIterable().validApertures()
|
||||
} catch (e: RuntimeException) {
|
||||
PLog.d(TAG, "Xiaomi aperture characteristics unavailable: ${e.message}")
|
||||
emptyList()
|
||||
}
|
||||
|
||||
private fun CameraCharacteristics.sharesPrimaryLensWith(logical: CameraCharacteristics): Boolean {
|
||||
val focalLength = readMetadataOrNull(CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS)
|
||||
?.singleOrNull() ?: return false
|
||||
return focalLength == logical
|
||||
.readMetadataOrNull(CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS)
|
||||
?.singleOrNull()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun <T> CaptureRequest.Builder.supportsApertureKey(key: CaptureRequest.Key<T>, value: T): Boolean =
|
||||
try {
|
||||
// A defined but unset tag can be read even if its vendor type cannot marshal this value.
|
||||
val original = get(key)
|
||||
set(key, value)
|
||||
set(key, original)
|
||||
true
|
||||
} catch (e: RuntimeException) {
|
||||
PLog.d("LensApertureControl", "Aperture request key ${key.name} unavailable: ${e.message}")
|
||||
false
|
||||
}
|
||||
@@ -11,11 +11,13 @@ import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.draw.rotate
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalConfiguration
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.semantics.Role
|
||||
import androidx.compose.ui.semantics.selected
|
||||
@@ -30,6 +32,7 @@ import com.hinnka.mycamera.R
|
||||
import com.hinnka.mycamera.camera.CameraState
|
||||
import com.hinnka.mycamera.camera.CameraUtils
|
||||
import com.hinnka.mycamera.video.CaptureMode
|
||||
import java.text.NumberFormat
|
||||
import kotlin.math.roundToLong
|
||||
|
||||
|
||||
@@ -68,7 +71,7 @@ internal fun cameraParameterItems(state: CameraState): List<CameraParameterItem>
|
||||
// Match the mode switcher: PHOTO capture also backs the RAW professional mode.
|
||||
val showValues = state.captureMode == CaptureMode.VIDEO ||
|
||||
(state.useRaw && state.isRawSupported)
|
||||
return listOf(
|
||||
return listOfNotNull(
|
||||
CameraParameterItem(
|
||||
CameraParameter.EXPOSURE_COMPENSATION,
|
||||
stringResource(R.string.camera_parameter_exposure),
|
||||
@@ -83,6 +86,13 @@ internal fun cameraParameterItems(state: CameraState): List<CameraParameterItem>
|
||||
if (showValues) formatShutterSpeedValue(state.shutterSpeed) else null,
|
||||
isWarning = showValues && state.isPreviewExposureLimited()
|
||||
),
|
||||
if (state.isApertureAdjustable) {
|
||||
CameraParameterItem(
|
||||
CameraParameter.APERTURE,
|
||||
stringResource(R.string.camera_parameter_aperture),
|
||||
if (showValues) state.physicalAperture?.let { formatPhysicalAperture(it) } else null
|
||||
)
|
||||
} else null,
|
||||
CameraParameterItem(
|
||||
CameraParameter.ISO,
|
||||
stringResource(R.string.camera_parameter_iso),
|
||||
@@ -247,6 +257,15 @@ internal fun formatShutterSpeedValue(value: Long): String {
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
internal fun formatPhysicalAperture(value: Float): String {
|
||||
val locale = LocalConfiguration.current.locales[0]
|
||||
val formatter = remember(locale) {
|
||||
NumberFormat.getNumberInstance(locale).apply { maximumFractionDigits = 2 }
|
||||
}
|
||||
return stringResource(R.string.camera_aperture_f_number, formatter.format(value.toDouble()))
|
||||
}
|
||||
|
||||
internal fun formatFocusDistance(value: Float): String {
|
||||
return if (value <= 0.01f) "∞"
|
||||
else {
|
||||
|
||||
@@ -221,10 +221,11 @@ private fun PanelDismissPreviewOverlay(
|
||||
)
|
||||
}
|
||||
|
||||
private fun CameraParameter.defaultResetValue(): Float {
|
||||
private fun CameraParameter.defaultResetValue(): Float? {
|
||||
return when (this) {
|
||||
CameraParameter.EXPOSURE_COMPENSATION -> 0f
|
||||
CameraParameter.SHUTTER_SPEED -> DefaultShutterSpeedNs
|
||||
CameraParameter.APERTURE -> null
|
||||
CameraParameter.ISO -> DefaultIso
|
||||
CameraParameter.FOCUS -> DefaultFocusDistance
|
||||
CameraParameter.WHITE_BALANCE -> DefaultAwbTemperature
|
||||
@@ -931,6 +932,11 @@ fun CameraScreen(
|
||||
parameterRulerBounds = null
|
||||
}
|
||||
}
|
||||
LaunchedEffect(state.isApertureAdjustable) {
|
||||
if (selectedParameter == CameraParameter.APERTURE && !state.isApertureAdjustable) {
|
||||
showParameterRuler = false
|
||||
}
|
||||
}
|
||||
|
||||
val density = LocalDensity.current
|
||||
val width = with(density) { constraints.maxWidth.toDp() }
|
||||
@@ -1058,11 +1064,16 @@ fun CameraScreen(
|
||||
} else {
|
||||
state.awbTemperature.toFloat()
|
||||
}
|
||||
val apertureValue = state.requestedPhysicalAperture
|
||||
?: state.physicalAperture
|
||||
?: state.availableApertures.firstOrNull() ?: 0f
|
||||
val apertureSelected = selectedParameter == CameraParameter.APERTURE
|
||||
ParameterRuler(
|
||||
parameter = selectedParameter,
|
||||
currentValue = when (selectedParameter) {
|
||||
CameraParameter.EXPOSURE_COMPENSATION -> state.exposureCompensation * state.getExposureCompensationStep()
|
||||
CameraParameter.SHUTTER_SPEED -> state.shutterSpeed.toFloat()
|
||||
CameraParameter.APERTURE -> apertureValue
|
||||
CameraParameter.ISO -> state.iso.toFloat()
|
||||
CameraParameter.FOCUS -> state.focusDistance
|
||||
CameraParameter.WHITE_BALANCE -> whiteBalanceCurrentValue
|
||||
@@ -1070,6 +1081,7 @@ fun CameraScreen(
|
||||
minValue = when (selectedParameter) {
|
||||
CameraParameter.EXPOSURE_COMPENSATION -> state.getExposureCompensationRange().lower * state.getExposureCompensationStep()
|
||||
CameraParameter.SHUTTER_SPEED -> state.getManualShutterSpeedRange().lower.toFloat()
|
||||
CameraParameter.APERTURE -> state.availableApertures.firstOrNull() ?: apertureValue
|
||||
CameraParameter.ISO -> state.getIsoRange().lower.toFloat()
|
||||
CameraParameter.FOCUS -> 0f
|
||||
CameraParameter.WHITE_BALANCE -> state.awbTemperatureMin.toFloat()
|
||||
@@ -1077,6 +1089,7 @@ fun CameraScreen(
|
||||
maxValue = when (selectedParameter) {
|
||||
CameraParameter.EXPOSURE_COMPENSATION -> state.getExposureCompensationRange().upper * state.getExposureCompensationStep()
|
||||
CameraParameter.SHUTTER_SPEED -> state.getManualShutterSpeedRange().upper.toFloat()
|
||||
CameraParameter.APERTURE -> state.availableApertures.lastOrNull() ?: apertureValue
|
||||
CameraParameter.ISO -> state.getIsoRange().upper.toFloat()
|
||||
CameraParameter.FOCUS -> state.minimumFocusDistance
|
||||
CameraParameter.WHITE_BALANCE -> state.awbTemperatureMax.toFloat()
|
||||
@@ -1084,27 +1097,28 @@ fun CameraScreen(
|
||||
isAdjustable = when (selectedParameter) {
|
||||
CameraParameter.EXPOSURE_COMPENSATION -> state.isAutoExposure
|
||||
CameraParameter.SHUTTER_SPEED, CameraParameter.ISO -> true
|
||||
CameraParameter.APERTURE -> state.isApertureAdjustable
|
||||
CameraParameter.FOCUS -> state.minimumFocusDistance > 0f
|
||||
CameraParameter.WHITE_BALANCE -> state.canAdjustWhiteBalance
|
||||
},
|
||||
isAutoMode = when (selectedParameter) {
|
||||
CameraParameter.SHUTTER_SPEED -> state.isShutterSpeedAuto
|
||||
CameraParameter.APERTURE -> state.isApertureAuto
|
||||
CameraParameter.ISO -> state.isIsoAuto
|
||||
CameraParameter.FOCUS -> state.isAutoFocus
|
||||
CameraParameter.WHITE_BALANCE ->
|
||||
state.awbMode == android.hardware.camera2.CameraMetadata.CONTROL_AWB_MODE_AUTO
|
||||
CameraParameter.EXPOSURE_COMPENSATION -> false
|
||||
},
|
||||
showAutoButton = when (selectedParameter) {
|
||||
CameraParameter.SHUTTER_SPEED, CameraParameter.ISO, CameraParameter.WHITE_BALANCE, CameraParameter.FOCUS -> true
|
||||
else -> false
|
||||
},
|
||||
showAutoButton = selectedParameter != CameraParameter.EXPOSURE_COMPENSATION,
|
||||
isAutoModeToggleEnabled = when (selectedParameter) {
|
||||
CameraParameter.WHITE_BALANCE -> state.canAdjustWhiteBalance
|
||||
CameraParameter.FOCUS -> state.minimumFocusDistance > 0f
|
||||
CameraParameter.APERTURE -> state.isApertureAdjustable
|
||||
else -> true
|
||||
},
|
||||
valueStep = state.getExposureCompensationStep(),
|
||||
discreteValues = state.availableApertures.takeIf { apertureSelected },
|
||||
resetValue = selectedParameter.defaultResetValue(),
|
||||
showHyperfocalButton = selectedParameter == CameraParameter.FOCUS && state.minimumFocusDistance > 0f,
|
||||
hyperfocalEnabled = state.isHyperfocalFocusEnabled,
|
||||
@@ -1116,6 +1130,7 @@ fun CameraScreen(
|
||||
when (selectedParameter) {
|
||||
CameraParameter.EXPOSURE_COMPENSATION -> viewModel.setExposureCompensation((value / state.getExposureCompensationStep()).roundToInt())
|
||||
CameraParameter.SHUTTER_SPEED -> viewModel.setShutterSpeed(value.toLong())
|
||||
CameraParameter.APERTURE -> viewModel.setPhysicalAperture(value)
|
||||
CameraParameter.ISO -> viewModel.setIso(value.toInt())
|
||||
CameraParameter.FOCUS -> viewModel.setFocusDistance(value)
|
||||
CameraParameter.WHITE_BALANCE -> viewModel.setAwbTemperature(value.toInt())
|
||||
@@ -1124,6 +1139,7 @@ fun CameraScreen(
|
||||
onAutoModeToggle = {
|
||||
when (selectedParameter) {
|
||||
CameraParameter.SHUTTER_SPEED -> viewModel.setShutterSpeedAuto(!state.isShutterSpeedAuto)
|
||||
CameraParameter.APERTURE -> viewModel.setPhysicalApertureAuto(!state.isApertureAuto)
|
||||
CameraParameter.ISO -> viewModel.setIsoAuto(!state.isIsoAuto)
|
||||
CameraParameter.WHITE_BALANCE -> {
|
||||
if (state.canAdjustWhiteBalance) {
|
||||
|
||||
@@ -41,6 +41,7 @@ import kotlin.math.roundToInt
|
||||
enum class CameraParameter {
|
||||
EXPOSURE_COMPENSATION,
|
||||
SHUTTER_SPEED,
|
||||
APERTURE,
|
||||
ISO,
|
||||
FOCUS,
|
||||
WHITE_BALANCE
|
||||
@@ -60,6 +61,7 @@ fun ParameterRuler(
|
||||
showAutoButton: Boolean,
|
||||
isAutoModeToggleEnabled: Boolean = true,
|
||||
valueStep: Float = 1f / 3f,
|
||||
discreteValues: List<Float>? = null,
|
||||
resetValue: Float? = null,
|
||||
showHyperfocalButton: Boolean = false,
|
||||
hyperfocalEnabled: Boolean = false,
|
||||
@@ -69,8 +71,8 @@ fun ParameterRuler(
|
||||
onAutoModeToggle: () -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
val scaleValues = remember(parameter, minValue, maxValue, valueStep) {
|
||||
getScaleValues(parameter, minValue, maxValue, valueStep)
|
||||
val scaleValues = remember(parameter, minValue, maxValue, valueStep, discreteValues) {
|
||||
discreteValues?.takeIf { it.isNotEmpty() } ?: getScaleValues(parameter, minValue, maxValue, valueStep)
|
||||
}
|
||||
val accentColor = AccentColor
|
||||
var selectedValue by remember(parameter, scaleValues) { mutableFloatStateOf(currentValue) }
|
||||
@@ -79,6 +81,7 @@ fun ParameterRuler(
|
||||
}
|
||||
var isDragging by remember(parameter, scaleValues) { mutableStateOf(false) }
|
||||
val currentValueState by rememberUpdatedState(currentValue)
|
||||
val canReset = resetValue != null
|
||||
val resetValueState by rememberUpdatedState(resetValue)
|
||||
val adjustableState by rememberUpdatedState(isAdjustable && scaleValues.size > 1)
|
||||
val autoModeState by rememberUpdatedState(isAutoMode)
|
||||
@@ -105,6 +108,7 @@ fun ParameterRuler(
|
||||
}
|
||||
|
||||
val displayedValue = if (isDragging) selectedValue else currentValue
|
||||
val displayedValueLabel = formatParameterValue(parameter, displayedValue)
|
||||
Row(
|
||||
modifier = modifier.fillMaxWidth().height(CameraParameterRulerHeight)
|
||||
.padding(horizontal = 12.dp),
|
||||
@@ -150,7 +154,7 @@ fun ParameterRuler(
|
||||
Box(
|
||||
modifier = Modifier.weight(1f).fillMaxHeight().clipToBounds()
|
||||
.semantics {
|
||||
stateDescription = formatParameterValue(parameter, displayedValue)
|
||||
stateDescription = displayedValueLabel
|
||||
progressBarRangeInfo = ProgressBarRangeInfo(
|
||||
closestScaleIndex(scaleValues, displayedValue).toFloat(),
|
||||
0f..scaleValues.lastIndex.toFloat(),
|
||||
@@ -163,13 +167,15 @@ fun ParameterRuler(
|
||||
}
|
||||
}
|
||||
}
|
||||
.pointerInput(parameter, scaleValues) {
|
||||
.pointerInput(parameter, scaleValues, canReset) {
|
||||
detectTapGestures(
|
||||
onDoubleTap = {
|
||||
if (adjustableState) {
|
||||
resetValueState?.let { selectValue(it, force = true) }
|
||||
onDoubleTap = if (canReset) {
|
||||
{
|
||||
if (adjustableState) {
|
||||
resetValueState?.let { selectValue(it, force = true) }
|
||||
}
|
||||
}
|
||||
},
|
||||
} else null,
|
||||
onTap = { position ->
|
||||
if (adjustableState) {
|
||||
val index = (scrollIndex + (position.x - size.width / 2f) /
|
||||
@@ -204,7 +210,7 @@ fun ParameterRuler(
|
||||
contentAlignment = Alignment.TopCenter
|
||||
) {
|
||||
Text(
|
||||
text = formatParameterValue(parameter, displayedValue),
|
||||
text = displayedValueLabel,
|
||||
modifier = Modifier.padding(top = 3.dp),
|
||||
color = AccentColor,
|
||||
fontSize = 14.sp,
|
||||
@@ -282,6 +288,7 @@ private fun formatHyperfocalDistance(distanceMeters: Float): String = if (distan
|
||||
private fun closestScaleIndex(values: List<Float>, value: Float): Int =
|
||||
values.indices.minByOrNull { abs(values[it] - value) } ?: 0
|
||||
|
||||
@Composable
|
||||
private fun formatParameterValue(parameter: CameraParameter, value: Float): String = when (parameter) {
|
||||
CameraParameter.EXPOSURE_COMPENSATION -> when {
|
||||
abs(value) < 0.05f -> "0"
|
||||
@@ -289,6 +296,7 @@ private fun formatParameterValue(parameter: CameraParameter, value: Float): Stri
|
||||
else -> String.format("%.1f", value)
|
||||
}
|
||||
CameraParameter.SHUTTER_SPEED -> formatShutterSpeedValue(value.toLong())
|
||||
CameraParameter.APERTURE -> if (value > 0f) formatPhysicalAperture(value) else stringResource(R.string.unknown)
|
||||
CameraParameter.ISO -> value.roundToInt().toString()
|
||||
CameraParameter.WHITE_BALANCE -> "${value.roundToInt()}K"
|
||||
CameraParameter.FOCUS -> formatFocusDistance(value)
|
||||
@@ -297,6 +305,8 @@ private fun formatParameterValue(parameter: CameraParameter, value: Float): Stri
|
||||
private fun getScaleValues(parameter: CameraParameter, minValue: Float, maxValue: Float, valueStep: Float): List<Float> {
|
||||
if (maxValue <= minValue) return listOf(minValue)
|
||||
return when (parameter) {
|
||||
CameraParameter.APERTURE -> listOf(minValue)
|
||||
|
||||
CameraParameter.EXPOSURE_COMPENSATION -> {
|
||||
(0..((maxValue - minValue) / valueStep).roundToInt()).map { minValue + it * valueStep }
|
||||
}
|
||||
|
||||
@@ -72,10 +72,9 @@ fun LogViewerDialog(
|
||||
LogViewerHeader(
|
||||
logCount = logs.size,
|
||||
onDownload = {
|
||||
val formattedLogs = PLog.getFormattedLogs()
|
||||
coroutineScope.launch {
|
||||
val fileName = withContext(Dispatchers.IO) {
|
||||
downloadLogs(context, formattedLogs).getOrNull()
|
||||
downloadLogs(context, PLog.getFormattedLogs(context)).getOrNull()
|
||||
}
|
||||
val message = if (fileName != null) {
|
||||
downloadSuccessMessage.format(fileName)
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
package com.hinnka.mycamera.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import com.hinnka.mycamera.BuildConfig
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.security.MessageDigest
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
import java.util.concurrent.ConcurrentLinkedQueue
|
||||
@@ -134,27 +140,62 @@ object PLog {
|
||||
/**
|
||||
* 获取格式化的所有日志文本
|
||||
*/
|
||||
fun getFormattedLogs(): String {
|
||||
if (logQueue.isEmpty()) {
|
||||
fun getFormattedLogs(context: Context): String {
|
||||
val entries = getAllLogs()
|
||||
if (entries.isEmpty()) {
|
||||
return "暂无日志记录"
|
||||
}
|
||||
|
||||
return buildString {
|
||||
appendLine("=== MyCamera 日志记录 ===")
|
||||
appendLine("导出时间: ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(Date())}")
|
||||
appendLine("日志总数: ${logQueue.size}")
|
||||
appendLine("日志总数: ${entries.size}")
|
||||
appendLine("app.packageName: ${context.packageName}")
|
||||
appendLine("app.version: ${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})")
|
||||
appendLine("app.flavor: ${BuildConfig.FLAVOR}")
|
||||
appendLine("app.buildType: ${BuildConfig.BUILD_TYPE}")
|
||||
appendLine("app.lastUpdateTimeMs: ${lastUpdateTime(context) ?: "unavailable"}")
|
||||
val appInfo = context.applicationInfo
|
||||
for (apkPath in listOf(appInfo.sourceDir) + appInfo.splitSourceDirs.orEmpty()) {
|
||||
val apk = File(apkPath)
|
||||
appendLine("app.apk.sha256[${apk.name}]: ${sha256(apk) ?: "unavailable"}")
|
||||
}
|
||||
appendLine("厂商: ${Build.MANUFACTURER}")
|
||||
appendLine("型号: ${Build.MODEL}")
|
||||
appendLine("Android 版本: ${Build.VERSION.RELEASE}")
|
||||
appendLine("=".repeat(50))
|
||||
appendLine()
|
||||
|
||||
getAllLogs().forEach { entry ->
|
||||
entries.forEach { entry ->
|
||||
appendLine(entry.getFormattedLog())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
private fun lastUpdateTime(context: Context): Long? = try {
|
||||
context.packageManager.getPackageInfo(context.packageName, 0).lastUpdateTime
|
||||
} catch (_: PackageManager.NameNotFoundException) {
|
||||
null
|
||||
}
|
||||
|
||||
private fun sha256(file: File): String? = try {
|
||||
val digest = MessageDigest.getInstance("SHA-256")
|
||||
file.inputStream().use { input ->
|
||||
val buffer = ByteArray(64 * 1024)
|
||||
while (true) {
|
||||
val count = input.read(buffer)
|
||||
if (count < 0) break
|
||||
digest.update(buffer, 0, count)
|
||||
}
|
||||
}
|
||||
digest.digest().joinToString("") { (it.toInt() and 0xff).toString(16).padStart(2, '0') }
|
||||
} catch (_: IOException) {
|
||||
null
|
||||
} catch (_: SecurityException) {
|
||||
null
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空所有日志
|
||||
*/
|
||||
|
||||
@@ -3428,6 +3428,14 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
updateUserCaptureSettings { setShutterSpeed(value) }
|
||||
}
|
||||
|
||||
fun setPhysicalAperture(value: Float) {
|
||||
cameraController.setPhysicalAperture(value)
|
||||
}
|
||||
|
||||
fun setPhysicalApertureAuto(enabled: Boolean) {
|
||||
cameraController.setPhysicalApertureAuto(enabled)
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置计算光圈 (等效虚化)
|
||||
*/
|
||||
|
||||
@@ -30,6 +30,8 @@
|
||||
<!-- Exposure Controls -->
|
||||
<string name="camera_parameter_exposure" translatable="false">EV</string>
|
||||
<string name="camera_parameter_shutter" translatable="false">Tv</string>
|
||||
<string name="camera_parameter_aperture" translatable="false">Av</string>
|
||||
<string name="camera_aperture_f_number" translatable="false">f/%1$s</string>
|
||||
<string name="camera_parameter_iso" translatable="false">ISO</string>
|
||||
<string name="camera_parameter_focus" translatable="false">AF</string>
|
||||
<string name="camera_parameter_white_balance" translatable="false">AWB</string>
|
||||
|
||||
Reference in new issue
Block a user