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) {
|
if (activePanel == ActivePanel.NONE) {
|
||||||
// Zoom Control Bar (Overlay at bottom of preview)
|
// Zoom Control Bar (Overlay at bottom of preview)
|
||||||
ZoomControlBar(
|
ZoomControlBar(
|
||||||
|
viewModel = viewModel,
|
||||||
zoomRatio = viewModel.zoomRatioByMain,
|
zoomRatio = viewModel.zoomRatioByMain,
|
||||||
availableCameras = state.availableCameras,
|
availableCameras = state.availableCameras,
|
||||||
currentCameraId = state.getCurrentCameraInfo()?.cameraId ?: "0",
|
currentCameraId = state.getCurrentCameraInfo()?.cameraId ?: "0",
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import androidx.compose.ui.unit.sp
|
|||||||
import com.hinnka.mycamera.R
|
import com.hinnka.mycamera.R
|
||||||
import com.hinnka.mycamera.camera.CameraInfo
|
import com.hinnka.mycamera.camera.CameraInfo
|
||||||
import com.hinnka.mycamera.camera.LensType
|
import com.hinnka.mycamera.camera.LensType
|
||||||
|
import com.hinnka.mycamera.viewmodel.CameraViewModel
|
||||||
import kotlin.math.abs
|
import kotlin.math.abs
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
@@ -39,6 +40,7 @@ enum class ZoomDisplayMode {
|
|||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ZoomControlBar(
|
fun ZoomControlBar(
|
||||||
|
viewModel: CameraViewModel,
|
||||||
zoomRatio: Float,
|
zoomRatio: Float,
|
||||||
availableCameras: List<CameraInfo>,
|
availableCameras: List<CameraInfo>,
|
||||||
currentCameraId: String,
|
currentCameraId: String,
|
||||||
@@ -59,8 +61,8 @@ fun ZoomControlBar(
|
|||||||
availableCameras.find { it.lensType == if (currentCamera?.lensType == LensType.FRONT) LensType.FRONT else LensType.BACK_MAIN }
|
availableCameras.find { it.lensType == if (currentCamera?.lensType == LensType.FRONT) LensType.FRONT else LensType.BACK_MAIN }
|
||||||
|
|
||||||
// 根据可用相机计算变焦档位
|
// 根据可用相机计算变焦档位
|
||||||
val lensZoomStops = calculateLensZoomStops(availableCameras, currentCamera)
|
val lensZoomStops = viewModel.calculateLensZoomStops(availableCameras, currentCamera)
|
||||||
val zoomStops = allZoomStops(lensZoomStops, mainCamera, currentCamera)
|
val zoomStops = viewModel.allZoomStops(lensZoomStops, mainCamera, currentCamera)
|
||||||
|
|
||||||
Box(
|
Box(
|
||||||
modifier = modifier
|
modifier = modifier
|
||||||
@@ -118,7 +120,7 @@ fun ZoomControlBar(
|
|||||||
displayMode = displayMode,
|
displayMode = displayMode,
|
||||||
onZoomChange = { newZoom ->
|
onZoomChange = { newZoom ->
|
||||||
// 检查是否需要切换镜头
|
// 检查是否需要切换镜头
|
||||||
val camera = findOptimalLens(newZoom, availableCameras, currentCameraIdState)
|
val camera = viewModel.findOptimalLens(newZoom, availableCameras, currentCameraIdState)
|
||||||
if (camera != null && camera.cameraId != currentCameraIdState) {
|
if (camera != null && camera.cameraId != currentCameraIdState) {
|
||||||
onLensSwitch(camera.cameraId)
|
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
|
@Composable
|
||||||
fun ZoomRuler(
|
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()
|
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>,
|
fun calculateLensZoomStops(
|
||||||
currentCamera: com.hinnka.mycamera.camera.CameraInfo
|
cameras: List<CameraInfo>,
|
||||||
|
currentCamera: CameraInfo?
|
||||||
): List<Float> {
|
): List<Float> {
|
||||||
val stops = mutableSetOf<Float>()
|
val stops = mutableListOf<Float>()
|
||||||
if (currentCamera.lensType == LensType.FRONT) {
|
|
||||||
cameras.filter { it.lensType == LensType.FRONT }
|
val filter: (CameraInfo) -> Boolean = if (currentCamera?.lensType == LensType.FRONT) {
|
||||||
.forEach { if (it.intrinsicZoomRatio > 0) stops.add(it.intrinsicZoomRatio) }
|
{ it.lensType == LensType.FRONT }
|
||||||
} else {
|
} else {
|
||||||
cameras.filter { it.lensType != LensType.FRONT && it.lensType != LensType.BACK_MACRO }
|
{ it.lensType != LensType.FRONT && it.lensType != LensType.BACK_MACRO }
|
||||||
.forEach { if (it.intrinsicZoomRatio > 0) stops.add(it.intrinsicZoomRatio) }
|
|
||||||
}
|
|
||||||
return stops.toList().sorted()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun allZoomStops(
|
// 添加各个镜头的固有变焦比例
|
||||||
lensZoomStops: List<Float>,
|
cameras.filter(filter).forEach { camera ->
|
||||||
mainCamera: com.hinnka.mycamera.camera.CameraInfo,
|
if (camera.intrinsicZoomRatio > 0) {
|
||||||
currentCamera: com.hinnka.mycamera.camera.CameraInfo
|
// 避免添加极其接近的变焦倍率(例如 1.0 和 1.0006)
|
||||||
): List<Float> {
|
if (stops.none { abs(it - camera.intrinsicZoomRatio) < 0.01f }) {
|
||||||
val stops = mutableSetOf<Float>()
|
stops.add(camera.intrinsicZoomRatio)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return stops.sorted()
|
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,
|
targetZoom: Float,
|
||||||
cameras: List<com.hinnka.mycamera.camera.CameraInfo>,
|
cameras: List<CameraInfo>,
|
||||||
currentCameraId: String
|
currentCameraId: String
|
||||||
): com.hinnka.mycamera.camera.CameraInfo? {
|
): CameraInfo? {
|
||||||
val currentLensType = cameras.find { it.cameraId == currentCameraId }?.lensType
|
val currentLensType = cameras.find { it.cameraId == currentCameraId }?.lensType
|
||||||
val zoomableCameras =
|
val zoomableCameras =
|
||||||
cameras.filter { if (currentLensType == LensType.FRONT) it.lensType == LensType.FRONT else (it.lensType != LensType.FRONT && it.lensType != LensType.BACK_MACRO) }
|
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