|
@@ -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)
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|