fix(camera): 修复检查活动中的相机预览功能

- 添加了预览表面管理以避免内存泄漏
- 增加了相机打开和关闭的日志记录
- 修复了相机预览缓冲区大小设置问题
- 添加了相机错误处理的日志记录
- 实现了预览表面的正确释放机制
- 修复了相机预览启动失败时的资源清理
```
这个提交包含在:
徐勤民 2026-04-21 17:56:37 +08:00
父节点 92674a5b93
当前提交 a0aa806f9b

查看文件

@ -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
}