fix: camera switch
This commit is contained in:
1 parent
c03bb4e157
commit
b9c8c8a85d
3 files changed
+65
-106
No files matched your search
@@ -228,6 +228,7 @@ fun CameraScreen(
|
||||
if (activePanel == ActivePanel.NONE) {
|
||||
// Zoom Control Bar (Overlay at bottom of preview)
|
||||
ZoomControlBar(
|
||||
viewModel = viewModel,
|
||||
zoomRatio = viewModel.zoomRatioByMain,
|
||||
availableCameras = state.availableCameras,
|
||||
currentCameraId = state.getCurrentCameraInfo()?.cameraId ?: "0",
|
||||
|
||||
@@ -26,6 +26,7 @@ import androidx.compose.ui.unit.sp
|
||||
import com.hinnka.mycamera.R
|
||||
import com.hinnka.mycamera.camera.CameraInfo
|
||||
import com.hinnka.mycamera.camera.LensType
|
||||
import com.hinnka.mycamera.viewmodel.CameraViewModel
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
@@ -39,6 +40,7 @@ enum class ZoomDisplayMode {
|
||||
|
||||
@Composable
|
||||
fun ZoomControlBar(
|
||||
viewModel: CameraViewModel,
|
||||
zoomRatio: Float,
|
||||
availableCameras: List<CameraInfo>,
|
||||
currentCameraId: String,
|
||||
@@ -59,8 +61,8 @@ fun ZoomControlBar(
|
||||
availableCameras.find { it.lensType == if (currentCamera?.lensType == LensType.FRONT) LensType.FRONT else LensType.BACK_MAIN }
|
||||
|
||||
// 根据可用相机计算变焦档位
|
||||
val lensZoomStops = calculateLensZoomStops(availableCameras, currentCamera)
|
||||
val zoomStops = allZoomStops(lensZoomStops, mainCamera, currentCamera)
|
||||
val lensZoomStops = viewModel.calculateLensZoomStops(availableCameras, currentCamera)
|
||||
val zoomStops = viewModel.allZoomStops(lensZoomStops, mainCamera, currentCamera)
|
||||
|
||||
Box(
|
||||
modifier = modifier
|
||||
@@ -118,7 +120,7 @@ fun ZoomControlBar(
|
||||
displayMode = displayMode,
|
||||
onZoomChange = { newZoom ->
|
||||
// 检查是否需要切换镜头
|
||||
val camera = findOptimalLens(newZoom, availableCameras, currentCameraIdState)
|
||||
val camera = viewModel.findOptimalLens(newZoom, availableCameras, currentCameraIdState)
|
||||
if (camera != null && camera.cameraId != currentCameraIdState) {
|
||||
onLensSwitch(camera.cameraId)
|
||||
}
|
||||
@@ -130,79 +132,7 @@ fun ZoomControlBar(
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 计算变焦档位
|
||||
*/
|
||||
private fun calculateLensZoomStops(
|
||||
cameras: List<CameraInfo>,
|
||||
currentCamera: CameraInfo?
|
||||
): List<Float> {
|
||||
val stops = mutableListOf<Float>()
|
||||
|
||||
val filter: (CameraInfo) -> Boolean = if (currentCamera?.lensType == LensType.FRONT) {
|
||||
{ it.lensType == LensType.FRONT }
|
||||
} else {
|
||||
{ it.lensType != LensType.FRONT && it.lensType != LensType.BACK_MACRO }
|
||||
}
|
||||
|
||||
// 添加各个镜头的固有变焦比例
|
||||
cameras.filter(filter).forEach { camera ->
|
||||
if (camera.intrinsicZoomRatio > 0) {
|
||||
// 避免添加极其接近的变焦倍率(例如 1.0 和 1.0006)
|
||||
if (stops.none { abs(it - camera.intrinsicZoomRatio) < 0.01f }) {
|
||||
stops.add(camera.intrinsicZoomRatio)
|
||||
}
|
||||
}
|
||||
}
|
||||
return stops.sorted()
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算变焦档位
|
||||
*/
|
||||
private fun allZoomStops(
|
||||
lensZoomStops: List<Float>,
|
||||
mainCamera: CameraInfo?,
|
||||
currentCamera: CameraInfo?
|
||||
): List<Float> {
|
||||
val stops = mutableListOf<Float>()
|
||||
stops.addAll(lensZoomStops)
|
||||
|
||||
if (currentCamera?.lensType == LensType.FRONT) {
|
||||
if (stops.none { abs(it - 2f) <= 0.1f }) {
|
||||
stops.add(2f)
|
||||
}
|
||||
return stops.sorted()
|
||||
}
|
||||
|
||||
mainCamera ?: return stops.sorted()
|
||||
|
||||
listOf(35f, 50f, 85f, 200f).forEach { fl ->
|
||||
val zoom = fl / mainCamera.focalLength35mmEquivalent
|
||||
if (stops.none { abs(it - zoom) <= 0.1f }) {
|
||||
stops.add(zoom)
|
||||
}
|
||||
}
|
||||
return stops.sorted()
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据变焦倍率找到最佳镜头
|
||||
*/
|
||||
private fun findOptimalLens(
|
||||
targetZoom: Float,
|
||||
cameras: List<CameraInfo>,
|
||||
currentCameraId: String
|
||||
): CameraInfo? {
|
||||
val currentLensType = cameras.find { it.cameraId == currentCameraId }?.lensType
|
||||
val zoomableCameras =
|
||||
cameras.filter { if (currentLensType == LensType.FRONT) it.lensType == LensType.FRONT else (it.lensType != LensType.FRONT && it.lensType != LensType.BACK_MACRO) }
|
||||
if (zoomableCameras.isEmpty()) return null
|
||||
return zoomableCameras
|
||||
.filter { it.intrinsicZoomRatio <= targetZoom + 0.01f }
|
||||
.sortedByDescending { it.intrinsicZoomRatio }
|
||||
.getOrNull(0)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ZoomRuler(
|
||||
|
||||
@@ -360,7 +360,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
/**
|
||||
* 获取所有后置摄像头
|
||||
*/
|
||||
fun getBackCameras(): List<com.hinnka.mycamera.camera.CameraInfo> {
|
||||
fun getBackCameras(): List<CameraInfo> {
|
||||
return cameraController.getBackCameras()
|
||||
}
|
||||
|
||||
@@ -945,50 +945,78 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
}
|
||||
|
||||
// --- 变焦档位计算逻辑同步自 ZoomControlBar.kt ---
|
||||
|
||||
private fun calculateLensZoomStops(
|
||||
cameras: List<com.hinnka.mycamera.camera.CameraInfo>,
|
||||
currentCamera: com.hinnka.mycamera.camera.CameraInfo
|
||||
/**
|
||||
* 计算变焦档位
|
||||
*/
|
||||
fun calculateLensZoomStops(
|
||||
cameras: List<CameraInfo>,
|
||||
currentCamera: CameraInfo?
|
||||
): List<Float> {
|
||||
val stops = mutableSetOf<Float>()
|
||||
if (currentCamera.lensType == LensType.FRONT) {
|
||||
cameras.filter { it.lensType == LensType.FRONT }
|
||||
.forEach { if (it.intrinsicZoomRatio > 0) stops.add(it.intrinsicZoomRatio) }
|
||||
val stops = mutableListOf<Float>()
|
||||
|
||||
val filter: (CameraInfo) -> Boolean = if (currentCamera?.lensType == LensType.FRONT) {
|
||||
{ it.lensType == LensType.FRONT }
|
||||
} else {
|
||||
cameras.filter { it.lensType != LensType.FRONT && it.lensType != LensType.BACK_MACRO }
|
||||
.forEach { if (it.intrinsicZoomRatio > 0) stops.add(it.intrinsicZoomRatio) }
|
||||
}
|
||||
return stops.toList().sorted()
|
||||
{ it.lensType != LensType.FRONT && it.lensType != LensType.BACK_MACRO }
|
||||
}
|
||||
|
||||
private fun allZoomStops(
|
||||
lensZoomStops: List<Float>,
|
||||
mainCamera: com.hinnka.mycamera.camera.CameraInfo,
|
||||
currentCamera: com.hinnka.mycamera.camera.CameraInfo
|
||||
): List<Float> {
|
||||
val stops = mutableSetOf<Float>()
|
||||
stops.addAll(lensZoomStops)
|
||||
if (currentCamera.lensType == LensType.FRONT) {
|
||||
if (lensZoomStops.none { abs(it - 2f) <= 0.1f }) stops.add(2f)
|
||||
} else {
|
||||
listOf(35f, 50f, 85f, 200f).forEach { fl ->
|
||||
val zoom = fl / mainCamera.focalLength35mmEquivalent
|
||||
if (lensZoomStops.none { abs(it - zoom) <= 0.1f }) stops.add(zoom)
|
||||
// 添加各个镜头的固有变焦比例
|
||||
cameras.filter(filter).forEach { camera ->
|
||||
if (camera.intrinsicZoomRatio > 0) {
|
||||
// 避免添加极其接近的变焦倍率(例如 1.0 和 1.0006)
|
||||
if (stops.none { abs(it - camera.intrinsicZoomRatio) < 0.01f }) {
|
||||
stops.add(camera.intrinsicZoomRatio)
|
||||
}
|
||||
}
|
||||
}
|
||||
return stops.sorted()
|
||||
}
|
||||
|
||||
private fun findOptimalLens(
|
||||
/**
|
||||
* 计算变焦档位
|
||||
*/
|
||||
fun allZoomStops(
|
||||
lensZoomStops: List<Float>,
|
||||
mainCamera: CameraInfo?,
|
||||
currentCamera: CameraInfo?
|
||||
): List<Float> {
|
||||
val stops = mutableListOf<Float>()
|
||||
stops.addAll(lensZoomStops)
|
||||
|
||||
if (currentCamera?.lensType == LensType.FRONT) {
|
||||
if (stops.none { abs(it - 2f) <= 0.1f }) {
|
||||
stops.add(2f)
|
||||
}
|
||||
return stops.sorted()
|
||||
}
|
||||
|
||||
mainCamera ?: return stops.sorted()
|
||||
|
||||
listOf(35f, 50f, 85f, 200f).forEach { fl ->
|
||||
val zoom = fl / mainCamera.focalLength35mmEquivalent
|
||||
if (stops.none { abs(it - zoom) <= 0.1f }) {
|
||||
stops.add(zoom)
|
||||
}
|
||||
}
|
||||
return stops.sorted()
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据变焦倍率找到最佳镜头
|
||||
*/
|
||||
fun findOptimalLens(
|
||||
targetZoom: Float,
|
||||
cameras: List<com.hinnka.mycamera.camera.CameraInfo>,
|
||||
cameras: List<CameraInfo>,
|
||||
currentCameraId: String
|
||||
): com.hinnka.mycamera.camera.CameraInfo? {
|
||||
): CameraInfo? {
|
||||
val currentLensType = cameras.find { it.cameraId == currentCameraId }?.lensType
|
||||
val zoomableCameras =
|
||||
cameras.filter { if (currentLensType == LensType.FRONT) it.lensType == LensType.FRONT else (it.lensType != LensType.FRONT && it.lensType != LensType.BACK_MACRO) }
|
||||
return zoomableCameras.filter { it.intrinsicZoomRatio <= targetZoom }.maxByOrNull { it.intrinsicZoomRatio }
|
||||
if (zoomableCameras.isEmpty()) return null
|
||||
return zoomableCameras
|
||||
.filter { it.intrinsicZoomRatio <= targetZoom + 0.01f }
|
||||
.sortedByDescending { it.intrinsicZoomRatio }
|
||||
.getOrNull(0)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user