瀏覽代碼

:seedling: 整合人脸SDK

everywindchase 1 年之前
父節點
當前提交
162c448623

+ 1 - 1
app/src/main/assets/CWModels/_configs_frontend_x86_arm.xml

@@ -49,7 +49,7 @@
 
             <Image_Preprocess_Mode>0</Image_Preprocess_Mode>
 
-            <ImgAligned_Color_Mode>3</ImgAligned_Color_Mode>
+            <ImgAligned_Color_Mode>2</ImgAligned_Color_Mode>
 
             <Fake_Face_Filter_Aggregate>0</Fake_Face_Filter_Aggregate>
 

+ 1 - 1
app/src/main/java/com/ethan/psbc/managers/impls/ImplFaceManager.kt

@@ -54,7 +54,7 @@ class ImplFaceManager: IFaceManager, KoinComponent {
                     Log.d("demo", "设置人脸跟踪参数" )
                     var faceParam= FaceParam()
                     FaceDetTrack.cwGetFaceParam(res,faceParam)
-                    faceParam.minSize=100
+                    faceParam.minSize=60
                     faceParam.maxSize=400
                     FaceDetTrack.cwSetFaceParam(res,faceParam,faceDetConfigFile.absolutePath)
 

+ 1 - 1
app/src/main/java/com/ethan/psbc/managers/impls/ImplPreferencesManager.kt

@@ -14,7 +14,7 @@ internal class ImplPreferencesManager : IPreferencesManager, KoinComponent {
     private val context: Context by inject()
     private val defaultSharedPreferences: SharedPreferences = android.preference.PreferenceManager.getDefaultSharedPreferences(context)
     override var serverIp: String
-        get() = defaultSharedPreferences.getString("serverIp", "47.98.168.1")!!
+        get() = defaultSharedPreferences.getString("serverIp", "172.16.10.34")!!
         set(value) = defaultSharedPreferences.edit().putString("serverIp", value).apply()
 
     override var serverPort: Int

+ 74 - 26
app/src/main/java/com/ethan/psbc/ui/dialogs/DialogValidateFace.kt

@@ -3,6 +3,7 @@ package com.ethan.psbc.ui.dialogs
 import android.Manifest
 import android.content.Context
 import android.content.pm.PackageManager
+import android.content.res.Configuration
 import android.graphics.ImageFormat
 import android.os.Environment
 import android.util.Log
@@ -73,6 +74,7 @@ class DialogValidateFace( mContext: Context) : FullScreenPopupView(mContext), IV
         mBinding = DialogValidateFaceBinding.bind(popupContentView)
         val faceHandleRet:Int=mFace.initSdk()
         this.faceHandleRet=faceHandleRet
+        mBinding.mPreviewView.scaleType=PreviewView.ScaleType.FIT_CENTER
         runCamera(this, mBinding.mPreviewView, faceHandleRet, context = context)
         mBinding.mPreviewView.bringToFront()
         mBinding.takePhoto.setOnClickListener(this)
@@ -160,7 +162,7 @@ class DialogValidateFace( mContext: Context) : FullScreenPopupView(mContext), IV
 
                 imageAnalysis = ImageAnalysis.Builder().setOutputImageFormat(OUTPUT_IMAGE_FORMAT_YUV_420_888)
                     .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
-                    .setTargetResolution(Size(640, 480))
+                    //.setTargetResolution(Size(640, 480))
                     .build()
 
 
@@ -247,13 +249,45 @@ class DialogValidateFace( mContext: Context) : FullScreenPopupView(mContext), IV
                             val iFeaLen =FaceRecog.cwGetFeatureLength(handle)
                              Log.d("demo", "获取到人脸特征长度:${iFeaLen}")
                             var pFaceBuffer=Array(1){_ ->FaceInfo()}
-                            val faceDetRet:Int= FaceDetTrack.cwFaceDetection(handle,nv21,640, 480, FaceInterface.cw_img_form_t.CW_IMAGE_NV21 , 0, 0, 	FaceInterface.cw_op_t.CW_OP_DET , pFaceBuffer)
+                            var faceDetRet:Int=-1
+                           var  lensType:Int= camera.cameraInfo.lensFacing
+                              val   orientation= this.resources.configuration.orientation;
+                            when(lensType){
+                                CameraSelector.LENS_FACING_FRONT->{
+                                        if(Configuration.ORIENTATION_PORTRAIT==orientation){
+                                            faceDetRet= FaceDetTrack.cwFaceDetection(handle,nv21,640, 480, FaceInterface.cw_img_form_t.CW_IMAGE_NV21 , FaceInterface.cw_img_angle_t.CW_IMAGE_ANGLE_270,  FaceInterface.cw_img_mirror_t.CW_IMAGE_MIRROR_HOR, 	FaceInterface.cw_op_t.CW_OP_DET , pFaceBuffer)
+                                        }else{
+                                            faceDetRet= FaceDetTrack.cwFaceDetection(handle,nv21,640, 480, FaceInterface.cw_img_form_t.CW_IMAGE_NV21 , FaceInterface.cw_img_angle_t.CW_IMAGE_ANGLE_0,  FaceInterface.cw_img_mirror_t.CW_IMAGE_MIRROR_HOR,	FaceInterface.cw_op_t.CW_OP_DET , pFaceBuffer)
+                                        }
+                                }
+                                CameraSelector.LENS_FACING_BACK->{
+                                    if(Configuration.ORIENTATION_PORTRAIT==orientation){
+                                        faceDetRet= FaceDetTrack.cwFaceDetection(handle,nv21,640, 480, FaceInterface.cw_img_form_t.CW_IMAGE_NV21 ,  FaceInterface.cw_img_angle_t.CW_IMAGE_ANGLE_270, FaceInterface.cw_img_mirror_t.CW_IMAGE_MIRROR_NONE, 	FaceInterface.cw_op_t.CW_OP_DET , pFaceBuffer)
+                                    }else{
+                                        faceDetRet= FaceDetTrack.cwFaceDetection(handle,nv21,640, 480, FaceInterface.cw_img_form_t.CW_IMAGE_NV21 , FaceInterface.cw_img_angle_t.CW_IMAGE_ANGLE_0, FaceInterface.cw_img_mirror_t.CW_IMAGE_MIRROR_NONE, 	FaceInterface.cw_op_t.CW_OP_DET , pFaceBuffer)
+                                    }
+                                }
+                                CameraSelector.LENS_FACING_UNKNOWN->{
+                                        // ignore
+                                }
+                            }
+
+
+
                             if (faceDetRet >= FaceInterface.cw_errcode_t.CW_UNKNOWN_ERR) {
                                 Log.d("demo", "检测到人脸异常:${faceDetRet}" )
                             }else  if(faceDetRet<1){
                                 Log.d("demo", "未检测到人脸:${faceDetRet}" )
+                                context.lifecycleScope.launch {
+                                    Toast.makeText(context, "未检测到人脸:${faceDetRet}", Toast.LENGTH_SHORT).show()
+                                }
+
                             }else{
                                 Log.d("demo", "检测到人脸" )
+                                context.lifecycleScope.launch {
+                                    Toast.makeText(context, "检测到人脸:${faceDetRet}", Toast.LENGTH_SHORT).show()
+                                }
+
                                 val btFeature=ByteArray(iFeaLen)
                                 FaceRecog.cwGetFaceFeature(handle,pFaceBuffer[0].alignedData,pFaceBuffer[0].alignedW,
                                     pFaceBuffer[0].alignedH,pFaceBuffer[0].nChannels, btFeature)
@@ -286,34 +320,25 @@ class DialogValidateFace( mContext: Context) : FullScreenPopupView(mContext), IV
                         imageCamera,
                         imageAnalysis
                     )!!//绑定用例
-                } catch (_: Exception) {
+                    preview?.setSurfaceProvider(previewView?.surfaceProvider)
+                    if(null!=previewView) {
+                        observeCameraLife(camera, lifecycleOwner, previewView)
+                    }
+
 
+                } catch (e: Exception) {
+                    Log.d("demo","初始化相机异常${e.message}")
                 }
 
 
-                preview?.setSurfaceProvider(previewView?.surfaceProvider)
+
 
 
 
         }, ContextCompat.getMainExecutor(context))
 
 
-        camera.cameraInfo.cameraState.observe(lifecycleOwner) { value ->
-            when (value.type) {
-                CameraState.Type.OPEN         ->{
-                    Log.d("demo", "相机正在打开中" )
-                    if (null != previewView) {
-                        autoFocus(camera, previewView, 200f, 200f, true, ContextCompat.getMainExecutor(context))
-                    }
-                }
 
-                CameraState.Type.PENDING_OPEN -> Log.d("demo", "相机待打开" )
-                CameraState.Type.OPENING      -> Log.d("demo", "相机已打开" )
-                CameraState.Type.CLOSING      ->Log.d("demo", "相机关闭中" )
-                CameraState.Type.CLOSED       -> Log.d("demo", "相机已关闭" )
-            }
-
-        }
 
     }
 
@@ -328,6 +353,24 @@ class DialogValidateFace( mContext: Context) : FullScreenPopupView(mContext), IV
 
 
 
+    fun observeCameraLife(camera:Camera,lifecycleOwner:LifecycleOwner,previewView:PreviewView){
+        camera.cameraInfo.cameraState.observe(lifecycleOwner) { value ->
+            when (value.type) {
+                CameraState.Type.OPEN         ->{
+                    Log.d("demo", "相机正在打开中" )
+                    autoFocus(camera, previewView, 200f, 200f, true, ContextCompat.getMainExecutor(context))
+
+                }
+
+                CameraState.Type.PENDING_OPEN -> Log.d("demo", "相机待打开" )
+                CameraState.Type.OPENING      -> Log.d("demo", "相机已打开" )
+                CameraState.Type.CLOSING      ->Log.d("demo", "相机关闭中" )
+                CameraState.Type.CLOSED       -> Log.d("demo", "相机已关闭" )
+            }
+
+        }
+    }
+
 
 
     fun autoFocus(camera:Camera,previewView:PreviewView,x:Float,y:Float,auto:Boolean,executor: Executor) {
@@ -360,26 +403,31 @@ class DialogValidateFace( mContext: Context) : FullScreenPopupView(mContext), IV
       val build= FocusMeteringAction.Builder(createPoint)
             .setAutoCancelDuration(3,TimeUnit.SECONDS).build()
 
-       var result=cameraCrtl.startFocusAndMetering(build)
+
+
+
+
+        var result=cameraCrtl.startFocusAndMetering(build)
 
         result.addListener({
-            try{
-                if(result.get().isFocusSuccessful) {
+            try {
+                if (result.get().isFocusSuccessful) {
                     Log.d("demo", "聚集成功")
 
-                }else{
+                } else {
                     Log.d("demo", "聚集失败")
                 }
 
-            }catch (e:Exception){
-                Log.d("demo","聚集异常${e.message}")
+            } catch (e: Exception) {
+                Log.d("demo", "聚集异常${e.message}")
             }
 
 
 
         },executor)
+            }
+
 
-    }
 
 
 

+ 9 - 5
app/src/main/res/layout/activity_login.xml

@@ -57,12 +57,16 @@
         android:orientation="vertical"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
-        app:layout_constraintHorizontal_bias="1.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/layout_toolbar" app:layout_constraintVertical_bias="0.021" tools:visibility="visible">
+        app:layout_constraintHorizontal_bias="0.454"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toBottomOf="@+id/layout_toolbar"
+        app:layout_constraintVertical_bias="0.122"
+        tools:visibility="visible">
 
         <androidx.appcompat.widget.AppCompatImageView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            app:srcCompat="@mipmap/img_login_logo"/>
+            app:srcCompat="@mipmap/img_login_logo" />
 
         <androidx.appcompat.widget.AppCompatEditText
             android:id="@+id/edit_account"
@@ -78,7 +82,7 @@
             android:textColorHint="@color/color_969696"
             app:bl_solid_color="@color/qmui_config_color_white"
             tools:ignore="HardcodedText"
-            tools:text="gs001"/>
+            tools:text="gs001" />
 
         <androidx.appcompat.widget.AppCompatEditText
             android:id="@+id/edit_password"
@@ -94,7 +98,7 @@
             android:textColorHint="@color/color_969696"
             app:bl_solid_color="@color/qmui_config_color_white"
             tools:ignore="HardcodedText"
-            tools:text="123456"/>
+            tools:text="123456" />
 
         <androidx.appcompat.widget.AppCompatButton
             android:id="@+id/btn_login"
@@ -109,7 +113,7 @@
             android:textSize="@dimen/sp_20"
             android:textStyle="bold"
             app:bl_corners_radius="@dimen/dp_30"
-            app:bl_solid_color="@color/color_12C194"/>
+            app:bl_solid_color="@color/color_12C194" />
 
 
     </androidx.appcompat.widget.LinearLayoutCompat>

+ 6 - 4
app/src/main/res/layout/dialog_validate_face.xml

@@ -10,23 +10,25 @@
 
     <androidx.constraintlayout.widget.ConstraintLayout
         android:id="@+id/camera_layout"
-        android:layout_width="640dp"
+        android:layout_width="400dp"
         android:layout_height="480dp"
         android:gravity="center"
         android:layout_gravity="center"
         android:background="@color/qmui_s_transparent"  >
 
 
-
-
         <androidx.camera.view.PreviewView
             android:id="@+id/mPreviewView"
             android:layout_width="0dp"
             android:layout_height="0dp"
+            android:layout_gravity="center"
+            android:gravity="center"
             app:layout_constraintBottom_toBottomOf="parent"
+            app:layout_constraintHorizontal_bias="1.0"
             app:layout_constraintLeft_toLeftOf="parent"
             app:layout_constraintRight_toRightOf="parent"
-            app:layout_constraintTop_toTopOf="parent" />
+            app:layout_constraintTop_toTopOf="parent"
+            app:layout_constraintVertical_bias="0.0" />
 
     </androidx.constraintlayout.widget.ConstraintLayout>