diff --git a/app/src/main/java/com/sw/dualscreen/activity/MainActivity.kt b/app/src/main/java/com/sw/dualscreen/activity/MainActivity.kt index 59cee2b..c4ded40 100644 --- a/app/src/main/java/com/sw/dualscreen/activity/MainActivity.kt +++ b/app/src/main/java/com/sw/dualscreen/activity/MainActivity.kt @@ -692,7 +692,11 @@ class MainActivity : BaseActivity() { val weight = lastWeight lastWeight = 0.0 presentation?.hideRecImage() - recognizeByWeight(weight, true) + presentation?.step1FoodRecognizing() + + binding.root.postDelayed({ + recognizeByWeight(weight, true) + },500) } isRefreshPage = !isRefreshPage } @@ -748,6 +752,7 @@ class MainActivity : BaseActivity() { foodWeight: Int, eatWeight: Int, eatNum: Int, + userId: String?, block: () -> Unit ) { Timber.tag(TAG).d("foodWeight=$foodWeight,eatWeight=$eatWeight") @@ -769,7 +774,7 @@ class MainActivity : BaseActivity() { eatWeight = eatWeight, //根据specId对应规格重量计算 eatNum = eatNum, - userId = null, + userId = userId, notPay = chargeMode != 0, //即放即取-1,称重-2 mode = if (pickupMode == 1) 1 else 2 @@ -823,6 +828,13 @@ class MainActivity : BaseActivity() { isStartRecognize = false presentation?.hideNutritionView(event.chargeMode) // presentation?.cancel() + + if (event.chargeMode == 1) { + presentation?.let { + it.onStop() + it.setupArcCamera() + } + } } private var isRefreshPage = false @Subscribe(threadMode = ThreadMode.MAIN) @@ -868,7 +880,8 @@ class MainActivity : BaseActivity() { foodInfo = checkedItem!!, foodWeight = foodWeight.roundToInt(), eatWeight = eatWeight.roundToInt(), - eatNum = eatNum + eatNum = eatNum, + userId = null, ) { // TODO: 暂不关闭首页副屏 //presentation?.dismiss() diff --git a/app/src/main/java/com/sw/dualscreen/activity/fragment/BusinessFragment.kt b/app/src/main/java/com/sw/dualscreen/activity/fragment/BusinessFragment.kt index 724b1c6..33d66e5 100644 --- a/app/src/main/java/com/sw/dualscreen/activity/fragment/BusinessFragment.kt +++ b/app/src/main/java/com/sw/dualscreen/activity/fragment/BusinessFragment.kt @@ -3,6 +3,7 @@ package com.sw.dualscreen.activity.fragment import android.view.LayoutInflater import com.sw.dualscreen.GlobalKey import com.sw.dualscreen.R +import com.sw.dualscreen.activity.SettingActivity import com.sw.dualscreen.databinding.FragmentBusinessBinding import com.sw.dualscreen.model.response.ChargeModeEvent import com.sw.dualscreen.sdk.SensorScaleUtils @@ -37,7 +38,7 @@ class BusinessFragment: BaseFragment() { binding.btnClearZero.setOnClickListener { SensorScaleUtils.tare() } - binding.btnConfirm.setOnClickListener { + binding.btnConfirm.setOnClickListener { v -> if (binding.rgCharge.checkedRadioButtonId == -1) { ToastUtils.showToast("请设置计费模式") return@setOnClickListener @@ -59,8 +60,14 @@ class BusinessFragment: BaseFragment() { val pickupMode = if (binding.rgTakeFood.checkedRadioButtonId == R.id.rbPickAndPlace) 0 else 1 SPUtil.getInstance().put(GlobalKey.KEY_PICKUP_MODE, pickupMode) - ToastUtils.showToast("设置已保存") - activity?.finish() + //ToastUtils.showToast("设置已保存") + (activity as SettingActivity).let { + it.showWaitingDialog("设置保存中") + v.postDelayed({ + it.hideWaitingDialog() + it.finish() + }, 1000) + } } SensorScaleUtils.addWeightListener { value -> val realWeight = (value * 1000).roundToInt() diff --git a/app/src/main/java/com/sw/dualscreen/presentation/MainScreenPresentation.kt b/app/src/main/java/com/sw/dualscreen/presentation/MainScreenPresentation.kt index 3f68c4f..bf57a62 100644 --- a/app/src/main/java/com/sw/dualscreen/presentation/MainScreenPresentation.kt +++ b/app/src/main/java/com/sw/dualscreen/presentation/MainScreenPresentation.kt @@ -19,6 +19,8 @@ import android.view.ViewTreeObserver import android.widget.FrameLayout import androidx.annotation.RequiresApi import androidx.core.content.ContextCompat +import androidx.core.view.isInvisible +import androidx.core.view.isVisible import androidx.core.view.updateLayoutParams import androidx.lifecycle.Observer import androidx.lifecycle.lifecycleScope @@ -109,6 +111,8 @@ class MainScreenPresentation( var currentUserId: String? = null + var facePreviewInfoList: MutableList? = null + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = PresentationMainScreenBinding.inflate(layoutInflater) @@ -116,7 +120,10 @@ class MainScreenPresentation( mealPickupMode = SPUtil.getInstance().get(GlobalKey.KEY_PICKUP_MODE, 0) ?: 0 window?.setBackgroundDrawableResource(android.R.color.white) initView() - setupArcCamera() + val chargeMode = SPUtil.getInstance().get(GlobalKey.KEY_CHARGE_MODE, 0) + if (chargeMode == 1) { + setupArcCamera() + } registerDataChange() startTime = System.currentTimeMillis() } @@ -226,7 +233,7 @@ class MainScreenPresentation( /** * 启用虹软人脸失败 */ - private fun setupArcCamera() { + fun setupArcCamera() { initArcViewModel() initArcView() openRectInfoDraw = true @@ -268,7 +275,6 @@ class MainScreenPresentation( LightManager.openGreenLight() currentStep = 3 stepChangeCallback(currentStep) - //根据接口数据更新热量数据-------------------------- userViewModel.getUserNutritionData(userId = userId) { nutrition -> activity.runOnUiThread { @@ -384,13 +390,18 @@ class MainScreenPresentation( return num?.roundToInt() ?: 0 } + private var isLoadUnbilledMode = false + /** * 餐品识别成功,不计费模式 */ fun loadUnbilledMode() { + isLoadUnbilledMode = true + Timber.tag(TAG).d("loadUnbilledMode") binding.calorieInclude.root.gone() binding.nutritionInclude.root.visible() + binding.ivPreviewImage.gone() binding.flCameraView.visible() binding.ivFaceRecMask.gone() binding.tvFaceTip.gone() @@ -406,10 +417,14 @@ class MainScreenPresentation( // ------------------------------------------------- // 删除userNutritionData,改为userNutrition calculateNutrition(recognitionWeight - lastWeight) + binding.root.postDelayed({ + isLoadUnbilledMode = false + }, 10000) } private var isFirstOpen = true var startTime = 0L + /** * 餐品识别中 */ @@ -519,6 +534,7 @@ class MainScreenPresentation( // 当前食物信息 private var currentFood: FoodInfo? = null fun updateFood(foodInfo: FoodInfo?) { + //if (isLoadUnbilledMode) return Timber.tag(TAG).d("main,updateFood,耗时:${System.currentTimeMillis() - startTime}") startTime = System.currentTimeMillis() currentFood = foodInfo @@ -546,7 +562,7 @@ class MainScreenPresentation( } else { //不计费 //开启人脸识别 - if (currentStep == 1) { + if (currentStep == 1 || binding.tvFaceTip.isVisible.not()) { step2FaceRecognizing(foodInfo) } } @@ -713,15 +729,11 @@ class MainScreenPresentation( step1FoodRecognizing() } - override fun onStop() { - if (rgbCameraHelper != null) { - rgbCameraHelper!!.release() - rgbCameraHelper = null - } - if (irCameraHelper != null) { - irCameraHelper!!.release() - irCameraHelper = null - } + public override fun onStop() { + rgbCameraHelper?.release() + rgbCameraHelper = null + irCameraHelper?.release() + irCameraHelper = null recognizeViewModel.destroy() super.onStop() } @@ -740,7 +752,7 @@ class MainScreenPresentation( recognizeViewModel.onPreviewFrame(ByteArray(1382400), true) } - + private var recognizeTime = 0L private fun initArcViewModel() { recognizeViewModel.setLiveType(livenessType) recognizeViewModel.ftInitCode.observe(activity, Observer { ftInitCode: Int? -> @@ -786,26 +798,22 @@ class MainScreenPresentation( recognizeViewModel.recognizeUserId.observe( activity, Observer { compareResult: CompareResult -> + if (currentStep != 2 || System.currentTimeMillis() - recognizeTime <= 2000) { + //忽略非识别中及两秒内的再次识别 + return@Observer + } + recognizeTime = System.currentTimeMillis() activity.runOnUiThread { activity.showWaitingDialog("加载中,请稍后……") } Timber.tag(TAG) - .i("recognizeUserId observe compareResult = ${compareResult.trackId}, userId = ${compareResult.faceEntity.userName}") + .i("recognizeUserId observe compareResult = ${compareResult.trackId}, userId = ${compareResult.faceEntity.userName}, step = $currentStep") recognitionTime = System.currentTimeMillis() recognitionWeight = lastWeight lastFaceTrackId = compareResult.trackId val faceEntity = compareResult.faceEntity val userId = faceEntity.userName currentUserId = userId -// if (TextUtils.equals(userId, "2")) { -// //解决识别出现recognizeStatus = 2待重试的问题 -// activity.runOnUiThread { -// ToastUtils.showToast("一开始就在屏幕内发现无法识别,从屏幕外进入就可以识别") -// activity.hideWaitingDialog() -// } -// //userViewModel.getUserFaceCache() -// return@Observer -// } if (userId == null) return@Observer //ToastUtils.showToast("人脸识别成功,userId = $userId") if (currentFood == null) { @@ -962,18 +970,17 @@ class MainScreenPresentation( }) } - @RequiresApi(api = Build.VERSION_CODES.Q) override fun onPreview(nv21: ByteArray?, camera: Camera?) { if (!isRecognition) { return } binding.dualCameraFaceRectView.clearFaceInfo() - val facePreviewInfoList: MutableList? = - recognizeViewModel.onPreviewFrame(nv21, true) + facePreviewInfoList = recognizeViewModel.onPreviewFrame(nv21, true) if (facePreviewInfoList != null && rgbFaceRectTransformer != null) { - drawPreviewInfo(facePreviewInfoList) + drawPreviewInfo(facePreviewInfoList!!) } - recognizeViewModel.clearLeftFace(facePreviewInfoList) + //recognizeViewModel.clearLeftFace(facePreviewInfoList) + clearLeftFaceData() } override fun onCameraClosed() { @@ -1111,10 +1118,10 @@ class MainScreenPresentation( * * @param facePreviewInfoList RGB画面的实时人脸信息 */ - private fun drawPreviewInfo(facePreviewInfoList: MutableList) { + private fun drawPreviewInfo(facePreviewInfoList: MutableList) { // Timber.tag(TAG).d("drawPreviewInfo facePreviewInfoList = ${facePreviewInfoList.size}, rgbFaceRectTransformer = ${rgbFaceRectTransformer != null}") if (rgbFaceRectTransformer != null) { - val rgbDrawInfoList: MutableList? = recognizeViewModel.getDrawInfo( + val rgbDrawInfoList: MutableList? = recognizeViewModel.getDrawInfo( facePreviewInfoList, LivenessType.RGB, openRectInfoDraw @@ -1122,15 +1129,24 @@ class MainScreenPresentation( // 识别成功 binding.dualCameraFaceRectView.drawRealtimeFaceInfo(rgbDrawInfoList) } + + //binding.dualCameraFaceRectView.clearFaceInfo() + //clearLeftFaceData() + val listIsEmpty = facePreviewInfoList.isEmpty() - val listFirstTrackId = if (listIsEmpty.not()) facePreviewInfoList[0]!!.trackId else null + val listFirstTrackId = if (listIsEmpty.not()) facePreviewInfoList[0].trackId else null Timber.tag(TAG) .d("listIsEmpty=$listIsEmpty,lastFaceTrackId=$lastFaceTrackId,listFirstTrackId=$listFirstTrackId") if (listIsEmpty || (lastFaceTrackId != listFirstTrackId)) { - if (lastFaceTrackId != -1 && currentUserId!=null) { + if (lastFaceTrackId != -1 && currentUserId != null) { mealPickupMode = SPUtil.getInstance().get(GlobalKey.KEY_PICKUP_MODE, 0) ?: 0 Timber.tag(TAG).i("$lastFaceTrackId 用户离开") lastFaceTrackId = -1 + Timber.tag(TAG) + .d("postUserData userNutritionData是否null: ${userNutrition == null}, currentFood是否null: ${currentFood == null},currentUserId是否null:${currentUserId == null}") + if (userNutrition == null || currentFood == null || currentUserId == null) { + return + } postUserData() if (mealPickupMode == 1 || isKeepFaceState) { step2FaceRecognizing(currentFood!!) @@ -1144,11 +1160,6 @@ class MainScreenPresentation( } fun postUserData() { - Timber.tag(TAG) - .d("postUserData userNutritionData是否null: ${userNutrition == null}, currentFood是否null: ${currentFood == null},currentUserId是否null:${currentUserId == null}", ) - if (userNutrition == null || currentFood == null || currentUserId == null) { - return - } createOrder() } @@ -1173,7 +1184,8 @@ class MainScreenPresentation( foodInfo = currentFood!!, foodWeight = lastWeight.roundToInt(), eatWeight = eatWeight.roundToInt(), - eatNum = activity.getEatNum(eatWeight, currentFood!!.specWeight) + eatNum = activity.getEatNum(eatWeight, currentFood!!.specWeight), + userId = currentUserId ) { if (MyApp.DEBUG) { ToastUtils.showToast("订单已生成") @@ -1182,6 +1194,13 @@ class MainScreenPresentation( } } + private fun clearLeftFaceData() { + facePreviewInfoList?.let { + recognizeViewModel.clearLeftFace(facePreviewInfoList!!) + } + facePreviewInfoList = null + } + fun hideNutritionView(chargeMode: Int) { binding.nutritionInclude.root.gone() binding.calorieInclude.root.gone()