fix: camera parameters
This commit is contained in:
1 parent
9edeba62af
commit
fa196cbdb0
6 files changed
+189
-197
No files matched your search
@@ -8,6 +8,14 @@
|
||||
},
|
||||
"path": "standard.plut"
|
||||
},
|
||||
{
|
||||
"id": "pentax",
|
||||
"name": {
|
||||
"en": "Pentax",
|
||||
"zh": "鲜明"
|
||||
},
|
||||
"path": "pentax.plut"
|
||||
},
|
||||
{
|
||||
"id": "leica",
|
||||
"name": {
|
||||
@@ -71,14 +79,6 @@
|
||||
"zh": "索尼VV2"
|
||||
},
|
||||
"path": "vv2.plut"
|
||||
},
|
||||
{
|
||||
"id": "pentax",
|
||||
"name": {
|
||||
"en": "Pentax",
|
||||
"zh": "宾得鲜明"
|
||||
},
|
||||
"path": "pentax.plut"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -306,20 +306,8 @@ class Camera2Controller(private val context: Context) {
|
||||
// 设置连续自动对焦
|
||||
set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE)
|
||||
|
||||
if (state.value.isAutoExposure) {
|
||||
// 设置自动曝光
|
||||
set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON)
|
||||
} else {
|
||||
// 手动曝光
|
||||
set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_OFF)
|
||||
set(CaptureRequest.SENSOR_SENSITIVITY, state.value.iso)
|
||||
|
||||
// 预览时限制曝光时间,防止画面卡死
|
||||
val previewExposureTime = state.value.shutterSpeed.coerceAtMost(MAX_PREVIEW_EXPOSURE_TIME)
|
||||
set(CaptureRequest.SENSOR_EXPOSURE_TIME, previewExposureTime)
|
||||
}
|
||||
|
||||
setupFlatProfileRequest(this)
|
||||
// 应用所有相机参数(曝光、白平衡、闪光灯、变焦、色调映射)
|
||||
applyBaseCameraSettings(this, isCapture = false)
|
||||
}
|
||||
|
||||
val surfaces = mutableListOf(surface, reader.surface)
|
||||
@@ -397,6 +385,141 @@ class Camera2Controller(private val context: Context) {
|
||||
// builder.set(CaptureRequest.EDGE_MODE, CaptureRequest.EDGE_MODE_OFF)
|
||||
}
|
||||
|
||||
// ==================== 统一参数配置 ====================
|
||||
|
||||
/**
|
||||
* 将当前状态中的相机参数应用到 CaptureRequest.Builder
|
||||
*
|
||||
* 这是确保预览与拍摄参数一致性的核心方法
|
||||
*
|
||||
* @param builder 需要配置的 Builder
|
||||
* @param isCapture 是否为拍摄请求(预览时某些参数有限制)
|
||||
*/
|
||||
private fun applyBaseCameraSettings(builder: CaptureRequest.Builder, isCapture: Boolean = false) {
|
||||
val currentState = _state.value
|
||||
|
||||
// 1. 曝光设置
|
||||
applyExposureSettings(builder, currentState, isCapture)
|
||||
|
||||
// 2. 白平衡设置
|
||||
applyWhiteBalanceSettings(builder, currentState)
|
||||
|
||||
// 3. 闪光灯设置
|
||||
applyFlashSettings(builder, currentState)
|
||||
|
||||
// 4. 变焦设置
|
||||
applyZoomSettings(builder, currentState)
|
||||
|
||||
// 5. 色调映射(Flat Profile)
|
||||
setupFlatProfileRequest(builder)
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用曝光设置
|
||||
*/
|
||||
private fun applyExposureSettings(builder: CaptureRequest.Builder, state: CameraState, isCapture: Boolean) {
|
||||
if (state.isAutoExposure) {
|
||||
// 自动曝光
|
||||
builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON)
|
||||
builder.set(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, state.exposureCompensation)
|
||||
} else {
|
||||
// 手动曝光
|
||||
builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_OFF)
|
||||
builder.set(CaptureRequest.SENSOR_SENSITIVITY, state.iso)
|
||||
|
||||
// 预览时限制曝光时间,防止画面卡死;拍摄时使用完整的用户设置
|
||||
val exposureTime = if (isCapture) {
|
||||
state.shutterSpeed
|
||||
} else {
|
||||
state.shutterSpeed.coerceAtMost(MAX_PREVIEW_EXPOSURE_TIME)
|
||||
}
|
||||
builder.set(CaptureRequest.SENSOR_EXPOSURE_TIME, exposureTime)
|
||||
|
||||
// 拍摄时设置低帧率范围以支持长曝光
|
||||
if (isCapture) {
|
||||
try {
|
||||
val characteristics = cameraManager.getCameraCharacteristics(state.currentCameraId)
|
||||
val availableFpsRanges = characteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES)
|
||||
val lowestFpsRange = availableFpsRanges?.minByOrNull { it.upper }
|
||||
lowestFpsRange?.let {
|
||||
builder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, it)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to set FPS range", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用白平衡设置
|
||||
*/
|
||||
private fun applyWhiteBalanceSettings(builder: CaptureRequest.Builder, state: CameraState) {
|
||||
builder.set(CaptureRequest.CONTROL_AWB_MODE, state.awbMode)
|
||||
|
||||
if (state.awbMode == CameraMetadata.CONTROL_AWB_MODE_OFF && supportsManualWhiteBalance()) {
|
||||
// 手动白平衡,应用当前色温
|
||||
builder.set(CaptureRequest.COLOR_CORRECTION_MODE, CaptureRequest.COLOR_CORRECTION_MODE_TRANSFORM_MATRIX)
|
||||
val gains = kelvinToRggbGains(state.awbTemperature)
|
||||
builder.set(CaptureRequest.COLOR_CORRECTION_GAINS, gains)
|
||||
} else {
|
||||
builder.set(CaptureRequest.COLOR_CORRECTION_MODE, CaptureRequest.COLOR_CORRECTION_MODE_FAST)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用闪光灯设置
|
||||
*/
|
||||
private fun applyFlashSettings(builder: CaptureRequest.Builder, state: CameraState) {
|
||||
when (state.flashMode) {
|
||||
CameraMetadata.FLASH_MODE_OFF -> {
|
||||
// 注意:如果是自动曝光模式,这里不需要重新设置 AE_MODE
|
||||
// 因为 applyExposureSettings 已经处理了
|
||||
builder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF)
|
||||
}
|
||||
CameraMetadata.FLASH_MODE_SINGLE -> {
|
||||
if (state.isAutoExposure) {
|
||||
builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH)
|
||||
}
|
||||
builder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_SINGLE)
|
||||
}
|
||||
CameraMetadata.FLASH_MODE_TORCH -> {
|
||||
builder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH)
|
||||
}
|
||||
else -> {
|
||||
builder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用变焦设置
|
||||
*/
|
||||
private fun applyZoomSettings(builder: CaptureRequest.Builder, state: CameraState) {
|
||||
val cameraId = state.currentCameraId
|
||||
if (cameraId.isEmpty() || state.zoomRatio <= 1f) return
|
||||
|
||||
try {
|
||||
val characteristics = cameraManager.getCameraCharacteristics(cameraId)
|
||||
val activeRect = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE) ?: return
|
||||
|
||||
val centerX = activeRect.width() / 2
|
||||
val centerY = activeRect.height() / 2
|
||||
val deltaX = ((activeRect.width() / 2) / state.zoomRatio).toInt()
|
||||
val deltaY = ((activeRect.height() / 2) / state.zoomRatio).toInt()
|
||||
|
||||
val cropRect = android.graphics.Rect(
|
||||
centerX - deltaX,
|
||||
centerY - deltaY,
|
||||
centerX + deltaX,
|
||||
centerY + deltaY
|
||||
)
|
||||
builder.set(CaptureRequest.SCALER_CROP_REGION, cropRect)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to apply zoom settings", e)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最佳拍照尺寸
|
||||
*/
|
||||
@@ -1050,43 +1173,24 @@ class Camera2Controller(private val context: Context) {
|
||||
val captureBuilder = device.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE).apply {
|
||||
addTarget(reader.surface)
|
||||
|
||||
// 明确设置曝光模式和参数
|
||||
if (_state.value.isAutoExposure) {
|
||||
// 自动曝光
|
||||
set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON)
|
||||
set(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, _state.value.exposureCompensation)
|
||||
Log.d(TAG, "Capture with auto exposure, EV: ${_state.value.exposureCompensation}")
|
||||
} else {
|
||||
// 手动曝光 - 使用完整的用户设置(不限制曝光时间)
|
||||
set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_OFF)
|
||||
|
||||
// 关键修复:设置最低 FPS 范围以支持长曝光
|
||||
try {
|
||||
val characteristics = cameraManager.getCameraCharacteristics(_state.value.currentCameraId)
|
||||
val availableFpsRanges = characteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES)
|
||||
val lowestFpsRange = availableFpsRanges?.minByOrNull { it.upper }
|
||||
lowestFpsRange?.let {
|
||||
set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, it)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to set FPS range", e)
|
||||
}
|
||||
|
||||
set(CaptureRequest.SENSOR_SENSITIVITY, _state.value.iso)
|
||||
set(CaptureRequest.SENSOR_EXPOSURE_TIME, _state.value.shutterSpeed)
|
||||
}
|
||||
// 应用所有相机参数(曝光、白平衡、闪光灯、变焦、色调映射)
|
||||
// isCapture = true 确保使用完整的曝光时间(不限制长曝光)
|
||||
applyBaseCameraSettings(this, isCapture = true)
|
||||
|
||||
// 从预览请求复制其他设置(变焦、对焦区域等)
|
||||
// 从预览请求复制对焦相关设置
|
||||
previewRequestBuilder?.let { preview ->
|
||||
preview.get(CaptureRequest.SCALER_CROP_REGION)?.let {
|
||||
set(CaptureRequest.SCALER_CROP_REGION, it)
|
||||
preview.get(CaptureRequest.CONTROL_AF_MODE)?.let {
|
||||
set(CaptureRequest.CONTROL_AF_MODE, it)
|
||||
}
|
||||
preview.get(CaptureRequest.CONTROL_AF_REGIONS)?.let {
|
||||
set(CaptureRequest.CONTROL_AF_REGIONS, it)
|
||||
}
|
||||
preview.get(CaptureRequest.CONTROL_AE_REGIONS)?.let {
|
||||
set(CaptureRequest.CONTROL_AE_REGIONS, it)
|
||||
}
|
||||
}
|
||||
|
||||
// 设置 JPEG 方向
|
||||
set(CaptureRequest.JPEG_ORIENTATION, getJpegOrientation())
|
||||
set(CaptureRequest.JPEG_QUALITY, 95.toByte())
|
||||
setupFlatProfileRequest(this)
|
||||
Log.d(TAG, "Capture with ISO: ${_state.value.iso}, shutterSpeed: ${_state.value.shutterSpeed}, isAutoExposure: ${_state.value.isAutoExposure}")
|
||||
}
|
||||
|
||||
captureSession?.capture(captureBuilder.build(), object : CameraCaptureSession.CaptureCallback() {
|
||||
@@ -1246,15 +1350,6 @@ class Camera2Controller(private val context: Context) {
|
||||
|
||||
// ==================== 生命周期 ====================
|
||||
|
||||
/**
|
||||
* 检查相机状态并在必要时恢复
|
||||
*/
|
||||
fun checkAndRecoverCamera() {
|
||||
if (!_state.value.isPreviewActive && cameraDevice == null) {
|
||||
Log.d(TAG, "Camera not active, may need to reopen")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭相机
|
||||
*/
|
||||
|
||||
@@ -41,6 +41,7 @@ fun CameraParameterBar(
|
||||
label = "Tv",
|
||||
value = if (state.shutterSpeed >= 1_000_000_000.0) (state.shutterSpeed / 1_000_000_000.0).toInt().toString() else "1/${(1_000_000_000.0 / state.shutterSpeed).toInt()}",
|
||||
labelColor = yellow,
|
||||
valueColor = if (state.shutterSpeed > 1_000_000_000.0 / 15) Color.Red else null,
|
||||
isSelected = selectedParameter == CameraParameter.SHUTTER_SPEED,
|
||||
isEnabled = true,
|
||||
onClick = { onParameterClick(CameraParameter.SHUTTER_SPEED) }
|
||||
@@ -81,6 +82,7 @@ private fun ParameterItem(
|
||||
label: String,
|
||||
value: String,
|
||||
labelColor: Color,
|
||||
valueColor: Color? = null,
|
||||
isSelected: Boolean,
|
||||
isEnabled: Boolean,
|
||||
onClick: () -> Unit,
|
||||
@@ -107,7 +109,7 @@ private fun ParameterItem(
|
||||
)
|
||||
Text(
|
||||
text = value,
|
||||
color = if (isSelected) Color(0xFFFFD700) else Color.White.copy(alpha = if (isEnabled) 1f else 0.5f),
|
||||
color = valueColor ?: if (isSelected) Color(0xFFFFD700) else Color.White.copy(alpha = if (isEnabled) 1f else 0.5f),
|
||||
fontSize = 11.sp,
|
||||
fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Bold
|
||||
)
|
||||
|
||||
@@ -407,34 +407,34 @@ private fun getScaleValues(parameter: CameraParameter, minValue: Float, maxValue
|
||||
// Standard ISO values
|
||||
listOf(
|
||||
minValue,
|
||||
50,
|
||||
64,
|
||||
80,
|
||||
100,
|
||||
125,
|
||||
160,
|
||||
200,
|
||||
250,
|
||||
320,
|
||||
400,
|
||||
500,
|
||||
640,
|
||||
800,
|
||||
1000,
|
||||
1250,
|
||||
1600,
|
||||
2000,
|
||||
2500,
|
||||
3200,
|
||||
4000,
|
||||
5000,
|
||||
6400,
|
||||
8000,
|
||||
12800,
|
||||
50f,
|
||||
64f,
|
||||
80f,
|
||||
100f,
|
||||
125f,
|
||||
160f,
|
||||
200f,
|
||||
250f,
|
||||
320f,
|
||||
400f,
|
||||
500f,
|
||||
640f,
|
||||
800f,
|
||||
1000f,
|
||||
1250f,
|
||||
1600f,
|
||||
2000f,
|
||||
2500f,
|
||||
3200f,
|
||||
4000f,
|
||||
5000f,
|
||||
6400f,
|
||||
8000f,
|
||||
12800f,
|
||||
maxValue
|
||||
)
|
||||
.toSet()
|
||||
.map { it.toFloat() }
|
||||
.map { it }
|
||||
.filter { it in minValue..maxValue }
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ import androidx.compose.ui.unit.sp
|
||||
import coil.compose.AsyncImage
|
||||
import coil.request.ImageRequest
|
||||
import com.hinnka.mycamera.R
|
||||
import com.hinnka.mycamera.ui.components.CustomSlider
|
||||
import com.hinnka.mycamera.ui.theme.AccentOrange
|
||||
import com.hinnka.mycamera.viewmodel.GalleryViewModel
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -273,16 +274,11 @@ fun PhotoEditScreen(
|
||||
fontSize = 14.sp,
|
||||
modifier = Modifier.width(60.dp)
|
||||
)
|
||||
|
||||
Slider(
|
||||
|
||||
CustomSlider(
|
||||
value = editLutIntensity,
|
||||
onValueChange = { viewModel.updateEditLutIntensity(it) },
|
||||
valueRange = 0f..1f,
|
||||
colors = SliderDefaults.colors(
|
||||
thumbColor = AccentOrange,
|
||||
activeTrackColor = AccentOrange,
|
||||
inactiveTrackColor = Color.White.copy(alpha = 0.3f)
|
||||
),
|
||||
modifier = Modifier.weight(1f)
|
||||
)
|
||||
|
||||
@@ -328,106 +324,6 @@ fun PhotoEditScreen(
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// App 品牌开关(仅当选择了边框时显示)
|
||||
if (editFrameId != null) {
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween
|
||||
) {
|
||||
Text(
|
||||
text = stringResource(R.string.show_app_branding),
|
||||
color = Color.White,
|
||||
fontSize = 14.sp
|
||||
)
|
||||
|
||||
Switch(
|
||||
checked = editShowAppBranding,
|
||||
onCheckedChange = { viewModel.setShowAppBranding(it) },
|
||||
colors = SwitchDefaults.colors(
|
||||
checkedThumbColor = AccentOrange,
|
||||
checkedTrackColor = AccentOrange.copy(alpha = 0.5f),
|
||||
uncheckedThumbColor = Color.Gray,
|
||||
uncheckedTrackColor = Color.Gray.copy(alpha = 0.3f)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
// 旋转控制
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Text(
|
||||
text = stringResource(R.string.rotate),
|
||||
color = Color.White,
|
||||
fontSize = 16.sp,
|
||||
modifier = Modifier.width(80.dp)
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.weight(1f))
|
||||
|
||||
IconButton(
|
||||
onClick = { viewModel.rotate90() },
|
||||
modifier = Modifier
|
||||
.size(48.dp)
|
||||
.background(AccentOrange.copy(alpha = 0.2f), CircleShape)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Rotate90DegreesCw,
|
||||
contentDescription = "Rotate 90°",
|
||||
tint = AccentOrange
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
|
||||
Text(
|
||||
text = "${rotation.toInt()}°",
|
||||
color = Color.White.copy(alpha = 0.7f),
|
||||
fontSize = 14.sp
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
// 亮度控制
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Text(
|
||||
text = stringResource(R.string.brightness),
|
||||
color = Color.White,
|
||||
fontSize = 16.sp,
|
||||
modifier = Modifier.width(80.dp)
|
||||
)
|
||||
|
||||
Slider(
|
||||
value = brightness,
|
||||
onValueChange = { viewModel.setBrightness(it) },
|
||||
valueRange = 0.5f..2f,
|
||||
colors = SliderDefaults.colors(
|
||||
thumbColor = AccentOrange,
|
||||
activeTrackColor = AccentOrange,
|
||||
inactiveTrackColor = Color.White.copy(alpha = 0.3f)
|
||||
),
|
||||
modifier = Modifier.weight(1f)
|
||||
)
|
||||
|
||||
Text(
|
||||
text = String.format("%.1fx", brightness),
|
||||
color = Color.White.copy(alpha = 0.7f),
|
||||
fontSize = 14.sp,
|
||||
modifier = Modifier.width(48.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,7 +208,6 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
* 检查相机状态并在必要时恢复
|
||||
*/
|
||||
fun checkAndRecoverCamera() {
|
||||
cameraController.checkAndRecoverCamera()
|
||||
// 如果有保存的 SurfaceTexture,重新打开相机
|
||||
currentSurfaceTexture?.let { texture ->
|
||||
if (!state.value.isPreviewActive) {
|
||||
|
||||
Reference in new issue
Block a user