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