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 }