refactor: 改进 CameraHelper 资源清理的错误处理

- 添加 stopRepeating() 和 abortCaptures() 调用
- 为每个资源关闭操作添加独立的 try-catch
- 确保即使部分资源关闭失败,其他资源仍会被清理

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
let5sne.win10
2026-02-12 22:39:54 +08:00
parent e9741b4dd2
commit c68ed15ed5

View File

@@ -149,11 +149,32 @@ class CameraHelper(
} }
private fun closeCamera() { private fun closeCamera() {
captureSession?.close() try {
captureSession?.stopRepeating()
captureSession?.abortCaptures()
} catch (e: Exception) {
e.printStackTrace()
}
try {
captureSession?.close()
} catch (e: Exception) {
e.printStackTrace()
}
captureSession = null captureSession = null
imageReader?.close()
try {
imageReader?.close()
} catch (e: Exception) {
e.printStackTrace()
}
imageReader = null imageReader = null
cameraDevice?.close()
try {
cameraDevice?.close()
} catch (e: Exception) {
e.printStackTrace()
}
cameraDevice = null cameraDevice = null
} }
} }