From 92674a5b936514f5c40760859f84dcc0b3086ac0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8B=A4=E6=B0=91?= Date: Tue, 21 Apr 2026 17:54:18 +0800 Subject: [PATCH] =?UTF-8?q?fix(camera):=20=E4=BF=AE=E5=A4=8D=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E6=B4=BB=E5=8A=A8=E4=B8=AD=E7=9B=B8=E6=9C=BA=E9=A2=84?= =?UTF-8?q?=E8=A7=88=E5=90=AF=E5=8A=A8=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加了 previewStartAttempted 标志防止重复启动相机预览 - 在多个关键位置增加了调试日志用于追踪相机状态 - 优化了相机预览可用性检测和启动时机 - 修复了相机预览启动失败后的状态重置逻辑 - 改进了相机预览启动时的 UI 可见性控制 - 添加了相机预览停止时的异常处理机制 --- .../nova/brain/glass/ui/InspectionActivity.kt | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/nova/brain/glass/ui/InspectionActivity.kt b/app/src/main/java/com/nova/brain/glass/ui/InspectionActivity.kt index c5c1212..8b2ceae 100644 --- a/app/src/main/java/com/nova/brain/glass/ui/InspectionActivity.kt +++ b/app/src/main/java/com/nova/brain/glass/ui/InspectionActivity.kt @@ -41,10 +41,12 @@ class InspectionActivity : private var isPreviewActive = false private var currentZoomLevel = 1 private var maxZoomLevel = 1 + private var previewStartAttempted = false private val listener = object : OfflineCmdListener { override fun onOfflineCmd(cmd: String) { runOnUiThread { + LogHelper.d("InspectionActivity onOfflineCmd: $cmd") when (cmd) { "退出", "返回", "退回" -> finish() "开始", "开始任务" -> startCapture() @@ -93,6 +95,7 @@ class InspectionActivity : private val previewTextureListener = object : TextureView.SurfaceTextureListener { override fun onSurfaceTextureAvailable(surface: android.graphics.SurfaceTexture, width: Int, height: Int) { + LogHelper.d("Inspection preview surface available: ${width}x$height, requested=$isPreviewRequested") if (isPreviewRequested) { startCameraPreview() } @@ -169,27 +172,43 @@ class InspectionActivity : capturePhoto() return } + LogHelper.d("Inspection startCapture: previewActive=$isPreviewActive, textureAvailable=${binding.cameraPreview.isAvailable}") binding.hint.text = "相机预览启动中,请稍后..." isPreviewRequested = true + previewStartAttempted = false SprayingPhotoManager.clear() + binding.cameraPreviewContainer.visibility = View.VISIBLE + binding.zoomText.visibility = View.VISIBLE if (binding.cameraPreview.isAvailable) { startCameraPreview() } else { - binding.cameraPreviewContainer.visibility = View.VISIBLE - binding.zoomText.visibility = View.VISIBLE + binding.cameraPreview.post { + LogHelper.d("Inspection preview post check: textureAvailable=${binding.cameraPreview.isAvailable}, requested=$isPreviewRequested") + if (isPreviewRequested && binding.cameraPreview.isAvailable) { + startCameraPreview() + } + } } } private fun startCameraPreview() { + if (!isPreviewRequested || previewStartAttempted) { + LogHelper.d("Inspection startCameraPreview skipped: requested=$isPreviewRequested attempted=$previewStartAttempted") + return + } val surfaceTexture = binding.cameraPreview.surfaceTexture ?: return + previewStartAttempted = true val surface = Surface(surfaceTexture) + LogHelper.d("Inspection startCameraPreview invoke startCameraShare") runCatching { GlassMediaServiceHelper.startCameraShare(surface, cameraSurfaceCallback) }.onFailure { isPreviewRequested = false + previewStartAttempted = false binding.cameraPreviewContainer.visibility = View.GONE binding.zoomText.visibility = View.GONE binding.hint.text = "相机预览启动失败,请重试" + LogHelper.e("Inspection startCameraPreview failed: ${it.message}", it) (it.message ?: "相机预览启动失败").showMessage() } } @@ -197,8 +216,12 @@ class InspectionActivity : private fun stopCameraPreview() { isPreviewRequested = false isPreviewActive = false + previewStartAttempted = false + LogHelper.d("Inspection stopCameraPreview") runCatching { GlassMediaServiceHelper.stopCameraShare(cameraSurfaceCallback) + }.onFailure { + LogHelper.e("Inspection stopCameraPreview failed: ${it.message}", it) } binding.cameraPreviewContainer.visibility = View.GONE binding.zoomText.visibility = View.GONE