This commit is contained in:
2025-12-19 17:34:31 +08:00
parent af56fb4aeb
commit 960b429e69
18 changed files with 294 additions and 181 deletions
@@ -32,6 +32,7 @@ import com.sw.dualscreen.ext.gone
import com.sw.dualscreen.ext.load
import com.sw.dualscreen.ext.visible
import com.sw.dualscreen.model.response.ChargeModeEvent
import com.sw.dualscreen.model.response.ClickBackEvent
import com.sw.dualscreen.model.response.FoodInfo
import com.sw.dualscreen.model.response.FoodOrder
import com.sw.dualscreen.model.response.PaySuccessEvent
@@ -114,6 +115,8 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
private var isRecognitionFood = true
private var isFirstOpen = true
private var isStartRecognize = false
override fun getViewModel(): BaseViewModel {
return viewModel
}
@@ -240,17 +243,23 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
}
pauseAnalysis()
recognizeWeight = lastWeight
Timber.tag(TAG)
.d("registerDataChange,副屏updateFood调用前耗时:${System.currentTimeMillis() - startTime}")
startTime = System.currentTimeMillis()
presentation?.updateFood(foodInfo)
binding.tvFoodName.text = foodInfo.foodName
binding.previewView.gone()
binding.ivImg.visible()
if (foodInfo.foodImg.isNullOrBlank()) {
takePhoto { imgUri ->
takePhoto(successCallback = { imgUri ->
binding.ivImg.load(imgUri)
presentation?.updateFood(foodInfo.copy(foodImg = imgUri.toString()))
}
}, failureCallback = {
})
} else {
binding.ivImg.load(foodInfo.foodImg)
}
binding.ivImg.load(foodInfo.foodImg ?: foodInfo.photoUri)
//pauseAnalysis()
}
@@ -283,6 +292,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
SensorScaleUtils.addWeightListener { weight ->
Timber.d("registerDataChange weight = $weight")
if (isPageVisible.not() || isStartRecognize.not()) return@addWeightListener
val isWeightChange = weight - lastWeight > 0.05
recognizeByWeight(weight, isWeightChange)
}
@@ -308,15 +318,23 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
lastWeight = weight
}
private var startTime = 0L
fun recognizeFood() {
startTime = System.currentTimeMillis()
LightManager.openRedLight()
LightManager.openGreenLight()
takePhoto { photoUri ->
showWaitingDialog("识别中,请稍后……")
takePhoto(successCallback = { photoUri ->
lastPhotoUri = photoUri
Timber.tag(TAG).d("main,takePhoto耗时:${System.currentTimeMillis() - startTime}")
startTime = System.currentTimeMillis()
Timber.d("registerDataChange photoUri = ${photoUri.path}")
// viewModel.getIdentifiedFoodList()
ImageUtil.uriToBitmap(this, photoUri)?.let { bitmap ->
Timber.tag(TAG).d("main,uriToBitmap耗时:${System.currentTimeMillis() - startTime}")
startTime = System.currentTimeMillis()
Timber.d("registerDataChange photoUri 拿到bitmap")
// val bmp = BitmapCropper.cropCenter(bitmap, 1300, 900)
Timber.d("registerDataChange photoUri bitmap裁剪完成")
@@ -325,19 +343,21 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
this,
"IMG_CROP_${System.currentTimeMillis()}.jpg"
)
Timber.tag(TAG)
.d("main,saveToAppFilesDir耗时:${System.currentTimeMillis() - startTime}")
startTime = System.currentTimeMillis()
Timber.d("registerDataChange photoUri bitmap保存文件路径:${file?.absolutePath}")
//val nameList = FoodModule.queryFood(bitmap)
val scoreList = FoodModule.getFoodScoreList(bitmap)
Timber.d(
"registerDataChange photoUri 拿到识别数据:${
GsonUtils.toJson(
scoreList
)
}"
)
val recDataJson = GsonUtils.toJson(scoreList)
Timber.tag(TAG)
.d("main,getFoodScoreList耗时:${System.currentTimeMillis() - startTime}")
startTime = System.currentTimeMillis()
Timber.d("registerDataChange photoUri 拿到识别数据:$recDataJson")
val nameList = scoreList.map { it.name }
val foodName = nameList.joinToString(separator = ",")
hideWaitingDialog()
Timber.d("registerDataChange photoUri 识别数据名称:$foodName")
if (TextUtils.isEmpty(foodName)) {
LightManager.closeGreenLight()
@@ -350,15 +370,28 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
}
} else {
//binding.layoutRescan.visibility = View.GONE
showWaitingDialog("正在查询菜品信息,请稍后……")
viewModel.getFoodInfo(foodName) { list ->
runOnUiThread {
hideWaitingDialog()
Timber.tag(TAG)
.d("main,getFoodInfo耗时:${System.currentTimeMillis() - startTime}")
startTime = System.currentTimeMillis()
updateFoodInfo(list.toMutableList(), scoreList)
}
}
}
}
}
}, failureCallback = {
runOnUiThread {
hideWaitingDialog()
ToastUtils.showToast("拍照异常,请手动放置后重新识别")
}
// binding.root.postDelayed({
// recognizeFood()
// }, 500)
})
}
@SuppressLint("NotifyDataSetChanged")
@@ -447,6 +480,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
if (isAnalyzing) {
val bitmap = imageProxy.toBitmap()
presentation?.updateImage(bitmap) // 更新副屏
isStartRecognize = true
}
imageProxy.close() // 必须关闭以释放资源
}
@@ -518,7 +552,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
}
// 拍照功能
private fun takePhoto(callback: (Uri) -> Unit) {
private fun takePhoto(successCallback: (Uri) -> Unit, failureCallback: () -> Unit) {
Timber.d("takePhoto")
// 临时停止分析以避免干扰
isAnalyzing = false
@@ -545,7 +579,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
Timber.d("takePhoto savedUri = $savedUri")
// ToastUtils.showToast("照片保存成功: $savedUri")
if (savedUri != null) {
callback(savedUri)
successCallback(savedUri)
}
}
// 恢复图像分析
@@ -554,6 +588,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
override fun onError(exception: ImageCaptureException) {
Timber.e("拍照失败: ${exception.message}")
failureCallback()
//runOnUiThread {
// ToastUtils.showToast("拍照失败: ${exception.message}")
//}
@@ -631,14 +666,16 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
binding.tvTitleTime.text = dateTime
}
private var isPageVisible = true
override fun onResume() {
super.onResume()
Timber.tag("onResume").d("=================onResume================")
isPageVisible = true
// 启动定时器
handler.postDelayed(timeoutRunnable, TIME_OUT)
if (isFirstOpen) {
//首次
//首次
setupSecondaryDisplay()
presentation?.step1FoodRecognizing()
isFirstOpen = false
@@ -650,11 +687,14 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
setupSecondaryDisplay()
presentation?.step1FoodRecognizing()
} else {
//已显示副屏,则进行更新
val weight = lastWeight
lastWeight = 0.0
presentation?.hideRecImage()
recognizeByWeight(weight, true)
if (isRefreshPage) {
//已显示副屏,则进行更新
val weight = lastWeight
lastWeight = 0.0
presentation?.hideRecImage()
recognizeByWeight(weight, true)
}
isRefreshPage = !isRefreshPage
}
}
@@ -674,6 +714,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
override fun onPause() {
super.onPause()
isPageVisible = false
// 取消定时器
handler.removeCallbacks(timeoutRunnable)
}
@@ -770,16 +811,24 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
@Subscribe(threadMode = ThreadMode.MAIN)
fun onPaySuccessEvent(event: PaySuccessEvent) {
val chargeMode = SPUtil.getInstance().get(GlobalKey.KEY_CHARGE_MODE, 0)?:0
isRefreshPage = true
val chargeMode = SPUtil.getInstance().get(GlobalKey.KEY_CHARGE_MODE, 0) ?: 0
presentation?.hideNutritionView(chargeMode)
presentation?.cancel()
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun onChargeModeEvent(event: ChargeModeEvent) {
isRefreshPage = true
isStartRecognize = false
presentation?.hideNutritionView(event.chargeMode)
// presentation?.cancel()
}
private var isRefreshPage = false
@Subscribe(threadMode = ThreadMode.MAIN)
fun onClickBackEvent(event: ClickBackEvent) {
isRefreshPage = false
}
private fun clickPayButton() {
if (checkedItem == null) {