From a0aa806f9be9ab5dd98a27323dd62b314adcfdda 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:56:37 +0800 Subject: [PATCH] =?UTF-8?q?```=20fix(camera):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E6=B4=BB=E5=8A=A8=E4=B8=AD=E7=9A=84=E7=9B=B8?= =?UTF-8?q?=E6=9C=BA=E9=A2=84=E8=A7=88=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加了预览表面管理以避免内存泄漏 - 增加了相机打开和关闭的日志记录 - 修复了相机预览缓冲区大小设置问题 - 添加了相机错误处理的日志记录 - 实现了预览表面的正确释放机制 - 修复了相机预览启动失败时的资源清理 ``` --- .../nova/brain/glass/ui/InspectionActivity.kt | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 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 8b2ceae..589bb28 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 @@ -42,6 +42,7 @@ class InspectionActivity : private var currentZoomLevel = 1 private var maxZoomLevel = 1 private var previewStartAttempted = false + private var previewSurface: Surface? = null private val listener = object : OfflineCmdListener { override fun onOfflineCmd(cmd: String) { @@ -61,6 +62,7 @@ class InspectionActivity : private val cameraSurfaceCallbackId = UUID.randomUUID().toString() private val cameraSurfaceCallback = object : ICameraSurfaceCallback.Stub() { override fun onCameraOpened(width: Int, height: Int) { + LogHelper.d("Inspection preview onCameraOpened: ${width}x$height") isPreviewActive = true currentZoomLevel = GlassMediaServiceHelper.getZoomLevel().coerceAtLeast(1) maxZoomLevel = GlassMediaServiceHelper.getMaxZoomLevel().coerceAtLeast(1) @@ -73,6 +75,7 @@ class InspectionActivity : } override fun onCameraClosed() { + LogHelper.d("Inspection preview onCameraClosed") isPreviewActive = false runOnUiThread { binding.cameraPreviewContainer.visibility = View.GONE @@ -81,6 +84,7 @@ class InspectionActivity : } override fun onError(code: Int, message: String?) { + LogHelper.d("Inspection preview onError: code=$code msg=$message") isPreviewActive = false runOnUiThread { binding.cameraPreviewContainer.visibility = View.GONE @@ -197,14 +201,20 @@ class InspectionActivity : return } val surfaceTexture = binding.cameraPreview.surfaceTexture ?: return + val width = binding.cameraPreview.width.coerceAtLeast(1) + val height = binding.cameraPreview.height.coerceAtLeast(1) + surfaceTexture.setDefaultBufferSize(width, height) previewStartAttempted = true - val surface = Surface(surfaceTexture) - LogHelper.d("Inspection startCameraPreview invoke startCameraShare") + previewSurface?.release() + previewSurface = Surface(surfaceTexture) + LogHelper.d("Inspection startCameraPreview invoke startCameraShare, buffer=${width}x$height") runCatching { - GlassMediaServiceHelper.startCameraShare(surface, cameraSurfaceCallback) + GlassMediaServiceHelper.startCameraShare(previewSurface!!, cameraSurfaceCallback) }.onFailure { isPreviewRequested = false previewStartAttempted = false + previewSurface?.release() + previewSurface = null binding.cameraPreviewContainer.visibility = View.GONE binding.zoomText.visibility = View.GONE binding.hint.text = "相机预览启动失败,请重试" @@ -223,6 +233,8 @@ class InspectionActivity : }.onFailure { LogHelper.e("Inspection stopCameraPreview failed: ${it.message}", it) } + previewSurface?.release() + previewSurface = null binding.cameraPreviewContainer.visibility = View.GONE binding.zoomText.visibility = View.GONE }