添加了模拟识别菜品功能

This commit is contained in:
zxj
2025-08-13 11:29:08 +08:00
parent e93550a24b
commit f055441340
5 changed files with 63 additions and 39 deletions
@@ -31,6 +31,7 @@ import com.sw.dualscreen.ext.dp
import com.sw.dualscreen.model.response.FoodInfo
import com.sw.dualscreen.presentation.SecondaryScreenPresentation
import com.sw.dualscreen.sdk.SensorScaleUtils
import com.sw.dualscreen.utils.Debouncer
import com.sw.dualscreen.utils.GlideUtils
import com.sw.dualscreen.view.CustomBottomSheetDialog
import com.sw.dualscreen.viewmodel.BaseViewModel
@@ -55,6 +56,8 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
private var canIdentify: Boolean = false
private var bottomSheetDialog: CustomBottomSheetDialog? = null
private var lastWeight: Double = 0.0
private var lastPhotoUri: Uri? = null // 最后拍照的图片
private var debouncer = Debouncer(2000)
override fun getViewModel(): BaseViewModel {
return viewModel
@@ -87,6 +90,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
binding.previewView.clipToOutline = true
binding.tvToSearch.setOnClickListener {
bottomSheetDialog = CustomBottomSheetDialog.newInstance(viewModel) {
it.photoUri = null
updateCurrentFood(it)
}
bottomSheetDialog!!.show(supportFragmentManager, "CustomBottomSheetDialog")
@@ -140,7 +144,11 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
presentation?.updateFood(foodInfo.copy(imgUrl = imgUri.toString()))
}
} else {
GlideUtils.loadRoundCornerImage(this, foodInfo.imgUrl, binding.ivImg, 12)
if (foodInfo.photoUri != null) {
GlideUtils.loadRoundCornerImage(this, foodInfo.photoUri, binding.ivImg, 12)
} else {
GlideUtils.loadRoundCornerImage(this, foodInfo.imgUrl, binding.ivImg, 12)
}
}
pauseAnalysis()
} else {
@@ -156,20 +164,28 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
super.registerDataChange()
lifecycleScope.launch {
viewModel.identifiedFoodInfoList.collect { list ->
binding.recyclerview
adapter.updateData(list)
// presentation.step2()
if (list.isNotEmpty()) {
checkedItem = list[0]
checkedItem!!.photoUri = lastPhotoUri
updateCurrentFood(checkedItem)
lastPhotoUri = null
}
}
}
SensorScaleUtils.startScale { weight ->
Timber.d("registerDataChange weight = $weight")
presentation?.updateWeight(weight)
if (weight > 0.0) {
if (canIdentify) {
canIdentify = false
viewModel.getIdentifiedFoodList()
if (weight - lastWeight > 0.1 && presentation?.mealPickupMode == 0) { // 大于100g
debouncer.debounce {
takePhoto { photoUri ->
lastPhotoUri = photoUri
viewModel.getIdentifiedFoodList()
}
}
}
lastWeight = weight
}
}
@@ -191,6 +207,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
//
this.llRoot.setOnClickListener {
Timber.d("itemClick ${item.foodName}, position = $position")
item.photoUri = null
checkedItem = item
adapter.notifyDataSetChanged()
updateCurrentFood(item)
@@ -319,7 +336,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
runOnUiThread {
val savedUri = outputFileResults.savedUri
Timber.d("takePhoto savedUri = $savedUri")
ToastUtils.showToast("照片保存成功: $savedUri")
// ToastUtils.showToast("照片保存成功: $savedUri")
if (savedUri != null) {
callback(savedUri)
}