fix: camera zoom
This commit is contained in:
1 parent
a2a9614477
commit
480bb45045
4 files changed
+60
-36
No files matched your search
@@ -170,12 +170,13 @@ class Camera2Controller(private val context: Context) {
|
||||
@SuppressLint("MissingPermission")
|
||||
fun openCamera(surfaceTexture: SurfaceTexture) {
|
||||
val cameraId = _state.value.currentCameraId
|
||||
val aspectRatio = _state.value.aspectRatio
|
||||
if (cameraId.isEmpty()) {
|
||||
Log.e(TAG, "No camera ID set")
|
||||
return
|
||||
}
|
||||
|
||||
val previewSize = CameraUtils.getFixedPreviewSize(context, cameraId)
|
||||
val previewSize = CameraUtils.getFixedPreviewSize(context, cameraId, aspectRatio)
|
||||
surfaceTexture.setDefaultBufferSize(previewSize.width, previewSize.height)
|
||||
|
||||
try {
|
||||
@@ -186,7 +187,7 @@ class Camera2Controller(private val context: Context) {
|
||||
// HighResolutionHelper.logResolutionCapabilities(characteristics)
|
||||
|
||||
// 创建 ImageReader 用于拍照 (使用 YUV 格式)
|
||||
val captureSize = getBestCaptureSize(cameraId)
|
||||
val captureSize = CameraUtils.getBestCaptureSize(context, cameraId, aspectRatio)
|
||||
imageReader = ImageReader.newInstance(
|
||||
captureSize.width,
|
||||
captureSize.height,
|
||||
@@ -726,22 +727,7 @@ class Camera2Controller(private val context: Context) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最佳拍照尺寸
|
||||
*/
|
||||
private fun getBestCaptureSize(cameraId: String): Size {
|
||||
return try {
|
||||
val characteristics = cameraManager.getCameraCharacteristics(cameraId)
|
||||
val map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
|
||||
val sizes = map?.getOutputSizes(ImageFormat.YUV_420_888) ?: arrayOf(Size(1920, 1080))
|
||||
|
||||
// 选择最大的尺寸
|
||||
sizes.maxByOrNull { it.width * it.height } ?: Size(1920, 1080)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to get capture size", e)
|
||||
Size(1920, 1080)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取传感器方向(供外部 YUV 处理使用)
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package com.hinnka.mycamera.camera
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.ImageFormat
|
||||
import android.graphics.SurfaceTexture
|
||||
import android.hardware.camera2.CameraCharacteristics
|
||||
import android.hardware.camera2.CameraManager
|
||||
import android.util.Size
|
||||
import kotlin.math.abs
|
||||
|
||||
/**
|
||||
* 相机工具类
|
||||
@@ -18,7 +20,8 @@ object CameraUtils {
|
||||
*/
|
||||
fun getFixedPreviewSize(
|
||||
context: Context,
|
||||
cameraId: String
|
||||
cameraId: String,
|
||||
aspectRatio: AspectRatio
|
||||
): Size {
|
||||
val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
|
||||
val characteristics = cameraManager.getCameraCharacteristics(cameraId)
|
||||
@@ -26,9 +29,7 @@ object CameraUtils {
|
||||
?: return Size(1920, 1080)
|
||||
|
||||
// 首先获取最大的拍照尺寸,以此作为传感器的原生比例
|
||||
val captureSizes = map.getOutputSizes(android.graphics.ImageFormat.YUV_420_888)
|
||||
val largestCaptureSize = captureSizes?.maxByOrNull { it.width * it.height } ?: Size(4032, 3024)
|
||||
val sensorRatio = largestCaptureSize.width.toFloat() / largestCaptureSize.height
|
||||
val sensorRatio = aspectRatio.getValue(true)
|
||||
|
||||
val previewSizes = map.getOutputSizes(SurfaceTexture::class.java)?.toList() ?: emptyList()
|
||||
|
||||
@@ -47,6 +48,35 @@ object CameraUtils {
|
||||
// 如果没有比例完全匹配的,则选择高度 >= 1080 中最小的一个(之前的逻辑)
|
||||
return previewSizes.filter { it.height >= 1080 }.minByOrNull { it.width } ?: Size(1920, 1080)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最佳拍照尺寸
|
||||
*/
|
||||
fun getBestCaptureSize(context: Context, cameraId: String, aspectRatio: AspectRatio): Size {
|
||||
return try {
|
||||
val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
|
||||
val characteristics = cameraManager.getCameraCharacteristics(cameraId)
|
||||
val map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
|
||||
val sizes = map?.getOutputSizes(ImageFormat.YUV_420_888) ?: arrayOf(Size(1920, 1080))
|
||||
val sensorRatio = aspectRatio.getValue(true)
|
||||
|
||||
// 寻找比例最匹配传感器原生比例
|
||||
val matchingSizes = sizes.filter {
|
||||
val ratio = it.width.toFloat() / it.height
|
||||
abs(ratio - sensorRatio) < 0.01
|
||||
}
|
||||
|
||||
val bestMatching = matchingSizes.filter { it.height >= 2160 }.maxByOrNull { it.width * it.height }
|
||||
|
||||
if (bestMatching != null) return bestMatching
|
||||
|
||||
// 选择最大的尺寸
|
||||
sizes.maxByOrNull { it.width * it.height } ?: Size(1920, 1080)
|
||||
} catch (e: Exception) {
|
||||
// Log.e(TAG, "Failed to get capture size", e)
|
||||
Size(1920, 1080)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化快门速度显示
|
||||
|
||||
@@ -97,7 +97,7 @@ fun CameraScreen(
|
||||
}
|
||||
}
|
||||
|
||||
val previewSize = CameraUtils.getFixedPreviewSize(context, state.currentCameraId)
|
||||
val previewSize by remember(state) { mutableStateOf(CameraUtils.getFixedPreviewSize(context, state.currentCameraId, state.aspectRatio)) }
|
||||
|
||||
Column(
|
||||
modifier = modifier
|
||||
|
||||
@@ -56,7 +56,7 @@ fun ZoomControlBar(
|
||||
|
||||
// 根据可用相机计算变焦档位
|
||||
val lensZoomStops = calculateLensZoomStops(availableCameras, currentCamera)
|
||||
val zoomStops = (lensZoomStops + extraZoomStops(availableCameras, currentCamera)).sorted()
|
||||
val zoomStops = allZoomStops(lensZoomStops, mainCamera, currentCamera)
|
||||
|
||||
Box(
|
||||
modifier = modifier
|
||||
@@ -156,22 +156,30 @@ private fun calculateLensZoomStops(
|
||||
/**
|
||||
* 计算变焦档位
|
||||
*/
|
||||
private fun extraZoomStops(
|
||||
cameras: List<CameraInfo>,
|
||||
private fun allZoomStops(
|
||||
lensZoomStops: List<Float>,
|
||||
mainCamera: CameraInfo?,
|
||||
currentCamera: CameraInfo?
|
||||
): List<Float> {
|
||||
val stops = mutableSetOf<Float>()
|
||||
if (currentCamera?.lensType == LensType.FRONT) {
|
||||
stops.add(2f)
|
||||
return stops.toList()
|
||||
}
|
||||
val mainCamera = cameras.find { it.lensType == LensType.BACK_MAIN } ?: return stops.toList()
|
||||
stops.addAll(lensZoomStops)
|
||||
|
||||
stops.add(35f / mainCamera.focalLength35mmEquivalent)
|
||||
stops.add(50f / mainCamera.focalLength35mmEquivalent)
|
||||
stops.add(85f / mainCamera.focalLength35mmEquivalent)
|
||||
stops.add(200f / mainCamera.focalLength35mmEquivalent)
|
||||
return stops.toList()
|
||||
if (currentCamera?.lensType == LensType.FRONT) {
|
||||
if (lensZoomStops.find { abs(it - 2f) <= 0.1f } == null) {
|
||||
stops.add(2f)
|
||||
}
|
||||
return stops.sorted()
|
||||
}
|
||||
|
||||
mainCamera ?: return stops.sorted()
|
||||
|
||||
listOf(35f, 50f, 85f, 200f).forEach { fl ->
|
||||
val zoom = fl / mainCamera.focalLength35mmEquivalent
|
||||
if (lensZoomStops.find { abs(it - zoom) <= 0.1f } == null) {
|
||||
stops.add(zoom)
|
||||
}
|
||||
}
|
||||
return stops.sorted()
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user