优化
This commit is contained in:
@@ -66,6 +66,9 @@ import org.greenrobot.eventbus.Subscribe
|
|||||||
import org.greenrobot.eventbus.ThreadMode
|
import org.greenrobot.eventbus.ThreadMode
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
import java.util.concurrent.ScheduledExecutorService
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
import kotlin.math.log
|
import kotlin.math.log
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -239,6 +242,9 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
|
|||||||
// return@Observer
|
// return@Observer
|
||||||
// }
|
// }
|
||||||
currentUserId = result?.faceEntity?.userName
|
currentUserId = result?.faceEntity?.userName
|
||||||
|
plateEnable = true
|
||||||
|
// checkFaceJob?.cancel()
|
||||||
|
// getFaceJob?.cancel()
|
||||||
// viewModel.getUserInfoById(memberId = userId)
|
// viewModel.getUserInfoById(memberId = userId)
|
||||||
// ToastUtils.showToast(currentUserId)
|
// ToastUtils.showToast(currentUserId)
|
||||||
// if (!currentUserId.isNullOrBlank()) {
|
// if (!currentUserId.isNullOrBlank()) {
|
||||||
@@ -305,7 +311,6 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
|
|||||||
SpTool.savePlateData(items)
|
SpTool.savePlateData(items)
|
||||||
|
|
||||||
// debouncer.reset()
|
// debouncer.reset()
|
||||||
//recognizeViewModel.recognizeUserId.postValue(CompareResult(null,0f))
|
|
||||||
}
|
}
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
@@ -343,8 +348,10 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
|
|||||||
recognizeViewModel.destroy()
|
recognizeViewModel.destroy()
|
||||||
|
|
||||||
EventBus.getDefault().unregister(this)
|
EventBus.getDefault().unregister(this)
|
||||||
countDownJob?.cancel()
|
// countDownJob?.cancel()
|
||||||
checkFaceJob?.cancel()
|
// checkFaceJob?.cancel()
|
||||||
|
getFaceScheduler?.shutdown()
|
||||||
|
checkFaceScheduler?.shutdown()
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -469,6 +476,8 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
|
|||||||
if (facePreviewInfoList.isNullOrEmpty()) {
|
if (facePreviewInfoList.isNullOrEmpty()) {
|
||||||
currentPlateInfo = null
|
currentPlateInfo = null
|
||||||
currentUserId = null
|
currentUserId = null
|
||||||
|
// checkFaceJob?.cancel()
|
||||||
|
// getFaceJob?.cancel()
|
||||||
}
|
}
|
||||||
recognizeViewModel.clearLeftFace(facePreviewInfoList)
|
recognizeViewModel.clearLeftFace(facePreviewInfoList)
|
||||||
}
|
}
|
||||||
@@ -552,23 +561,28 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
|
|||||||
private var tempUserId: String = ""
|
private var tempUserId: String = ""
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
if (isFirstOpen) {
|
try {
|
||||||
goInitActivity()
|
if (isFirstOpen) {
|
||||||
binding.root.postDelayed({
|
goInitActivity()
|
||||||
isFirstOpen = false
|
binding.root.postDelayed({
|
||||||
}, 1000)
|
isFirstOpen = false
|
||||||
return
|
}, 1000)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
faceDataList.clear()
|
||||||
|
plateEnable = true
|
||||||
|
collectCount = 0
|
||||||
|
tempUserId = "${System.currentTimeMillis()}"
|
||||||
|
resumeCamera()
|
||||||
|
viewModel.resetUserInfo()
|
||||||
|
countDownTimer?.let {
|
||||||
|
it.cancel()
|
||||||
|
it.start()
|
||||||
|
}
|
||||||
|
checkFaceState()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
plateEnable = true
|
|
||||||
collectCount = 0
|
|
||||||
tempUserId = "${System.currentTimeMillis()}"
|
|
||||||
resumeCamera()
|
|
||||||
viewModel.resetUserInfo()
|
|
||||||
countDownTimer?.let {
|
|
||||||
it.cancel()
|
|
||||||
it.start()
|
|
||||||
}
|
|
||||||
checkFaceState()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private var isFirstOpen = true
|
private var isFirstOpen = true
|
||||||
@@ -622,15 +636,25 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
|
|||||||
recognizeViewModel.updateRegisterStatus(REGISTER_STATUS_READY)
|
recognizeViewModel.updateRegisterStatus(REGISTER_STATUS_READY)
|
||||||
loadLogInfo("collectFace: ----------5,人脸采集中-----------")
|
loadLogInfo("collectFace: ----------5,人脸采集中-----------")
|
||||||
getFaceData { faceData ->
|
getFaceData { faceData ->
|
||||||
|
if (faceDataList.isNotEmpty()) {
|
||||||
|
return@getFaceData
|
||||||
|
}
|
||||||
|
faceDataList.add(faceData)
|
||||||
|
|
||||||
|
checkFaceScheduler?.shutdown()
|
||||||
|
getFaceScheduler?.shutdown()
|
||||||
|
// checkFaceJob?.cancel()
|
||||||
|
// getFaceJob?.cancel()
|
||||||
|
recognizeViewModel.updateRegisterStatus(RecognizeViewModel.REGISTER_STATUS_DONE)
|
||||||
// userFaceInfo = null
|
// userFaceInfo = null
|
||||||
Thread {
|
Thread {
|
||||||
val faceDao = FaceDatabase.getInstance(this).faceDao()
|
// val faceDao = FaceDatabase.getInstance(App.getContext()).faceDao()
|
||||||
val entity = faceDao.queryByUserName(tempUserId)
|
// val entity = faceDao.queryByUserName(tempUserId)
|
||||||
if (entity != null) {
|
// if (entity != null) {
|
||||||
//已存在用户
|
// //已存在用户
|
||||||
loadLogInfo("collectFace,saveFaceInfo: ----------9,数据库已存在该人脸数据-----------")
|
// loadLogInfo("collectFace,saveFaceInfo: ----------9,数据库已存在该人脸数据-----------")
|
||||||
return@Thread
|
// return@Thread
|
||||||
}
|
// }
|
||||||
saveFaceInfo(faceData)
|
saveFaceInfo(faceData)
|
||||||
}.start()
|
}.start()
|
||||||
}
|
}
|
||||||
@@ -647,29 +671,38 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
|
|||||||
tcpClient?.send(jsonObject)
|
tcpClient?.send(jsonObject)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val getFaceScheduler: ScheduledExecutorService? = Executors.newScheduledThreadPool(1);
|
||||||
|
private val checkFaceScheduler: ScheduledExecutorService? = Executors.newScheduledThreadPool(1);
|
||||||
|
|
||||||
private fun getFaceData(block: (String) -> Unit) {
|
private fun getFaceData(block: (String) -> Unit) {
|
||||||
runBlocking {
|
getFaceScheduler?.scheduleWithFixedDelay(
|
||||||
job = executor.startIntervalTask(5) {
|
{
|
||||||
// if (currentUserId != null && currentUserId!!.isNotBlank()) {
|
getFaceDataBlock(block)
|
||||||
// toast("您已采集过人脸信息")
|
}, 5L, 5L, TimeUnit.MILLISECONDS
|
||||||
// return@startIntervalTask
|
)
|
||||||
// }
|
// runBlocking {
|
||||||
// loadLogInfo("collectFace,getFaceData: ----------6,正在获人脸取特征数据")
|
// getFaceJob = executor.startIntervalTask(5) {
|
||||||
if (userFaceInfo == null) {
|
|
||||||
return@startIntervalTask
|
// getFaceJob?.cancel()
|
||||||
}
|
// checkFaceJob?.cancel()
|
||||||
loadLogInfo("collectFace,getFaceData: ----------7,拿到人脸取特征数据-----------")
|
// }
|
||||||
val faceData: String? = Base64.encode(userFaceInfo?.faceFeature?.featureData)
|
// }
|
||||||
if (faceData.isNullOrBlank()) {
|
|
||||||
return@startIntervalTask
|
|
||||||
}
|
|
||||||
block(faceData)
|
|
||||||
checkFaceJob?.cancel()
|
|
||||||
job?.cancel()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun getFaceDataBlock(block: (String) -> Unit) {
|
||||||
|
if (userFaceInfo == null) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val faceData: String? = Base64.encode(userFaceInfo?.faceFeature?.featureData)
|
||||||
|
if (faceData.isNullOrBlank()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
loadLogInfo("collectFace,getFaceData: ----------7,拿到人脸取特征数据-----------")
|
||||||
|
block(faceData)
|
||||||
|
}
|
||||||
|
|
||||||
|
private var faceDataList: MutableList<String> = mutableListOf()
|
||||||
|
|
||||||
private fun loadLogInfo(msg: String) {
|
private fun loadLogInfo(msg: String) {
|
||||||
Log.d(TAG, msg)
|
Log.d(TAG, msg)
|
||||||
// logAdapter.addData(msg)
|
// logAdapter.addData(msg)
|
||||||
@@ -678,7 +711,7 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
|
|||||||
private var facePreviewInfoList: MutableList<FacePreviewInfo>? = null
|
private var facePreviewInfoList: MutableList<FacePreviewInfo>? = null
|
||||||
private var userFaceInfo: UserFaceInfo? = null
|
private var userFaceInfo: UserFaceInfo? = null
|
||||||
private var countDownJob: Job? = null
|
private var countDownJob: Job? = null
|
||||||
private var job: Job? = null
|
// private var getFaceJob: Job? = null
|
||||||
val executor = IntervalExecutor()
|
val executor = IntervalExecutor()
|
||||||
private var currentUserId: String? = null
|
private var currentUserId: String? = null
|
||||||
private fun countDown() {
|
private fun countDown() {
|
||||||
@@ -703,7 +736,8 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
|
|||||||
|
|
||||||
private fun saveFaceInfo(faceData: String) {
|
private fun saveFaceInfo(faceData: String) {
|
||||||
try {
|
try {
|
||||||
checkFaceJob?.cancel()
|
// getFaceJob?.cancel()
|
||||||
|
// checkFaceJob?.cancel()
|
||||||
|
|
||||||
sendFaceData(faceData)
|
sendFaceData(faceData)
|
||||||
loadLogInfo("collectFace,saveFaceInfo: ----------12,人脸数据发送完成时间:${DateTimeUtils.getDateTimeString()}-----------")
|
loadLogInfo("collectFace,saveFaceInfo: ----------12,人脸数据发送完成时间:${DateTimeUtils.getDateTimeString()}-----------")
|
||||||
@@ -816,19 +850,31 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
|
|||||||
|
|
||||||
private val WAITING_TIME = 3 * 1000L
|
private val WAITING_TIME = 3 * 1000L
|
||||||
private var checkStartTime = 0L
|
private var checkStartTime = 0L
|
||||||
private var checkFaceJob: Job? = null
|
// private var checkFaceJob: Job? = null
|
||||||
private fun checkFaceState() {
|
private fun checkFaceState() {
|
||||||
checkStartTime = System.currentTimeMillis()
|
checkStartTime = System.currentTimeMillis()
|
||||||
checkFaceJob = executor.startIntervalTask(5) {
|
|
||||||
Log.d("Login", "currentUserId=${currentUserId}")
|
checkFaceScheduler?.scheduleWithFixedDelay(
|
||||||
if (currentUserId.isNullOrBlank().not()) {
|
{
|
||||||
checkFaceJob?.cancel()
|
checkFaceBlock()
|
||||||
return@startIntervalTask
|
}, 5L, 5L, TimeUnit.MILLISECONDS
|
||||||
}
|
)
|
||||||
if (System.currentTimeMillis() - checkStartTime >= WAITING_TIME) {
|
|
||||||
//超过5秒采集人脸并识别
|
// checkFaceJob = executor.startIntervalTask(5) {
|
||||||
collectFace()
|
//
|
||||||
}
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun checkFaceBlock() {
|
||||||
|
Log.d("Login", "currentUserId=${currentUserId}")
|
||||||
|
if (currentUserId.isNullOrBlank().not()) {
|
||||||
|
// checkFaceJob?.cancel()
|
||||||
|
checkFaceScheduler?.shutdown()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (System.currentTimeMillis() - checkStartTime >= WAITING_TIME) {
|
||||||
|
//超过5秒采集人脸并识别
|
||||||
|
collectFace()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ class IntervalExecutor {
|
|||||||
fun startIntervalTask(delayMillis: Long, action: suspend () -> Unit): Job {
|
fun startIntervalTask(delayMillis: Long, action: suspend () -> Unit): Job {
|
||||||
return CoroutineScope(Dispatchers.Default).launch {
|
return CoroutineScope(Dispatchers.Default).launch {
|
||||||
while (isActive) {
|
while (isActive) {
|
||||||
|
ensureActive()
|
||||||
action()
|
action()
|
||||||
delay(delayMillis)
|
delay(delayMillis)
|
||||||
}
|
}
|
||||||
@@ -36,6 +37,7 @@ class IntervalExecutor {
|
|||||||
return CoroutineScope(Dispatchers.Default).launch {
|
return CoroutineScope(Dispatchers.Default).launch {
|
||||||
delay(initialDelay)
|
delay(initialDelay)
|
||||||
while (isActive) {
|
while (isActive) {
|
||||||
|
ensureActive()
|
||||||
action()
|
action()
|
||||||
delay(delayMillis)
|
delay(delayMillis)
|
||||||
}
|
}
|
||||||
@@ -44,23 +46,23 @@ class IntervalExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 使用示例
|
// 使用示例
|
||||||
fun main() = runBlocking {
|
//fun main() = runBlocking {
|
||||||
val executor = IntervalExecutor()
|
// val executor = IntervalExecutor()
|
||||||
|
//
|
||||||
// 示例1:每隔10秒执行一次
|
// // 示例1:每隔10秒执行一次
|
||||||
val job1 = executor.startIntervalTask(10000) {
|
// val job1 = executor.startIntervalTask(10000) {
|
||||||
println("定时任务执行: ${System.currentTimeMillis()}")
|
// println("定时任务执行: ${System.currentTimeMillis()}")
|
||||||
// 这里可以执行你的业务逻辑
|
// // 这里可以执行你的业务逻辑
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// 示例2:先延迟5秒,然后每隔3秒执行一次
|
// // 示例2:先延迟5秒,然后每隔3秒执行一次
|
||||||
val job2 = executor.startIntervalTaskWithInitialDelay(5000, 3000) {
|
// val job2 = executor.startIntervalTaskWithInitialDelay(5000, 3000) {
|
||||||
println("带初始延迟的定时任务: ${System.currentTimeMillis()}")
|
// println("带初始延迟的定时任务: ${System.currentTimeMillis()}")
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// 运行30秒后取消任务
|
// // 运行30秒后取消任务
|
||||||
delay(30000)
|
// delay(30000)
|
||||||
job1.cancel()
|
// job1.cancel()
|
||||||
job2.cancel()
|
// job2.cancel()
|
||||||
println("所有定时任务已取消")
|
// println("所有定时任务已取消")
|
||||||
}
|
//}
|
||||||
@@ -141,27 +141,27 @@
|
|||||||
<!-- tools:ignore="HardcodedText" />-->
|
<!-- tools:ignore="HardcodedText" />-->
|
||||||
<!-- </LinearLayout>-->
|
<!-- </LinearLayout>-->
|
||||||
|
|
||||||
<androidx.recyclerview.widget.RecyclerView
|
<!-- <androidx.recyclerview.widget.RecyclerView-->
|
||||||
android:id="@+id/rvLogInfo"
|
<!-- android:id="@+id/rvLogInfo"-->
|
||||||
android:layout_width="match_parent"
|
<!-- android:layout_width="match_parent"-->
|
||||||
android:layout_height="match_parent"
|
<!-- android:layout_height="match_parent"-->
|
||||||
android:layout_gravity="bottom"
|
<!-- android:layout_gravity="bottom"-->
|
||||||
android:overScrollMode="never"
|
<!-- android:overScrollMode="never"-->
|
||||||
android:visibility="gone"
|
<!-- android:visibility="gone"-->
|
||||||
android:background="@color/black"
|
<!-- android:background="@color/black"-->
|
||||||
tools:listitem="@layout/list_item_log_info" />
|
<!-- tools:listitem="@layout/list_item_log_info" />-->
|
||||||
|
|
||||||
<ImageView
|
<!-- <ImageView-->
|
||||||
android:id="@+id/btnLogState"
|
<!-- android:id="@+id/btnLogState"-->
|
||||||
android:layout_width="60dp"
|
<!-- android:layout_width="60dp"-->
|
||||||
android:layout_height="60dp"
|
<!-- android:layout_height="60dp"-->
|
||||||
android:layout_gravity="bottom|end"
|
<!-- android:layout_gravity="bottom|end"-->
|
||||||
android:background="@drawable/shape_white_circle"
|
<!-- android:background="@drawable/shape_white_circle"-->
|
||||||
android:padding="10dp"
|
<!-- android:padding="10dp"-->
|
||||||
android:layout_marginEnd="10dp"
|
<!-- android:layout_marginEnd="10dp"-->
|
||||||
android:layout_marginBottom="10dp"
|
<!-- android:layout_marginBottom="10dp"-->
|
||||||
android:src="@drawable/ic_log_show"
|
<!-- android:src="@drawable/ic_log_show"-->
|
||||||
android:textSize="16sp" />
|
<!-- android:textSize="16sp" />-->
|
||||||
|
|
||||||
</FrameLayout>
|
</FrameLayout>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|||||||
Reference in New Issue
Block a user