功能优化
This commit is contained in:
@@ -32,8 +32,10 @@ import com.sw.dualscreen.adapter.dpToPx
|
||||
import com.sw.dualscreen.databinding.ActivityMainBinding
|
||||
import com.sw.dualscreen.databinding.ItemFoodInfoBinding
|
||||
import com.sw.dualscreen.ext.dp
|
||||
import com.sw.dualscreen.ext.format2String
|
||||
import com.sw.dualscreen.model.response.FoodInfo
|
||||
import com.sw.dualscreen.objbox.FoodModule
|
||||
import com.sw.dualscreen.objbox.FoodModule.IdNameScore
|
||||
import com.sw.dualscreen.presentation.SecondaryScreenPresentation
|
||||
import com.sw.dualscreen.sdk.SensorScaleUtils
|
||||
import com.sw.dualscreen.utils.AssetsTool
|
||||
@@ -54,6 +56,7 @@ import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import java.util.concurrent.Executors
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
/**
|
||||
* 服务员显示界面
|
||||
@@ -138,6 +141,8 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
updateCurrentFood(null)
|
||||
isRecognitionFood = true
|
||||
presentation?.updateMealPickupMode(position)
|
||||
|
||||
SensorScaleUtils.zero()
|
||||
}
|
||||
|
||||
override fun onNothingSelected(parent: AdapterView<*>?) {}
|
||||
@@ -156,6 +161,9 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
binding.tvFoodName.setOnClickListener {
|
||||
SensorScaleUtils.zero()
|
||||
}
|
||||
binding.tvRescan.setOnClickListener {
|
||||
debouncer.debounce { recognizeFood() }
|
||||
}
|
||||
}
|
||||
|
||||
fun updateCurrentFood(foodInfo: FoodInfo?) {
|
||||
@@ -189,19 +197,11 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
|
||||
override fun registerDataChange() {
|
||||
super.registerDataChange()
|
||||
lifecycleScope.launch {
|
||||
viewModel.identifiedFoodInfoList2.collect { list ->
|
||||
Timber.d("registerDataChange识别后查询接口数据:${GsonUtils.toJson(list)}")
|
||||
adapter.updateData(list)
|
||||
if (list.isNotEmpty()) {
|
||||
checkedItem = list[0]
|
||||
checkedItem!!.photoUri = lastPhotoUri
|
||||
updateCurrentFood(checkedItem)
|
||||
lastPhotoUri = null
|
||||
LightManager.closeRedLight()
|
||||
}
|
||||
}
|
||||
}
|
||||
// lifecycleScope.launch {
|
||||
// viewModel.identifiedFoodInfoList2.collect { list ->
|
||||
// updateFoodInfo(list)
|
||||
// }
|
||||
// }
|
||||
lifecycleScope.launch {
|
||||
viewModel.identifiedFoodInfoList.collect { list ->
|
||||
adapter.updateData(list)
|
||||
@@ -217,7 +217,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
Timber.d("registerDataChange weight = $weight")
|
||||
presentation?.updateWeight(weight)
|
||||
|
||||
if (weight <= 0.005 && presentation?.mealPickupMode == 1) {//余量取餐,检测到秤上没有东西,重新启动识别菜品
|
||||
if (weight <= 0.005) {//余量取餐,检测到秤上没有东西,重新启动识别菜品 && presentation?.mealPickupMode == 1
|
||||
isRecognitionFood = true
|
||||
}
|
||||
|
||||
@@ -226,42 +226,85 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
isRecognitionFood = false
|
||||
}
|
||||
debouncer.debounce {
|
||||
LightManager.openRedLight()
|
||||
LightManager.openGreenLight()
|
||||
takePhoto { photoUri ->
|
||||
lastPhotoUri = photoUri
|
||||
Timber.d("registerDataChange photoUri = ${photoUri.path}")
|
||||
// viewModel.getIdentifiedFoodList()
|
||||
|
||||
ImageUtil.uriToBitmap(this, photoUri)?.let { bitmap ->
|
||||
Timber.d("registerDataChange photoUri 拿到bitmap")
|
||||
// val bmp = BitmapCropper.cropCenter(bitmap, 1300, 900)
|
||||
Timber.d("registerDataChange photoUri bitmap裁剪完成")
|
||||
val file = BitmapSaver.saveToAppFilesDir(
|
||||
bitmap,
|
||||
this,
|
||||
"IMG_CROP_${System.currentTimeMillis()}.jpg"
|
||||
)
|
||||
Timber.d("registerDataChange photoUri bitmap保存文件路径:${file?.absolutePath}")
|
||||
val resultList = FoodModule.queryFood(bitmap)
|
||||
Timber.d("registerDataChange photoUri 拿到识别数据")
|
||||
val foodName = resultList.joinToString(separator = ",")
|
||||
Timber.d("registerDataChange photoUri 识别数据名称:$foodName")
|
||||
viewModel.getFoodInfo(foodName)
|
||||
}
|
||||
}
|
||||
recognizeFood()
|
||||
}
|
||||
}
|
||||
lastWeight = weight
|
||||
}
|
||||
}
|
||||
|
||||
private fun recognizeFood() {
|
||||
LightManager.openRedLight()
|
||||
LightManager.openGreenLight()
|
||||
takePhoto { photoUri ->
|
||||
lastPhotoUri = photoUri
|
||||
Timber.d("registerDataChange photoUri = ${photoUri.path}")
|
||||
// viewModel.getIdentifiedFoodList()
|
||||
|
||||
ImageUtil.uriToBitmap(this, photoUri)?.let { bitmap ->
|
||||
Timber.d("registerDataChange photoUri 拿到bitmap")
|
||||
// val bmp = BitmapCropper.cropCenter(bitmap, 1300, 900)
|
||||
Timber.d("registerDataChange photoUri bitmap裁剪完成")
|
||||
val file = BitmapSaver.saveToAppFilesDir(
|
||||
bitmap,
|
||||
this,
|
||||
"IMG_CROP_${System.currentTimeMillis()}.jpg"
|
||||
)
|
||||
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 nameList = scoreList.map { it.name }
|
||||
val foodName = nameList.joinToString(separator = ",")
|
||||
|
||||
Timber.d("registerDataChange photoUri 识别数据名称:$foodName")
|
||||
if (TextUtils.isEmpty(foodName)) {
|
||||
LightManager.closeGreenLight()
|
||||
LightManager.closeRedLight()
|
||||
binding.layoutRescan.visibility = View.VISIBLE
|
||||
} else {
|
||||
binding.layoutRescan.visibility = View.GONE
|
||||
viewModel.getFoodInfo(foodName) { list ->
|
||||
runOnUiThread {
|
||||
updateFoodInfo(list, scoreList)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateFoodInfo(list: List<FoodInfo>, scoreList: List<IdNameScore>) {
|
||||
Timber.d("registerDataChange识别后查询接口数据:${GsonUtils.toJson(list)}")
|
||||
list.forEach { foodInfo ->
|
||||
val scoreItem = scoreList.firstOrNull { it.name == foodInfo.foodName }
|
||||
val score = scoreItem?.score ?: 0.0
|
||||
foodInfo.score = ((1 - score) * 10000).roundToInt()
|
||||
}
|
||||
adapter.updateData(list)
|
||||
if (list.isNotEmpty()) {
|
||||
checkedItem = list[0]
|
||||
checkedItem!!.photoUri = lastPhotoUri
|
||||
updateCurrentFood(checkedItem)
|
||||
lastPhotoUri = null
|
||||
LightManager.closeRedLight()
|
||||
}
|
||||
}
|
||||
|
||||
private fun createAdapter(): GenericItemAdapter<FoodInfo, ItemFoodInfoBinding> {
|
||||
return GenericItemAdapter(
|
||||
items = emptyList(),
|
||||
bindingInflater = ItemFoodInfoBinding::inflate,
|
||||
bindCallback = { item, position ->
|
||||
this.tvName.text = item.foodName
|
||||
val score = (item.score / 100.0).format2String(2)
|
||||
this.tvName.text = "${item.foodName}\n${score}%"
|
||||
if (item.id != checkedItem?.id) {
|
||||
this.tvName.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
|
||||
this.tvName.setTextColor(resources.getColor(R.color.identify_normal))
|
||||
|
||||
Reference in New Issue
Block a user