fix(activity): 修复 PrepareCookActivity 相机未就绪时触发拍照导致报错的问题
This commit is contained in:
@@ -160,12 +160,14 @@ class PrepareCookActivity : BaseActivity() {
|
||||
|
||||
private val isTakingPhoto = AtomicBoolean(false)
|
||||
private var lastWeight = 0.0
|
||||
private var currentWeight = 0.0
|
||||
|
||||
/**
|
||||
* 重量稳定时触发菜品识别
|
||||
* @param weight 当前稳定重量
|
||||
*/
|
||||
private fun recognizeFood(weight: Double) {
|
||||
this.currentWeight = weight
|
||||
if (weight < WEIGHT_RECOGNIZE_VALUE) {
|
||||
if (recognizeDialog.isShowing) recognizeDialog.dismiss()
|
||||
return
|
||||
@@ -176,7 +178,6 @@ class PrepareCookActivity : BaseActivity() {
|
||||
succCallback = cameraSuccessCallback,
|
||||
failCallback = cameraFailureCallback
|
||||
)
|
||||
lastWeight = weight
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -500,6 +501,7 @@ class PrepareCookActivity : BaseActivity() {
|
||||
}
|
||||
recognizeDialog.loadData(foodList)
|
||||
isTakingPhoto.set(false)
|
||||
lastWeight = currentWeight
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,16 +6,22 @@ import androidx.camera.core.CameraSelector
|
||||
import androidx.camera.view.CameraController
|
||||
import androidx.camera.view.LifecycleCameraController
|
||||
import androidx.camera.view.PreviewView
|
||||
import androidx.core.content.ContextCompat
|
||||
|
||||
class CameraUtils(private var activity: ComponentActivity) {
|
||||
|
||||
private var cameraController: LifecycleCameraController? = null
|
||||
private var photoCaptureHelper: PhotoCaptureHelper? = null
|
||||
private var failCallback: ((msg: String) -> Unit)? = null
|
||||
private var isCameraReady = false
|
||||
|
||||
// private var isCameraReady = false
|
||||
fun takePhoto(succCallback: (Uri) -> Unit, failCallback: (msg: String) -> Unit = { }) {
|
||||
this.failCallback = failCallback
|
||||
// 相机未就绪时静默返回,避免触发错误提示
|
||||
if (!isCameraReady) {
|
||||
failCallback("")
|
||||
return
|
||||
}
|
||||
cameraController?.let {
|
||||
if (photoCaptureHelper == null) {
|
||||
initCaptureHelper()
|
||||
@@ -74,10 +80,16 @@ class CameraUtils(private var activity: ComponentActivity) {
|
||||
}
|
||||
|
||||
fun bind() {
|
||||
isCameraReady = false
|
||||
cameraController?.bindToLifecycle(activity)
|
||||
// 监听相机初始化完成,就绪后才允许拍照
|
||||
cameraController?.initializationFuture?.addListener({
|
||||
isCameraReady = true
|
||||
}, ContextCompat.getMainExecutor(activity))
|
||||
}
|
||||
|
||||
fun unbind() {
|
||||
isCameraReady = false
|
||||
cameraController?.unbind()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user