feat: implement manual focus control and focus peaking

This commit is contained in:
Hinnka committed 2026-05-04 10:39:58 +08:00
1 parent b7d327b0f7
commit c5f2dc51d3
12 files changed
+273 -61

No files matched your search

@@ -331,6 +331,7 @@ class Camera2Controller(private val context: Context) {
val exposureCompensation = result.get(CaptureResult.CONTROL_AE_EXPOSURE_COMPENSATION) ?: 0
val awbMode = result.get(CaptureResult.CONTROL_AWB_MODE) ?: CameraMetadata.CONTROL_AWB_MODE_AUTO
val aperture = result.get(CaptureResult.LENS_APERTURE)
val focusDistance = result.get(CaptureResult.LENS_FOCUS_DISTANCE) ?: 0f
// 关键修复:只在自动曝光模式下更新 ISO 和快门速度
// 手动模式下保持用户设置不变(因为预览使用的是限制后的曝光时间,不是用户设置的值)
@@ -340,6 +341,7 @@ class Camera2Controller(private val context: Context) {
?: _state.value.shutterSpeed else _state.value.shutterSpeed,
awbMode = awbMode,
physicalAperture = aperture ?: _state.value.physicalAperture,
focusDistance = focusDistance
)
}
}
@@ -709,7 +711,8 @@ class Camera2Controller(private val context: Context) {
isP010Supported = isP010Supported,
isHlg10Supported = isHlg10Supported,
availableNrModes = selectableNrModes,
currentPreviewSize = previewSize
currentPreviewSize = previewSize,
minimumFocusDistance = cachedCharacteristics?.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE) ?: 0f
)
} catch (e: Exception) {
PLog.e(TAG, "Failed to cache camera characteristics", e)
@@ -2345,6 +2348,42 @@ class Camera2Controller(private val context: Context) {
}
}
/**
* 设置自动对焦开关
*/
fun setAutoFocus(auto: Boolean) {
_state.value = _state.value.copy(isAutoFocus = auto)
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)
}
updatePreview()
}
}
/**
* 设置对焦距离 (0.0 ~ minimumFocusDistance)
*/
fun setFocusDistance(distance: Float) {
val minFocusDistance = _state.value.minimumFocusDistance
if (minFocusDistance <= 0) return
val clampedDistance = distance.coerceIn(0f, minFocusDistance)
_state.value = _state.value.copy(focusDistance = clampedDistance)
if (!_state.value.isAutoFocus) {
previewRequestBuilder?.apply {
set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF)
set(CaptureRequest.LENS_FOCUS_DISTANCE, clampedDistance)
updatePreview()
}
}
}
// ==================== 对焦控制 ====================
/**
@@ -2767,6 +2806,9 @@ class Camera2Controller(private val context: Context) {
preview.get(CaptureRequest.CONTROL_AF_MODE)?.let {
set(CaptureRequest.CONTROL_AF_MODE, it)
}
preview.get(CaptureRequest.LENS_FOCUS_DISTANCE)?.let {
set(CaptureRequest.LENS_FOCUS_DISTANCE, it)
}
preview.get(CaptureRequest.CONTROL_AF_REGIONS)?.let {
set(CaptureRequest.CONTROL_AF_REGIONS, it)
}
@@ -3246,6 +3288,7 @@ class Camera2Controller(private val context: Context) {
set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_IDLE)
builder.get(CaptureRequest.CONTROL_AF_MODE)?.let { set(CaptureRequest.CONTROL_AF_MODE, it) }
builder.get(CaptureRequest.LENS_FOCUS_DISTANCE)?.let { set(CaptureRequest.LENS_FOCUS_DISTANCE, it) }
builder.get(CaptureRequest.CONTROL_AF_REGIONS)?.let { set(CaptureRequest.CONTROL_AF_REGIONS, it) }
builder.get(CaptureRequest.CONTROL_AE_REGIONS)?.let { set(CaptureRequest.CONTROL_AE_REGIONS, it) }
}
@@ -495,7 +495,8 @@ class CameraDiscovery(private val context: Context) {
supportsRaw = characteristics.get(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES)?.contains(
CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_RAW
) == true,
isCustomLensId = isCustomLensId
isCustomLensId = isCustomLensId,
minimumFocusDistance = characteristics.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE) ?: 0f
)
}
@@ -74,7 +74,8 @@ data class CameraInfo(
val hardwareLevel: Int = -1, // 硬件支持级别
val supportsManualProcessing: Boolean = false, // 是否支持手动处理(关闭系统锐化/降噪)
val supportsRaw: Boolean = false, // 是否支持 RAW 格式
val isCustomLensId: Boolean = false // 是否来自用户手动添加的镜头 ID
val isCustomLensId: Boolean = false, // 是否来自用户手动添加的镜头 ID
val minimumFocusDistance: Float = 0f // 最小对焦距离 (diopters, 0 = infinity only)
) {
/**
* 获取镜头类型显示名称
@@ -141,6 +142,8 @@ data class CameraState(
val virtualAperture: Float = 2.0f, // 虚拟光圈值 (f-number)
// 对焦
val isAutoFocus: Boolean = true,
val focusDistance: Float = 0f, // 当前对焦距离 (0.0 - minimumFocusDistance)
val minimumFocusDistance: Float = 0f, // 最小对焦距离
val focusPoint: Pair<Float, Float>? = null, // normalized coordinates (0-1)
val isFocusing: Boolean = false,
val focusSuccess: Boolean? = null,
@@ -265,6 +265,19 @@ class LutRenderer : GLSurfaceView.Renderer {
private var bokehFboHeight: Int = 0
private var bokehRenderScale: Float = 0.5f // 降采样比例,0.5 代表 1/4 像素量
// Focus Peaking 实时预览资源
private var focusPeakingProgramId: Int = 0
private var uPeakInputTexLoc: Int = 0
private var uPeakTexelSizeLoc: Int = 0
private var uPeakThresholdLoc: Int = 0
private var uPeakColorLoc: Int = 0
private var aPeakPositionLoc: Int = 0
private var aPeakTexCoordLoc: Int = 0
private var focusPeakingFboId: Int = 0
private var focusPeakingTextureId: Int = 0
private var focusPeakingFboWidth: Int = 0
private var focusPeakingFboHeight: Int = 0
// Attribute 位置
private var aPositionLocation: Int = 0
private var aTexCoordLocation: Int = 0
@@ -301,6 +314,9 @@ class LutRenderer : GLSurfaceView.Renderer {
@Volatile
var lutEnabled: Boolean = false
@Volatile
var isAutoFocus: Boolean = true
@Volatile
var baselineLutEnabled: Boolean = false
@@ -488,7 +504,22 @@ class LutRenderer : GLSurfaceView.Renderer {
// 通知调用者 SurfaceTexture 已准备好
surfaceTexture?.let { onSurfaceTextureAvailable?.invoke(it) }
GlUtils.checkGlError("onSurfaceCreated")
// Focus Peaking Shader
val vs = GlUtils.compileShader(GLES30.GL_VERTEX_SHADER, Shaders.SIMPLE_VERTEX_SHADER)
val peakFrag = GlUtils.compileShader(GLES30.GL_FRAGMENT_SHADER, Shaders.FRAGMENT_SHADER_FOCUS_PEAKING)
focusPeakingProgramId = GlUtils.linkProgram(vs, peakFrag)
GLES30.glDeleteShader(vs)
GLES30.glDeleteShader(peakFrag)
if (focusPeakingProgramId != 0) {
uPeakInputTexLoc = GLES30.glGetUniformLocation(focusPeakingProgramId, "uInputTexture")
uPeakTexelSizeLoc = GLES30.glGetUniformLocation(focusPeakingProgramId, "uTexelSize")
uPeakThresholdLoc = GLES30.glGetUniformLocation(focusPeakingProgramId, "uThreshold")
uPeakColorLoc = GLES30.glGetUniformLocation(focusPeakingProgramId, "uPeakColor")
aPeakPositionLoc = GLES30.glGetAttribLocation(focusPeakingProgramId, "aPosition")
aPeakTexCoordLoc = GLES30.glGetAttribLocation(focusPeakingProgramId, "aTexCoord")
}
GlUtils.checkGlError("initShaderProgram")
}
/**
@@ -540,6 +571,11 @@ class LutRenderer : GLSurfaceView.Renderer {
bokehTextureId = 0
bokehFboWidth = 0
bokehFboHeight = 0
focusPeakingProgramId = 0
focusPeakingFboId = 0
focusPeakingTextureId = 0
focusPeakingFboWidth = 0
focusPeakingFboHeight = 0
lastCaptureWidth = 0
lastCaptureHeight = 0
viewportWidth = 0
@@ -872,18 +908,21 @@ class LutRenderer : GLSurfaceView.Renderer {
val hasCreativeLayer = hasCreativeLayer()
val hasDualLayer = hasBaselineLayer && hasCreativeLayer
uploadPendingCurveTextures()
val needsFbo = (liveRecorder != null || activeVideoRecorder != null || hdfEnabled || bokehNeeded || hasDualLayer) &&
val needsFbo = (liveRecorder != null || activeVideoRecorder != null || hdfEnabled || bokehNeeded || hasDualLayer || !isAutoFocus) &&
fboId != 0 && fboTextureId != 0
if (needsFbo) {
val identityMatrix = FloatArray(16).also { Matrix.setIdentityM(it, 0) }
val fullCropRect = floatArrayOf(0f, 0f, 1f, 1f)
var currentWidth = viewportWidth
var currentHeight = viewportHeight
// 1. 渲染色彩链路到 FBO
drawInternal(
fboId = fboId,
width = viewportWidth,
height = viewportHeight,
width = currentWidth,
height = currentHeight,
preferBaselineLayer = hasDualLayer,
suppressBaselineLayer = suppressBaselineLayerForVideoLog
)
@@ -913,6 +952,8 @@ class LutRenderer : GLSurfaceView.Renderer {
focusPointOverride = null
)
currentTexId = stackTextureId
currentWidth = viewportWidth
currentHeight = viewportHeight
}
// 深度采集(按需,从刚刚渲染好的 FBO 纹理读取)
@@ -923,14 +964,17 @@ class LutRenderer : GLSurfaceView.Renderer {
// 2. Bokeh 处理
if (bokehNeeded) {
currentTexId = renderBokehPreview(currentTexId, viewportWidth, viewportHeight)
currentWidth = viewportWidth
currentHeight = viewportHeight
}
// 3. HDF 多 Pass 处理
var outputTexId = currentTexId
if (hdfEnabled) {
renderHdfPreviewBlur(currentTexId, viewportWidth, viewportHeight)
renderHdfPreviewBlur(currentTexId, currentWidth, currentHeight)
}
// 确保 FBO 内容已刷入显存
GLES30.glFlush()
@@ -995,6 +1039,11 @@ class LutRenderer : GLSurfaceView.Renderer {
}
}
// 3.5. Focus Peaking (Only for preview, not for recording)
if (!isAutoFocus) {
currentTexId = renderFocusPeaking(currentTexId, currentWidth, currentHeight)
}
// 6. 显示到屏幕
if (hdfEnabled) {
drawHdfComposite(0, viewportWidth, viewportHeight, currentTexId)
@@ -2763,4 +2812,81 @@ class LutRenderer : GLSurfaceView.Renderer {
GLES30.glBindBuffer(GLES30.GL_ELEMENT_ARRAY_BUFFER, indexBufferId)
GLES30.glDrawElements(GLES30.GL_TRIANGLES, 6, GLES30.GL_UNSIGNED_SHORT, 0)
}
private fun renderFocusPeaking(inputTextureId: Int, width: Int, height: Int): Int {
if (focusPeakingProgramId == 0) return inputTextureId
ensureFocusPeakingFbo(width, height)
if (focusPeakingFboId == 0) return inputTextureId
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, focusPeakingFboId)
GLES30.glViewport(0, 0, width, height)
GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT)
GLES30.glUseProgram(focusPeakingProgramId)
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, inputTextureId)
GLES30.glUniform1i(uPeakInputTexLoc, 0)
GLES30.glUniform2f(uPeakTexelSizeLoc, 1.0f / width, 1.0f / height)
GLES30.glUniform1f(uPeakThresholdLoc, 0.8f)
GLES30.glUniform3f(uPeakColorLoc, 1.0f, 0.1f, 0.1f)
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, vertexBufferId)
GLES30.glEnableVertexAttribArray(aPeakPositionLoc)
GLES30.glVertexAttribPointer(aPeakPositionLoc, POSITION_COMPONENT_COUNT, GLES30.GL_FLOAT, false, 0, 0)
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, texCoordBufferId)
GLES30.glEnableVertexAttribArray(aPeakTexCoordLoc)
GLES30.glVertexAttribPointer(aPeakTexCoordLoc, TEXTURE_COORD_COMPONENT_COUNT, GLES30.GL_FLOAT, false, 0, 0)
GLES30.glBindBuffer(GLES30.GL_ELEMENT_ARRAY_BUFFER, indexBufferId)
GLES30.glDrawElements(GLES30.GL_TRIANGLES, 6, GLES30.GL_UNSIGNED_SHORT, 0)
GLES30.glDisableVertexAttribArray(aPeakPositionLoc)
GLES30.glDisableVertexAttribArray(aPeakTexCoordLoc)
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0)
GLES30.glBindBuffer(GLES30.GL_ELEMENT_ARRAY_BUFFER, 0)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
return focusPeakingTextureId
}
private fun ensureFocusPeakingFbo(width: Int, height: Int) {
if (focusPeakingFboWidth == width && focusPeakingFboHeight == height && focusPeakingFboId != 0) return
if (focusPeakingFboId != 0) {
GLES30.glDeleteFramebuffers(1, intArrayOf(focusPeakingFboId), 0)
}
if (focusPeakingTextureId != 0) {
GLES30.glDeleteTextures(1, intArrayOf(focusPeakingTextureId), 0)
}
val ids = IntArray(1)
GLES30.glGenFramebuffers(1, ids, 0)
focusPeakingFboId = ids[0]
GLES30.glGenTextures(1, ids, 0)
focusPeakingTextureId = ids[0]
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, focusPeakingTextureId)
GLES30.glTexImage2D(
GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, width, height,
0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null
)
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_LINEAR)
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR)
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE)
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, focusPeakingFboId)
GLES30.glFramebufferTexture2D(
GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0,
GLES30.GL_TEXTURE_2D, focusPeakingTextureId, 0
)
focusPeakingFboWidth = width
focusPeakingFboHeight = height
}
}
@@ -170,6 +170,40 @@ object Shaders {
}
""".trimIndent()
/**
* Focus Peaking Shader
*/
val FRAGMENT_SHADER_FOCUS_PEAKING = """
#version 300 es
precision highp float;
in vec2 vTexCoord;
out vec4 fragColor;
uniform sampler2D uInputTexture;
uniform vec2 uTexelSize;
uniform float uThreshold;
uniform vec3 uPeakColor;
void main() {
vec4 color = texture(uInputTexture, vTexCoord);
// Sobel edge detection
float l00 = dot(texture(uInputTexture, vTexCoord + vec2(-uTexelSize.x, -uTexelSize.y)).rgb, vec3(0.299, 0.587, 0.114));
float l10 = dot(texture(uInputTexture, vTexCoord + vec2(0.0, -uTexelSize.y)).rgb, vec3(0.299, 0.587, 0.114));
float l20 = dot(texture(uInputTexture, vTexCoord + vec2(uTexelSize.x, -uTexelSize.y)).rgb, vec3(0.299, 0.587, 0.114));
float l01 = dot(texture(uInputTexture, vTexCoord + vec2(-uTexelSize.x, 0.0)).rgb, vec3(0.299, 0.587, 0.114));
float l21 = dot(texture(uInputTexture, vTexCoord + vec2(uTexelSize.x, 0.0)).rgb, vec3(0.299, 0.587, 0.114));
float l02 = dot(texture(uInputTexture, vTexCoord + vec2(-uTexelSize.x, uTexelSize.y)).rgb, vec3(0.299, 0.587, 0.114));
float l12 = dot(texture(uInputTexture, vTexCoord + vec2(0.0, uTexelSize.y)).rgb, vec3(0.299, 0.587, 0.114));
float l22 = dot(texture(uInputTexture, vTexCoord + vec2(uTexelSize.x, uTexelSize.y)).rgb, vec3(0.299, 0.587, 0.114));
float gx = l00 + 2.0 * l01 + l02 - l20 - 2.0 * l21 - l22;
float gy = l00 + 2.0 * l10 + l20 - l02 - 2.0 * l12 - l22;
float edge = sqrt(gx * gx + gy * gy);
float peakFactor = smoothstep(uThreshold, uThreshold * 1.5, edge);
fragColor = vec4(mix(color.rgb, uPeakColor, peakFactor * 0.9), color.a);
}
""".trimIndent()
/**
* 片元着色器 - 带色彩配方和 3D LUT 支持
*
@@ -171,6 +171,11 @@ class CameraGLSurfaceView @JvmOverloads constructor(
requestRender()
}
fun setAutoFocus(auto: Boolean) {
renderer.isAutoFocus = auto
requestRender()
}
/**
* 获取当前 LUT 强度
*/
@@ -58,13 +58,12 @@ fun CameraParameterBar(
onClick = { onParameterClick(CameraParameter.ISO) }
)
ParameterItem(
label = "Av",
value = String.format("f/%.1f", if (state.isVirtualApertureEnabled) state.virtualAperture else state.physicalAperture),
label = "AF",
value = if (state.isAutoFocus) "AUTO" else formatFocusDistance(state.focusDistance),
labelColor = yellow,
valueColor = if (state.isVirtualApertureEnabled) Color(0xFF00E5FF) else null, // 开启虚拟光圈时显示青色
isSelected = selectedParameter == CameraParameter.APERTURE,
isEnabled = true, // Enabled for computational bokeh
onClick = { onParameterClick(CameraParameter.APERTURE) }
isSelected = selectedParameter == CameraParameter.FOCUS,
isEnabled = true,
onClick = { onParameterClick(CameraParameter.FOCUS) }
)
ParameterItem(
label = "AWB",
@@ -120,3 +119,12 @@ fun ParameterItem(
)
}
}
internal fun formatFocusDistance(value: Float): String {
return if (value <= 0.01f) "∞"
else {
val meters = 1.0f / value
if (meters >= 1.0f) String.format("%.1fm", meters)
else String.format("%dcm", (meters * 100).toInt())
}
}
@@ -57,13 +57,12 @@ fun CameraParameterBarVerticel(
onClick = { onParameterClick(CameraParameter.ISO) }
)
ParameterItem(
label = "Av",
value = String.format("f/%.1f", if (state.isVirtualApertureEnabled) state.virtualAperture else state.physicalAperture),
label = "AF",
value = if (state.isAutoFocus) "AUTO" else formatFocusDistance(state.focusDistance),
labelColor = yellow,
valueColor = if (state.isVirtualApertureEnabled) Color(0xFF00E5FF) else null, // 开启虚拟光圈时显示青色
isSelected = selectedParameter == CameraParameter.APERTURE,
isEnabled = true, // Enabled for computational bokeh
onClick = { onParameterClick(CameraParameter.APERTURE) }
isSelected = selectedParameter == CameraParameter.FOCUS,
isEnabled = true,
onClick = { onParameterClick(CameraParameter.FOCUS) }
)
ParameterItem(
label = "AWB",
@@ -79,4 +78,3 @@ fun CameraParameterBarVerticel(
)
}
}
@@ -56,6 +56,7 @@ fun CameraPreviewGL(
isHlgInput: Boolean = false,
onGLSurfaceViewReady: ((CameraGLSurfaceView) -> Unit)? = null,
aperture: Float = 0f,
isAutoFocus: Boolean = true,
modifier: Modifier = Modifier
) {
val rotationDegrees = OrientationObserver.rotationDegrees
@@ -208,6 +209,7 @@ fun CameraPreviewGL(
glSurfaceView.setVideoRecorder(videoRecorder)
glSurfaceView.setVideoLogProfile(videoLogProfile)
glSurfaceView.setIsHlgInput(isHlgInput)
glSurfaceView.setAutoFocus(isAutoFocus)
},
modifier = Modifier.fillMaxSize()
)
@@ -524,32 +524,32 @@ fun CameraScreen(
CameraParameter.EXPOSURE_COMPENSATION -> state.exposureCompensation * state.getExposureCompensationStep()
CameraParameter.SHUTTER_SPEED -> state.shutterSpeed.toFloat()
CameraParameter.ISO -> state.iso.toFloat()
CameraParameter.APERTURE -> if (state.isVirtualApertureEnabled) state.virtualAperture else state.physicalAperture
CameraParameter.FOCUS -> state.focusDistance
CameraParameter.WHITE_BALANCE -> state.awbTemperature.toFloat()
},
minValue = when (selectedParameter) {
CameraParameter.EXPOSURE_COMPENSATION -> state.getExposureCompensationRange().lower * state.getExposureCompensationStep()
CameraParameter.SHUTTER_SPEED -> state.getShutterSpeedRange().lower.toFloat()
CameraParameter.ISO -> state.getIsoRange().lower.toFloat()
CameraParameter.APERTURE -> 1f
CameraParameter.FOCUS -> 0f
CameraParameter.WHITE_BALANCE -> 2000f
},
maxValue = when (selectedParameter) {
CameraParameter.EXPOSURE_COMPENSATION -> state.getExposureCompensationRange().upper * state.getExposureCompensationStep()
CameraParameter.SHUTTER_SPEED -> state.getShutterSpeedRange().upper.toFloat()
CameraParameter.ISO -> state.getIsoRange().upper.toFloat()
CameraParameter.APERTURE -> 16.0f
CameraParameter.FOCUS -> state.minimumFocusDistance
CameraParameter.WHITE_BALANCE -> 10000f
},
isAdjustable = when (selectedParameter) {
CameraParameter.EXPOSURE_COMPENSATION -> state.isAutoExposure
CameraParameter.SHUTTER_SPEED -> !state.isShutterSpeedAuto
CameraParameter.ISO -> !state.isIsoAuto
CameraParameter.APERTURE -> state.isVirtualApertureEnabled
CameraParameter.FOCUS -> !state.isAutoFocus
CameraParameter.WHITE_BALANCE -> state.awbMode != android.hardware.camera2.CameraMetadata.CONTROL_AWB_MODE_AUTO
},
showAutoButton = when (selectedParameter) {
CameraParameter.SHUTTER_SPEED, CameraParameter.ISO, CameraParameter.WHITE_BALANCE, CameraParameter.APERTURE -> true
CameraParameter.SHUTTER_SPEED, CameraParameter.ISO, CameraParameter.WHITE_BALANCE, CameraParameter.FOCUS -> true
else -> false
},
onValueChange = { value ->
@@ -557,7 +557,7 @@ fun CameraScreen(
CameraParameter.EXPOSURE_COMPENSATION -> viewModel.setExposureCompensation((value / state.getExposureCompensationStep()).roundToInt())
CameraParameter.SHUTTER_SPEED -> viewModel.setShutterSpeed(value.toLong())
CameraParameter.ISO -> viewModel.setIso(value.toInt())
CameraParameter.APERTURE -> viewModel.setAperture(value)
CameraParameter.FOCUS -> viewModel.setFocusDistance(value)
CameraParameter.WHITE_BALANCE -> viewModel.setAwbTemperature(value.toInt())
}
},
@@ -573,7 +573,7 @@ fun CameraScreen(
}
}
CameraParameter.APERTURE -> viewModel.setVirtualApertureAuto(!state.isVirtualApertureEnabled)
CameraParameter.FOCUS -> viewModel.setAutoFocus(!state.isAutoFocus)
else -> {}
}
},
@@ -734,6 +734,7 @@ fun CameraScreen(
videoLogProfile = state.videoConfig.logProfile,
isHlgInput = if (hlgHardwareCompatibilityEnabled) state.isHLG else false,
aperture = if (state.isVirtualApertureEnabled) state.virtualAperture else 0f,
isAutoFocus = state.isAutoFocus,
modifier = Modifier.fillMaxSize()
)
@@ -37,7 +37,7 @@ enum class CameraParameter {
EXPOSURE_COMPENSATION, // AE
SHUTTER_SPEED, // Tv
ISO, // ISO
APERTURE, // Av
FOCUS, // Focus
WHITE_BALANCE // AWB
}
@@ -443,36 +443,13 @@ private fun getScaleValues(parameter: CameraParameter, minValue: Float, maxValue
.toList()
}
CameraParameter.APERTURE -> {
listOf(
minValue,
1f,
1.2f,
1.4f,
1.8f,
2f,
2.4f,
2.8f,
3.2f,
3.5f,
4f,
4.5f,
5f,
5.6f,
6.3f,
7.1f,
8f,
9f,
10f,
11f,
13f,
14f,
16f,
maxValue
)
.toSet()
.map { it }
.filter { it in minValue..maxValue }
CameraParameter.FOCUS -> {
val steps = 20
val list = mutableListOf<Float>()
for (i in 0..steps) {
list.add(minValue + (maxValue - minValue) * i / steps)
}
list
}
}
}
@@ -509,8 +486,13 @@ private fun formatParameterValue(parameter: CameraParameter, value: Float): Stri
"${value.toInt()}K"
}
CameraParameter.APERTURE -> {
"f/${String.format("%.1f", value)}"
CameraParameter.FOCUS -> {
if (value <= 0.01f) "∞"
else {
val meters = 1.0f / value
if (meters >= 1.0f) String.format("%.1fm", meters)
else String.format("%dcm", (meters * 100).toInt())
}
}
}
}
@@ -681,6 +681,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
currentState.focusPoint?.let { fp ->
view.setFocusPoint(android.graphics.PointF(fp.first, fp.second))
}
view.setAutoFocus(currentState.isAutoFocus)
}
}
}
@@ -1344,6 +1345,14 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
cameraController.setVirtualApertureEnabled(enabled)
}
fun setAutoFocus(auto: Boolean) {
cameraController.setAutoFocus(auto)
}
fun setFocusDistance(distance: Float) {
cameraController.setFocusDistance(distance)
}
/**
* 设置变焦倍数
*/