refactor(face): 优化人脸特征存储与增量同步逻辑
- FaceDao 新增 queryAllByUserName 查询用户全部特征记录 - 改为查询所有记录后做去重判断,支持同一用户保存多条特征数据 - 将 Thread 替换为 lifecycleScope.launch(Dispatchers.IO) 协程 - 提取 getNewFaceEntity 方法消除重复代码 - 新增特征比对调试日志 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -14,6 +14,7 @@ import androidx.activity.enableEdgeToEdge
|
|||||||
import androidx.activity.viewModels
|
import androidx.activity.viewModels
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.view.WindowCompat
|
import androidx.core.view.WindowCompat
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.viewbinding.ViewBinding
|
import androidx.viewbinding.ViewBinding
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import com.sw.inbound.utils.DateTimeUtils
|
import com.sw.inbound.utils.DateTimeUtils
|
||||||
@@ -468,9 +469,9 @@ abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {
|
|||||||
|
|
||||||
public val recognizeViewModel by viewModels<RecognizeViewModel>()
|
public val recognizeViewModel by viewModels<RecognizeViewModel>()
|
||||||
private fun updateFaceData(list: List<UserFaceModel>) {
|
private fun updateFaceData(list: List<UserFaceModel>) {
|
||||||
Thread {
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
val faceList = mutableListOf<FaceEntity>()
|
val faceList = mutableListOf<FaceEntity>()
|
||||||
val faceDao = FaceDatabase.getInstance(this).faceDao()
|
val faceDao = FaceDatabase.getInstance(this@BaseActivity).faceDao()
|
||||||
try {
|
try {
|
||||||
list.forEach { model ->
|
list.forEach { model ->
|
||||||
if (model.faceDeleted == true) {
|
if (model.faceDeleted == true) {
|
||||||
@@ -480,19 +481,14 @@ abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {
|
|||||||
// FaceEntity("-1", null, byteArrayOf(-1)).also { it.userType = "-1" }
|
// FaceEntity("-1", null, byteArrayOf(-1)).also { it.userType = "-1" }
|
||||||
// }
|
// }
|
||||||
} else {
|
} else {
|
||||||
val faceData = faceDao.queryByUserName(model.userId)
|
// 获取该用户所有已存特征,判断当前特征是否已存在
|
||||||
if (faceData == null) {
|
val existList = faceDao.queryAllByUserName(model.userId)
|
||||||
//保存数据
|
val featureMatches = existList.map { Base64.encode(it.featureData) == model.faceFeatureStr }
|
||||||
val faceEntity = FaceEntity(
|
Timber.d("userId=${model.userId}, 已存记录数=${existList.size}, 各条特征是否与服务器一致=$featureMatches")
|
||||||
model.userId,
|
val alreadyExists = featureMatches.any { it }
|
||||||
null,
|
if (!alreadyExists) {
|
||||||
Base64.decode(model.faceFeatureStr)
|
//当前特征不存在,保存
|
||||||
).also {
|
faceList.add(getNewFaceEntity(model))
|
||||||
//1-会员、2-临时用户
|
|
||||||
it.userType = if (model.member) "1" else "2"
|
|
||||||
it.cardNo = model.cardNo
|
|
||||||
}
|
|
||||||
faceList.add(faceEntity)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -501,13 +497,25 @@ abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
if (faceList.isNotEmpty()) {
|
if (faceList.isNotEmpty()) {
|
||||||
FaceDatabase.getInstance(this).faceDao().insert(faceList)
|
FaceDatabase.getInstance(this@BaseActivity).faceDao().insert(faceList)
|
||||||
}
|
}
|
||||||
recognizeViewModel.refreshFaceList()
|
recognizeViewModel.refreshFaceList()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
}.start()
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getNewFaceEntity(model: UserFaceModel) : FaceEntity {
|
||||||
|
return FaceEntity(
|
||||||
|
model.userId,
|
||||||
|
null,
|
||||||
|
Base64.decode(model.faceFeatureStr)
|
||||||
|
).also {
|
||||||
|
//1-会员、2-临时用户
|
||||||
|
it.userType = if (model.member) "1" else "2"
|
||||||
|
it.cardNo = model.cardNo
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun initPlateList() {
|
fun initPlateList() {
|
||||||
|
|||||||
@@ -92,6 +92,15 @@ public interface FaceDao {
|
|||||||
@Query("SELECT * FROM face WHERE user_name = :userName limit 1")
|
@Query("SELECT * FROM face WHERE user_name = :userName limit 1")
|
||||||
FaceEntity queryByUserName(String userName);
|
FaceEntity queryByUserName(String userName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询指定用户的所有人脸记录(同一用户可能存有多条特征数据)
|
||||||
|
*
|
||||||
|
* @param userName 用户ID
|
||||||
|
* @return 该用户所有人脸记录列表
|
||||||
|
*/
|
||||||
|
@Query("SELECT * FROM face WHERE user_name = :userName")
|
||||||
|
List<FaceEntity> queryAllByUserName(String userName);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return 删除临时用户人脸
|
* @return 删除临时用户人脸
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user