This commit is contained in:
2026-01-13 14:26:18 +08:00
parent c38bead8a3
commit e86db688e1
2 changed files with 1286 additions and 226 deletions
@@ -152,32 +152,17 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
box = ObjectBox.boxStore.boxFor(Food::class)
isPageVisible = true
addBackEventListener()
//FoodModule.init(this)
initView()
viewModel.getUserFaceCache(pageNo = 1)
//setupSecondaryDisplay()
setupCamera()
//presentation?.step1FoodRecognizing()
initData()
}
private fun initData() {
// viewModel.getEquipmentToken()
//viewModel.getUserFaceCache2()
startFaceTask()
// addSocketListener()
}
private fun initView() {
// // 在Activity中获取DPI
// val metrics = getResources()?.displayMetrics
// val screenDPI = metrics?.densityDpi
// val density = metrics?.density
//
// Log.d("ScreenInfo", "DPI: $screenDPI, density: $density");
viewModel.getDeviceConfig { deviceConfig ->
runOnUiThread {
if (deviceConfig == null) {
@@ -193,13 +178,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
updateDateTime()
binding.ivSetting.setOnClickListener {
startActivity(Intent(this, SettingActivity::class.java))
//finish()
}
//binding.foodDisplayLayout.setOnClickListener {
// val intent = Intent(this, FoodCollectionActivity::class.java)
// startActivity(intent)
// finish()
//}
binding.previewView.outlineProvider = object : ViewOutlineProvider() {
override fun getOutline(view: View, outline: Outline) {
outline.setRoundRect(0, 0, view.width, view.height, 12f.dp)
@@ -219,52 +198,13 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
bottomSheetDialog!!.show(supportFragmentManager, "CustomBottomSheetDialog")
}
//val spinnerAdapter = ArrayAdapter<String>(
// this,
// R.layout.item_for_custom_spinner,
// arrayOf("即放即取", "余量取餐")
//)
//binding.spinner.adapter = spinnerAdapter
// val mode = SPUtil.getInstance().get(GlobalKey.KEY_PICKUP_MODE, 0)
// //binding.spinner.setSelection(mode ?: 0)
// presentation?.updateMealPickupMode(mode ?: 0)
//binding.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
// override fun onItemSelected(
// parent: AdapterView<*>?,
// view: View?,
// position: Int,
// id: Long
// ) {
// SPUtil.getInstance().put(GlobalKey.KEY_PICKUP_MODE, position)
// updateCurrentFood(null)
// isRecognitionFood = true
// presentation?.updateMealPickupMode(position)
//
// SensorScaleUtils.zero()
// }
//
// override fun onNothingSelected(parent: AdapterView<*>?) {}
//}
//adapter = createAdapter()
binding.recyclerview.let {
it.layoutManager = GridLayoutManager(this, 2)
it.adapter = adapter
}
// 添加间距装饰(12dp)
//binding.recyclerview.addItemDecoration(
// GridSpacingItemDecoration(
// spanCount = 2,
// spacing = dpToPx(30),
// includeEdge = false // 包含边缘间距
// )
//)
//binding.recyclerview.adapter = adapter
binding.tvFoodName.setOnClickListener {
SensorScaleUtils.zero()
}
// binding.tvRescan.setOnClickListener {
// debouncer.debounce { recognizeFood() }
// }
binding.btnPay.setOnClickListener {
clickPayButton()
}
@@ -277,7 +217,6 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
binding.previewView.visibility = View.VISIBLE
binding.ivImg.visibility = View.GONE
resumeAnalysis()
// viewModel.cleanIdentifiedFoodInfoList()
return
}
pauseAnalysis()
@@ -289,16 +228,6 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
binding.tvFoodName.text = foodInfo.foodName
binding.previewView.gone()
binding.ivImg.visible()
// if (foodInfo.foodImg.isNullOrBlank()) {
// 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)
//pauseAnalysis()
}
@@ -309,27 +238,6 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
override fun registerDataChange() {
super.registerDataChange()
//// lifecycleScope.launch {
//// viewModel.identifiedFoodInfoList2.collect { list ->
//// updateFoodInfo(list)
//// }
//// }
// lifecycleScope.launch {
// viewModel.identifiedFoodInfoList.collect { list ->
// //adapter.updateData(list)
// searchFoodList.clear()
// searchFoodList.addAll(list)
// adapter.notifyDataSetChanged()
//
// if (list.isNotEmpty()) {
// checkedItem = list[0]
// checkedItem!!.photoUri = lastPhotoUri
// updateCurrentFood(checkedItem)
// lastPhotoUri = null
// }
// }
// }
SensorScaleUtils.addWeightListener { weight ->
Timber.d("registerDataChange weight = $weight")
if (isPageVisible.not() || isStartRecognize.not()) {
@@ -450,34 +358,6 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
}
}
// private fun createAdapter(): GenericItemAdapter<FoodInfo, ItemFoodInfoBinding> {
// return GenericItemAdapter(
// items = emptyList(),
// bindingInflater = ItemFoodInfoBinding::inflate,
// bindCallback = { item, position ->
// 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))
// this.llRoot.setBackgroundResource(R.drawable.grid_item_normal)
// } else {
// this.tvName.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
// this.tvName.setTextColor(resources.getColor(R.color.identify_checked))
// this.llRoot.setBackgroundResource(R.drawable.grid_item_checked)
// }
////
// this.llRoot.setOnClickListener {
// Timber.d("itemClick ${item.foodName}, position = $position")
// item.photoUri = null
// checkedItem = item
// adapter.notifyDataSetChanged()
// updateCurrentFood(item)
// }
// }
// )
// }
private fun setupCamera() {
Timber.d("setupCamera")
cameraProviderFuture = ProcessCameraProvider.getInstance(this)
@@ -649,20 +529,6 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
}
show()
}
//SecondaryScreenPresentation(
// activity = this,
// this,
// display = secondaryDisplay,
// viewModel = viewModel,
// recognizeViewModel = viewModels<RecognizeViewModel>().value
//)
// presentation!!.setStepChangeCallback { step ->
// if (step == 1) {
// Timber.d("setStepChangeCallback${step}")
// updateCurrentFood(null)
// bottomSheetDialog?.dismiss()
// }
// }
}
@SuppressLint("NotifyDataSetChanged")
@@ -721,13 +587,6 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
}
}
// fun retry() {
// val weight = lastWeight
// lastWeight = 0.0
// presentation?.hideRecImage()
// recognizeByWeight(weight, true)
// }
override fun onPause() {
super.onPause()
isPageVisible = false
@@ -1127,91 +986,6 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
private var tcpClient: TcpClient? = null
// private fun addSocketListener() {
// //val ipAddress = NetworkUtils.getIPAddress(true)
// //Log.d(TAG, "addSocketListener,ipAddress: $ipAddress");
// tcpClient = TcpClient(
// "192.168.1.95",
// 5000,
// GlobalData.deviceId, // clientId
// 5000, // connectTimeoutMs
// 10 * 1000, // heartbeatIntervalMs
// 30 * 1000 // heartbeatTimeoutMs
// )
//
// tcpClient?.setListener(object : TcpClientListenerImpl() {
// override fun onSendSuccess(json: JSONObject?) {
// super.onSendSuccess(json)
//// toast("发送成功")
// }
//
// override fun onSendFailed(json: JSONObject?, e: java.lang.Exception?) {
// super.onSendFailed(json, e)
// ToastUtils.showToast("发送失败${e?.message}")
// }
//
// override fun onMessage(json: JSONObject?) {
// super.onMessage(json)
// json?.let { receiveMessage(json!!) }
// }
// })
// tcpClient?.start();
//
// try {
// LanServer.getInstance().let {
// it.setListener(object : LanServerListenerImpl() {
// override fun onMessageReceived(clientId: String?, message: JSONObject?) {
// super.onMessageReceived(clientId, message)
// //toast("收到消息clientId=$clientId")
// }
// })
// it.start()
// }
// } catch (e: Exception) {
// e.printStackTrace()
// }
// }
// /**
// * 接收服务端发送的消息
// *
// * @param json 人脸数据
// */
// private fun receiveMessage(json: JSONObject) {
// try {
// val faceId = json.getString("faceId");
// val faceFeature = json.getString("faceFeature");
// queryByUserName(faceId, { faceEntity ->
// if (faceEntity != null) {
// //已存在用户faceId的人脸数据
// return@queryByUserName
// }
// //不存在用户faceId,保存数据
// val faceEntity2 = FaceEntity(faceId, null, Base64.decode(faceFeature))
// //设置用户类型:1-内部员工、2-临时用户、3-普通会员、或者其它待定类型
// faceEntity2.setUserType("2")
// saveFaceData(faceEntity2)
// })
// } catch (e: Exception) {
// e.printStackTrace()
// }
// }
// private fun saveFaceData(faceEntity: FaceEntity) {
// Thread {
// FaceDatabase.getInstance(this).faceDao().insert(faceEntity)
// }.start()
// }
//
// private fun queryByUserName(userName: String, block: (FaceEntity) -> Unit) {
// Thread {
// val faceEntity = FaceDatabase.getInstance(this).faceDao().queryByUserName(userName)
// runOnUiThread {
// block(faceEntity)
// }
// }.start()
// }
private fun onTakePhotoSuccess(photoUri: Uri) {
lastPhotoUri = photoUri
Timber.tag(TAG).d("main,takePhoto耗时:${System.currentTimeMillis() - startTime}")