调整了人脸识别界面

This commit is contained in:
zxj
2025-07-24 18:02:42 +08:00
parent 082011a939
commit 5b3773d277
34 changed files with 203 additions and 137 deletions
+8 -4
View File
@@ -33,17 +33,21 @@
android:name=".activity.LoginByPwdActivity"
android:exported="true"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<!-- <intent-filter>-->
<!-- <action android:name="android.intent.action.MAIN" />-->
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- <category android:name="android.intent.category.LAUNCHER" />-->
<!-- </intent-filter>-->
</activity>
<activity
android:name=".activity.LoginByFaceActivity"
android:exported="true"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.MainActivity"
@@ -54,6 +54,9 @@ abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {
// 确保内容延伸到导航栏区域
// WindowCompat.setDecorFitsSystemWindows(window, false)
//保持亮屏
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
binding = inflateViewBinding()
headerBinding = inflateTitleBinding()
setContentView(binding.root)
@@ -60,8 +60,7 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
}
override fun initialize() {
// viewModel.activeEngine()
viewModel.activeEngine()
initArcViewModel()
initArcView()
openRectInfoDraw = true
@@ -156,6 +155,10 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
Timber.i("recognizeUserId observe userId = $userId")
viewModel.getUserInfoById(memberId = userId?.toInt() ?: 0)
})
recognizeViewModel.drawRectInfoText.observe(this, Observer { info ->
Timber.i("drawRectInfoText observe info = $info")
})
}
private fun initArcView() {
@@ -250,8 +253,10 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
val layoutParams = adjustPreviewViewSize(
binding.dualCameraTexturePreviewRgb,
binding.dualCameraTexturePreviewRgb, binding.dualCameraFaceRectView,
previewSizeRgb, displayOrientation, 1.0f
previewSizeRgb, displayOrientation, 1f
)
Timber.d("initRgbCamera previewSizeRgb = ${previewSizeRgb.width}-${previewSizeRgb.height}")
Timber.d("initRgbCamera layoutParams = ${layoutParams.width}-${layoutParams.height}")
rgbFaceRectTransformer = FaceRectTransformer(
previewSizeRgb.width,
previewSizeRgb.height,
@@ -269,7 +274,6 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
})
}
@RequiresApi(api = Build.VERSION_CODES.Q)
override fun onPreview(nv21: ByteArray?, camera: Camera?) {
if (!isRecognition) {
@@ -322,7 +326,6 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
rgbCameraHelper!!.start()
}
/**
* 绘制RGB、IR画面的实时人脸信息
*
@@ -337,7 +340,6 @@ class LoginByFaceActivity : BaseActivity<ActivityLoginFaceBinding>(),
)
// 识别成功
binding.dualCameraFaceRectView.drawRealtimeFaceInfo(rgbDrawInfoList)
}
}
@@ -4,6 +4,7 @@ import android.content.Intent
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.sw.plate.utils.ToastUtils
import com.sw.plate.utils.arcface.face.Test
import com.sw.platecabinet.databinding.ActivityLoginByPwdBinding
import com.sw.platecabinet.databinding.ItemTitleTimeBinding
import com.sw.platecabinet.viewmodel.UserViewModel
@@ -28,6 +29,11 @@ class LoginByPwdActivity : BaseActivity<ActivityLoginByPwdBinding>() {
val pwd = binding.etPwd.text.toString()
if (phone.isEmpty() || pwd.isEmpty()) {
ToastUtils.showToast("手机或校验码不能为空")
val test = Test()
for (i in 1..1000) {
test.test1()
}
return@setOnClickListener
}
viewModel.loginWithPwd(phone = phone, password = pwd)
@@ -14,8 +14,10 @@ import com.sw.platecabinet.model.request.LoginParam
import com.sw.platecabinet.model.response.EquipmentUserInfo
import com.sw.platecabinet.model.response.UserFaceModel
import com.sw.platecabinet.utils.ThreadUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.withContext
import timber.log.Timber
class UserViewModel : BaseViewModel() {
@@ -55,8 +57,10 @@ class UserViewModel : BaseViewModel() {
val faceEntity = list.map {
FaceEntity(it.userId, null, Base64.decode(it.faceFeatureString))
}
faceApi.updateFaceData(index, faceEntity)
activeEngine()
withContext(Dispatchers.Default) {
faceApi.updateFaceData(index, faceEntity)
activeEngine()
}
}
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 477 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 467 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 927 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

@@ -2,7 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#14141E"
android:background="@drawable/bg"
android:orientation="vertical">
<include
@@ -33,11 +33,11 @@
android:gravity="center_vertical">
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="55dp"
android:src="@mipmap/ic_phone" />
android:src="@drawable/ic_phone" />
<EditText
android:id="@+id/et_phone"
@@ -73,11 +73,11 @@
android:gravity="center_vertical">
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="55dp"
android:src="@mipmap/ic_password" />
android:src="@drawable/ic_pwd" />
<EditText
android:id="@+id/et_pwd"
+46 -45
View File
@@ -4,9 +4,9 @@
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#14141E"
android:fitsSystemWindows="false"
android:orientation="vertical"
android:background="@drawable/bg"
tools:context=".activity.LoginByFaceActivity">
<include
@@ -28,59 +28,60 @@
android:textColor="#fff0c8b4"
android:textSize="36sp" />
<FrameLayout
android:layout_width="380dp"
android:layout_height="380dp"
android:layout_marginTop="119dp"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="129dp"
android:gravity="center_vertical"
android:visibility="visible">
<!-- <androidx.camera.view.PreviewView-->
<!-- android:id="@+id/previewView"-->
<!-- android:layout_margin="10dp"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent" />-->
<FrameLayout
android:layout_width="380dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="1dp">
<TextureView
android:id="@+id/dual_camera_texture_preview_rgb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp" />
<TextureView
android:id="@+id/dual_camera_texture_preview_rgb"
android:layout_width="380dp"
android:visibility="visible"
android:layout_height="wrap_content" />
<com.sw.plate.utils.arcface.FaceRectView
android:id="@+id/dual_camera_face_rect_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:background="@drawable/circle_dashed_border" />
<com.sw.plate.utils.arcface.FaceRectView
android:id="@+id/dual_camera_face_rect_view"
android:layout_width="match_parent"
android:layout_height="380dp" />
</FrameLayout>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bg_face" />
</FrameLayout>
<LinearLayout
android:id="@+id/ll_to_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:background="@drawable/btn_outline_887872"
android:gravity="center"
android:paddingHorizontal="45dp"
android:paddingVertical="18dp">
<TextView
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码登录"
android:textColor="#ff887872"
android:textSize="24sp" />
</LinearLayout>
android:background="@drawable/bg_face" />
<LinearLayout
android:id="@+id/ll_to_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="208dp"
android:layout_marginTop="500dp"
android:background="@drawable/btn_outline_887872"
android:gravity="center"
android:paddingHorizontal="45dp"
android:paddingVertical="18dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码登录"
android:textColor="#ff887872"
android:textSize="24sp" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
+1 -1
View File
@@ -5,7 +5,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#14141E"
android:background="@drawable/bg"
tools:context=".activity.MainActivity">
<include
@@ -28,10 +28,10 @@
tools:text="1-12" />
<ImageView
android:layout_width="280dp"
android:layout_height="24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="22dp"
android:src="@mipmap/ic_bind_img" />
android:src="@drawable/ic_bind_img" />
<LinearLayout
android:layout_width="match_parent"
@@ -9,9 +9,9 @@
tools:context=".fragment.PlateCabinetFullFragment">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@mipmap/ic_full" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_full" />
<TextView
android:layout_width="wrap_content"
@@ -28,9 +28,9 @@
android:orientation="vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_plate_open" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_plate_open" />
<TextView
android:layout_width="wrap_content"
@@ -10,10 +10,10 @@
<ImageView
android:layout_width="280dp"
android:layout_height="24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:src="@mipmap/ic_unbind_img" />
android:src="@drawable/ic_unbind_img" />
<TextView
android:layout_width="wrap_content"
Binary file not shown.

Before

Width:  |  Height:  |  Size: 102 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 610 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

+2
View File
@@ -10,4 +10,6 @@
<color name="bind_F0C8B4">#F0C8B4</color>
<color name="unbind_F0C8B4">#F0C8B4</color>
<color name="unbind_32283C">#32283C</color>
<color name="divColor">#4c91ceff</color>
</resources>
+1
View File
@@ -4,4 +4,5 @@
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="specific_engine_init_failed">%s 初始化失败,错误码:%d\n错误码常量名:%s</string>
<string name="permission_denied">权限被拒绝!</string>
</resources>
@@ -1,6 +1,7 @@
package com.sw.plate.utils.arcface;
import android.content.Context;
import android.util.Log;
import com.arcsoft.face.FaceEngine;
import com.arcsoft.face.enums.RuntimeABI;
@@ -14,6 +15,8 @@ import com.sw.plate.utils.arcface.facedb.entity.FaceEntity;
import java.util.List;
public class FaceApi {
private static final String TAG = "FaceApi";
public interface ActiveCallback {
void onSuccess(int code);
@@ -27,12 +30,12 @@ public class FaceApi {
* @param list
*/
public void updateFaceData(int index, List<FaceEntity> list) {
Log.d(TAG, "updateFaceData: index = " + index + ", listSize = " + list.size());
FaceDao faceDao = FaceDatabase.getInstance(App.getContext()).faceDao();
if (index == 0) {
faceDao.deleteAll();
faceDao.resetId();
}
faceDao.insert(list);
}
@@ -2,6 +2,8 @@ package com.sw.plate.utils.arcface.face;
import android.graphics.Rect;
import android.hardware.Camera;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import androidx.annotation.IntDef;
@@ -47,7 +49,6 @@ import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
@@ -529,7 +530,7 @@ public class FaceHelper implements FaceListener {
if (recognizeInfoMap.containsKey(trackId)) {
onFaceFeatureInfoGet(faceFeature, trackId, errorCode);
}
} catch (InterruptedException e) {
} catch (Exception e) {
Log.e(TAG, "onFaceFeatureInfoGet: 等待活体结果时退出界面会执行,正常现象,可注释异常代码块");
e.printStackTrace();
}
@@ -549,40 +550,35 @@ public class FaceHelper implements FaceListener {
}
}
// 定义一个Handler,用于处理延迟任务
private Handler delayHandler = new Handler(Looper.getMainLooper());
/**
* 延迟 {@link RecognizeConfiguration#getLivenessFailedRetryInterval()}后,重新进行活体检测
*
* @param trackId 人脸ID
*/
private void retryLivenessDetectDelayed(final Integer trackId) {
Observable.timer(recognizeConfiguration.getLivenessFailedRetryInterval(), TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Long>() {
Disposable disposable;
Log.d(TAG, "retryLivenessDetectDelayed: trackId = " + trackId + ", threadName =" + Thread.currentThread().getName());
Log.d(TAG, "retryLivenessDetectDelayed: Interval = " + recognizeConfiguration.getLivenessFailedRetryInterval());
@Override
public void onSubscribe(Disposable d) {
disposable = d;
delayFaceTaskCompositeDisposable.add(disposable);
}
// 创建延迟任务
Runnable delayTask = new Runnable() {
@Override
public void run() {
// 将该人脸状态置为UNKNOWN,帧回调处理时会重新进行活体检测
changeLiveness(trackId, LivenessInfo.UNKNOWN);
// 从集合中移除这个任务
delayFaceTaskList.remove(this);
}
};
@Override
public void onNext(Long aLong) {
// 将任务添加到集合中管理
delayFaceTaskList.add(delayTask);
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onComplete() {
// 将该人脸状态置为UNKNOWN,帧回调处理时会重新进行活体检测
changeLiveness(trackId, LivenessInfo.UNKNOWN);
delayFaceTaskCompositeDisposable.remove(disposable);
}
});
// 发送延迟任务
delayHandler.postDelayed(delayTask, recognizeConfiguration.getLivenessFailedRetryInterval());
}
/**
@@ -591,40 +587,33 @@ public class FaceHelper implements FaceListener {
* @param trackId 人脸ID
*/
private void retryRecognizeDelayed(final Integer trackId) {
Log.d(TAG, "retryRecognizeDelayed: trackId = " + trackId);
changeRecognizeStatus(trackId, RequestFeatureStatus.FAILED);
Observable.timer(recognizeConfiguration.getRecognizeFailedRetryInterval(), TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Long>() {
Disposable disposable;
@Override
public void onSubscribe(Disposable d) {
disposable = d;
delayFaceTaskCompositeDisposable.add(disposable);
}
// 创建延迟任务
Runnable delayTask = new Runnable() {
@Override
public void run() {
// 将该人脸特征提取状态置为FAILED,帧回调处理时会重新进行活体检测
changeRecognizeStatus(trackId, RequestFeatureStatus.TO_RETRY);
// 从集合中移除这个任务
delayFaceTaskList.remove(this);
}
};
@Override
public void onNext(Long aLong) {
// 将任务添加到集合中管理
delayFaceTaskList.add(delayTask);
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "retryRecognizeDelayed e = " + e);
e.printStackTrace();
}
@Override
public void onComplete() {
// 将该人脸特征提取状态置为FAILED,帧回调处理时会重新进行活体检测
changeRecognizeStatus(trackId, RequestFeatureStatus.TO_RETRY);
delayFaceTaskCompositeDisposable.remove(disposable);
}
});
// 发送延迟任务
delayHandler.postDelayed(delayTask, recognizeConfiguration.getRecognizeFailedRetryInterval());
}
// 需要在类中定义一个集合来管理所有的延迟任务,替代原来的CompositeDisposable
private List<Runnable> delayFaceTaskList = new ArrayList<>();
@Override
public void onFaceLivenessInfoGet(@Nullable LivenessInfo livenessInfo, Integer trackId, Integer errorCode) {
Log.d(TAG, "onFaceLivenessInfoGet: trackId = " + trackId);
if (livenessInfo != null) {
int liveness = livenessInfo.getLiveness();
Log.i(TAG, "onFaceLivenessInfoGet liveness:" + liveness);
@@ -0,0 +1,49 @@
package com.sw.plate.utils.arcface.face;
import android.util.Log;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
public class Test {
private static final String TAG = "Test";
private CompositeDisposable delayFaceTaskCompositeDisposable = new CompositeDisposable();
public void test1() {
Log.d(TAG, "test1: ");
Observable.timer(10, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Long>() {
Disposable disposable;
@Override
public void onSubscribe(Disposable d) {
disposable = d;
delayFaceTaskCompositeDisposable.add(disposable);
}
@Override
public void onNext(Long value) {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete: ");
delayFaceTaskCompositeDisposable.remove(disposable);
}
});
}
}
@@ -9,8 +9,6 @@ import androidx.room.RoomDatabase;
import com.sw.plate.utils.arcface.facedb.dao.FaceDao;
import com.sw.plate.utils.arcface.facedb.entity.FaceEntity;
import java.io.File;
@Database(entities = {FaceEntity.class}, version = 1, exportSchema = false)
public abstract class FaceDatabase extends RoomDatabase {
public abstract FaceDao faceDao();
@@ -22,7 +20,9 @@ public abstract class FaceDatabase extends RoomDatabase {
synchronized (FaceDatabase.class) {
if (faceDatabase == null) {
faceDatabase = Room.databaseBuilder(context, FaceDatabase.class,
context.getExternalFilesDir("database") + File.separator + "faceDB.db").build();
context.getDatabasePath("faceDB.db").getPath()
// context.getExternalFilesDir("database") + File.separator + "faceDB.db"
).build();
}
}
}
@@ -4,7 +4,6 @@ import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Environment;
import android.util.Log;
import com.arcsoft.face.ErrorInfo;
@@ -354,8 +353,11 @@ public class FaceServer {
* @return 存放注册照的文件夹路径
*/
private String getImageDir() {
return App.getContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES)
+ File.separator + "faceDB" + File.separator + "registerFaces";
// return App.getContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES)
// + File.separator + "faceDB" + File.separator + "registerFaces";
return App.getContext().getFilesDir()
+ File.separator + "faceDB"
+ File.separator + "registerFaces";
}
/**