优化
This commit is contained in:
@@ -12,6 +12,7 @@ import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.ViewTreeObserver
|
||||
import android.widget.FrameLayout
|
||||
import android.widget.Toast
|
||||
import androidx.activity.viewModels
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.lifecycle.Observer
|
||||
@@ -31,6 +32,7 @@ import com.sw.face.collect.utils.BitmapUtils
|
||||
import com.sw.face.collect.utils.Debouncer
|
||||
import com.sw.face.collect.utils.FaceEngineUtils
|
||||
import com.sw.face.collect.utils.IntervalExecutor
|
||||
import com.sw.face.collect.utils.RegisterCallbackHandler
|
||||
import com.sw.face.collect.utils.SoundPoolUtil
|
||||
import com.sw.face.collect.viewmodel.MainViewModel
|
||||
import com.sw.inbound.utils.DateTimeUtils
|
||||
@@ -110,14 +112,15 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), ViewTreeObserver.OnGlo
|
||||
//mainViewModel.getUserFaceCache(pageNo = pageNo)
|
||||
FaceEngineUtils.activeEngine()
|
||||
|
||||
// binding.btnCollectFace.setOnClickListener {
|
||||
binding.btnCollectFace.setOnClickListener {
|
||||
registerHandler = RegisterCallbackHandler()
|
||||
// requestSinglePermissionResult(Manifest.permission.CAMERA) { granted ->
|
||||
// if (granted.not()) {
|
||||
// toast("没有相机权限")
|
||||
// return@requestSinglePermissionResult
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
binding.tvTitle.setOnClickListener { finish() }
|
||||
//addSocketListener()
|
||||
|
||||
@@ -327,6 +330,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), ViewTreeObserver.OnGlo
|
||||
private var isCheckCamera = false
|
||||
private var isCollectFace = false
|
||||
private val isCollecting = AtomicBoolean(false)
|
||||
private var registerHandler: RegisterCallbackHandler? = null
|
||||
private fun initArcViewModel() {
|
||||
if (DualCameraHelper.hasDualCamera()) {
|
||||
livenessType = LivenessType.IR
|
||||
@@ -340,20 +344,22 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), ViewTreeObserver.OnGlo
|
||||
recognizeViewModel.prepareRegister()
|
||||
recognizeViewModel.setOnRegisterFinishedCallback { facePreviewInfo, userFaceInfo ->
|
||||
Log.d(TAG, "initArcViewModel: userFaceInfo == null : ${userFaceInfo == null}")
|
||||
if (!isCollectFace) {
|
||||
return@setOnRegisterFinishedCallback
|
||||
}
|
||||
if (this.userFaceInfo == null && userFaceInfo != null) {
|
||||
loadLogInfo("collectFace:开始采集------------")
|
||||
recognizeViewModel.updateRegisterStatus(RecognizeViewModel.REGISTER_STATUS_DONE)
|
||||
this.userFaceInfo = userFaceInfo
|
||||
getFaceDataBlock { faceData, headBmp ->
|
||||
isCollecting.set(false)
|
||||
isCollectFace = false
|
||||
loadLogInfo("collectFace:采集完成,准备上传------------")
|
||||
sendFaceData(faceData, headBmp)
|
||||
registerHandler?.setCallback {
|
||||
if (!isCollectFace) {
|
||||
return@setCallback
|
||||
}
|
||||
if (this.userFaceInfo == null && userFaceInfo != null) {
|
||||
Toast.makeText(this@MainActivity, "开始采集", Toast.LENGTH_SHORT).show()
|
||||
loadLogInfo("collectFace:开始采集------------")
|
||||
recognizeViewModel.updateRegisterStatus(RecognizeViewModel.REGISTER_STATUS_DONE)
|
||||
this.userFaceInfo = userFaceInfo
|
||||
getFaceDataBlock { faceData, headBmp ->
|
||||
isCollecting.set(false)
|
||||
isCollectFace = false
|
||||
loadLogInfo("collectFace:采集完成,准备上传------------")
|
||||
sendFaceData(faceData, headBmp)
|
||||
}
|
||||
}
|
||||
return@setOnRegisterFinishedCallback
|
||||
}
|
||||
}
|
||||
recognizeViewModel.ftInitCode.observe(this, Observer { ftInitCode: Int? ->
|
||||
@@ -473,7 +479,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), ViewTreeObserver.OnGlo
|
||||
|
||||
// // 防抖,防止重复开闸
|
||||
Debouncer(5000).debounce {
|
||||
countDownTimer?.cancel()
|
||||
// countDownTimer?.cancel()
|
||||
binding.tvCountDown.text = ""
|
||||
SoundPoolUtil.getInstance().play(RECOGNIZE_SUCCESS, 0)
|
||||
//openPlate(userId)
|
||||
@@ -655,6 +661,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), ViewTreeObserver.OnGlo
|
||||
}
|
||||
if (facePreviewInfoList.isNullOrEmpty()) {
|
||||
binding.tvSimilarValue.text = ""
|
||||
registerHandler = RegisterCallbackHandler()
|
||||
}
|
||||
recognizeViewModel.clearLeftFace(facePreviewInfoList)
|
||||
}
|
||||
@@ -975,12 +982,12 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), ViewTreeObserver.OnGlo
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
setupArcCamera()
|
||||
|
||||
registerHandler = RegisterCallbackHandler()
|
||||
resumeCamera()
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
countDownTimer?.cancel()
|
||||
// countDownTimer?.cancel()
|
||||
super.onPause()
|
||||
pauseCamera()
|
||||
}
|
||||
@@ -1013,7 +1020,8 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), ViewTreeObserver.OnGlo
|
||||
loadLogInfo("collectFace:数据为空------11111------")
|
||||
return
|
||||
}
|
||||
val faceData: String? = com.sw.plate.utils.Base64.encode(userFaceInfo?.faceFeature?.featureData)
|
||||
val faceData: String? =
|
||||
com.sw.plate.utils.Base64.encode(userFaceInfo?.faceFeature?.featureData)
|
||||
if (faceData.isNullOrBlank()) {
|
||||
loadLogInfo("collectFace:数据为空------22222------")
|
||||
return
|
||||
@@ -1035,41 +1043,41 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), ViewTreeObserver.OnGlo
|
||||
private val total = 30
|
||||
private var countDownJob: Job? = null
|
||||
private fun countDown() {
|
||||
// countDownJob = countDownByFlow(
|
||||
// total = total,
|
||||
// scope = lifecycleScope,
|
||||
// onStart = {
|
||||
// binding.tvCountDown.text = "${total}s"
|
||||
// },
|
||||
// onTick = { seconds ->
|
||||
// binding.tvCountDown.text = "${seconds}s"
|
||||
// },
|
||||
// onFinish = {
|
||||
// }
|
||||
// )
|
||||
if (countDownTimer == null) {
|
||||
initCountTime()
|
||||
}
|
||||
countDownDebounce.debounce {
|
||||
countDownTimer?.start()
|
||||
}
|
||||
//// countDownJob = countDownByFlow(
|
||||
//// total = total,
|
||||
//// scope = lifecycleScope,
|
||||
//// onStart = {
|
||||
//// binding.tvCountDown.text = "${total}s"
|
||||
//// },
|
||||
//// onTick = { seconds ->
|
||||
//// binding.tvCountDown.text = "${seconds}s"
|
||||
//// },
|
||||
//// onFinish = {
|
||||
//// }
|
||||
//// )
|
||||
// if (countDownTimer == null) {
|
||||
// initCountTime()
|
||||
// }
|
||||
// countDownDebounce.debounce {
|
||||
// countDownTimer?.start()
|
||||
// }
|
||||
}
|
||||
|
||||
private val countDownDebounce = Debouncer(10000)
|
||||
|
||||
private var countDownTimer:CountDownTimer?=null
|
||||
|
||||
private val totalTimeInMillis = 30 * 1000L
|
||||
// private var countDownTimer:CountDownTimer?=null
|
||||
//
|
||||
// private val totalTimeInMillis = 30 * 1000L
|
||||
fun initCountTime() {
|
||||
countDownTimer = object : CountDownTimer(totalTimeInMillis, 1000) {
|
||||
override fun onTick(millisUntilFinished: Long) {
|
||||
loadLogInfo("initCountTime onTick = ${(millisUntilFinished / 1000).toInt()}")
|
||||
binding.tvCountDown.text = "${(millisUntilFinished / 1000).toInt() + 1}s"
|
||||
}
|
||||
|
||||
override fun onFinish() {
|
||||
}
|
||||
}
|
||||
//countDownTimer = object : CountDownTimer(totalTimeInMillis, 1000) {
|
||||
// override fun onTick(millisUntilFinished: Long) {
|
||||
// loadLogInfo("initCountTime onTick = ${(millisUntilFinished / 1000).toInt()}")
|
||||
// binding.tvCountDown.text = "${(millisUntilFinished / 1000).toInt() + 1}s"
|
||||
// }
|
||||
//
|
||||
// override fun onFinish() {
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.sw.face.collect.utils
|
||||
|
||||
class RegisterCallbackHandler {
|
||||
private var isCallbackCalled = false
|
||||
private val lock = Any()
|
||||
|
||||
fun setCallback(callback: () -> Unit) {
|
||||
synchronized(lock) {
|
||||
if (!isCallbackCalled) {
|
||||
isCallbackCalled = true
|
||||
callback()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -135,25 +135,26 @@
|
||||
android:textSize="26sp" />
|
||||
</LinearLayout>
|
||||
|
||||
<!-- <LinearLayout-->
|
||||
<!-- android:layout_width="wrap_content"-->
|
||||
<!-- android:layout_height="wrap_content"-->
|
||||
<!-- android:layout_marginTop="30dp"-->
|
||||
<!-- android:gravity="center_vertical"-->
|
||||
<!-- android:orientation="horizontal">-->
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="10dp"
|
||||
android:gravity="center_vertical"
|
||||
android:orientation="horizontal"
|
||||
android:visibility="gone">
|
||||
|
||||
<!-- <androidx.appcompat.widget.AppCompatButton-->
|
||||
<!-- android:id="@+id/btnCollectFace"-->
|
||||
<!-- android:layout_width="300dp"-->
|
||||
<!-- android:layout_height="100dp"-->
|
||||
<!-- android:layout_margin="20dp"-->
|
||||
<!-- android:background="@drawable/bg_collect_btn"-->
|
||||
<!-- android:text="开始采集"-->
|
||||
<!-- android:textColor="@color/white"-->
|
||||
<!-- android:textSize="40sp"-->
|
||||
<!-- android:textStyle="normal" />-->
|
||||
<androidx.appcompat.widget.AppCompatButton
|
||||
android:id="@+id/btnCollectFace"
|
||||
android:layout_width="300dp"
|
||||
android:layout_height="100dp"
|
||||
android:layout_margin="20dp"
|
||||
android:background="@drawable/bg_collect_btn"
|
||||
android:text="开始采集"
|
||||
android:textColor="@color/white"
|
||||
android:textSize="40sp"
|
||||
android:textStyle="normal" />
|
||||
|
||||
<!-- </LinearLayout>-->
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user