2024-01-05 11:11:15 +08:00
|
|
|
|
//@ts-ignore
|
|
|
|
|
|
import camera from '@ohos.multimedia.camera';
|
|
|
|
|
|
import image from '@ohos.multimedia.image'
|
|
|
|
|
|
import util from '@ohos.util';
|
|
|
|
|
|
import { writeFile } from '../../common/service/fileService'
|
|
|
|
|
|
import TopLogo from '../compontents/topLogo'
|
|
|
|
|
|
import { cameraService } from '../../common/service/cameraService'
|
|
|
|
|
|
import { voiceService } from '../../common/service/voiceService'
|
|
|
|
|
|
import { faceCompare } from '../../api/userInfo'
|
|
|
|
|
|
import prompt from '@ohos.prompt'
|
|
|
|
|
|
import grantPermission from '../../common/utils/PermissionUtils';
|
|
|
|
|
|
import CameraModel from '../../common/service/CameraModel';
|
|
|
|
|
|
|
|
|
|
|
|
let previewWidth;
|
|
|
|
|
|
let previewHeight;
|
|
|
|
|
|
|
|
|
|
|
|
const PERMISSIONS: Array<string> = [
|
|
|
|
|
|
'ohos.permission.CAMERA']
|
|
|
|
|
|
|
|
|
|
|
|
@Component
|
2024-02-20 10:22:33 +08:00
|
|
|
|
export default struct FaceCompare {
|
2024-01-05 11:11:15 +08:00
|
|
|
|
constructor() {
|
|
|
|
|
|
super()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private mXComponentController: XComponentController = new XComponentController();
|
|
|
|
|
|
@State imageBase64: string = 'data:image/jpeg;base64,'
|
|
|
|
|
|
private cameraModel: CameraModel = new CameraModel();
|
|
|
|
|
|
@Prop sfzh: string;
|
|
|
|
|
|
@Prop firstImage: string;
|
|
|
|
|
|
@Link showFaceCompare: boolean;
|
|
|
|
|
|
@Link faceCompareSucess: boolean;
|
|
|
|
|
|
private cameraObj = null;
|
|
|
|
|
|
@State imageThumbnail: string = '';
|
|
|
|
|
|
private timer = null; //人脸比对执行任务定时器
|
|
|
|
|
|
private stopClose = false; // 阻止用户关闭此组件
|
|
|
|
|
|
private times = 1; //人脸比对失败次数, 超过3次将不会自动比对,需要点击重新打开重新触发
|
|
|
|
|
|
private vocObj = null;
|
|
|
|
|
|
private surfaceId: string = '';
|
|
|
|
|
|
|
|
|
|
|
|
build() {
|
|
|
|
|
|
Column() {
|
|
|
|
|
|
Column() {
|
|
|
|
|
|
Text('照片比对')
|
|
|
|
|
|
.lineHeight('16%')
|
|
|
|
|
|
.height('16%')
|
|
|
|
|
|
.fontWeight(FontWeight.Bold)
|
|
|
|
|
|
.fontSize(24)
|
|
|
|
|
|
|
|
|
|
|
|
Row() {
|
|
|
|
|
|
Row() {
|
|
|
|
|
|
XComponent({
|
|
|
|
|
|
id: 'componentId',
|
|
|
|
|
|
type: 'surface',
|
|
|
|
|
|
controller: this.mXComponentController
|
|
|
|
|
|
})
|
|
|
|
|
|
.onLoad(() => {
|
|
|
|
|
|
// this.mXComponentController.setXComponentSurfaceSize({surfaceWidth:640,surfaceHeight:480});
|
|
|
|
|
|
// let surfaceId = this.mXComponentController.getXComponentSurfaceId()
|
|
|
|
|
|
// console.log('jiangsong: xcomponentController this.surfaceId = ' + surfaceId)
|
|
|
|
|
|
this.mXComponentController.setXComponentSurfaceSize({ surfaceWidth: 640, surfaceHeight: 480 });
|
|
|
|
|
|
this.surfaceId = this.mXComponentController.getXComponentSurfaceId();
|
|
|
|
|
|
this.cameraModel.initCamera(this.surfaceId);
|
|
|
|
|
|
|
|
|
|
|
|
this.readyToTakePhoto(this.surfaceId)
|
|
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
.width('100%')
|
|
|
|
|
|
.height('100%')
|
|
|
|
|
|
.backgroundColor(Color.Black)
|
|
|
|
|
|
}
|
|
|
|
|
|
.width('48%')
|
|
|
|
|
|
.height('100%')
|
|
|
|
|
|
|
|
|
|
|
|
Row() {
|
|
|
|
|
|
Image(this.firstImage)
|
|
|
|
|
|
.width('100%')
|
|
|
|
|
|
.height('100%')
|
|
|
|
|
|
.backgroundColor(Color.Black)
|
|
|
|
|
|
}
|
|
|
|
|
|
.width('48%')
|
|
|
|
|
|
.height('100%')
|
|
|
|
|
|
}
|
|
|
|
|
|
.width('88.9%')
|
|
|
|
|
|
.height('53.2%')
|
|
|
|
|
|
.justifyContent(FlexAlign.SpaceBetween)
|
|
|
|
|
|
|
|
|
|
|
|
Text('正在等待拍照…')
|
|
|
|
|
|
.lineHeight('16%')
|
|
|
|
|
|
.height('16%')
|
|
|
|
|
|
.fontWeight(FontWeight.Bold)
|
|
|
|
|
|
.fontColor('#CC7E00')
|
|
|
|
|
|
|
|
|
|
|
|
Column() {
|
|
|
|
|
|
Row() {
|
|
|
|
|
|
Text('若验证无响应,点击此处')
|
|
|
|
|
|
.lineHeight('100%')
|
|
|
|
|
|
.height('100%')
|
|
|
|
|
|
.fontColor('#000000')
|
|
|
|
|
|
|
|
|
|
|
|
Column() {
|
|
|
|
|
|
Text('重新打开')
|
|
|
|
|
|
.lineHeight('100%')
|
|
|
|
|
|
.height('100%')
|
|
|
|
|
|
.fontColor(Color.White)
|
|
|
|
|
|
.fontSize(23)
|
|
|
|
|
|
}
|
|
|
|
|
|
.width('22.8%')
|
|
|
|
|
|
.height('89.9%')
|
|
|
|
|
|
.backgroundImage($r('app.media.nor'))
|
|
|
|
|
|
.backgroundImageSize({ width: '100%', height: '100%' })
|
|
|
|
|
|
.onClick(async () => {
|
|
|
|
|
|
this.times = 1;
|
|
|
|
|
|
// this.takePhoto('face_check.mp3');
|
|
|
|
|
|
this.readyToTakePhoto(this.surfaceId);
|
|
|
|
|
|
// await this.cameraObj.releaseSource()
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
.width('88.9%')
|
|
|
|
|
|
.height('100%')
|
|
|
|
|
|
.justifyContent(FlexAlign.SpaceBetween)
|
|
|
|
|
|
}
|
|
|
|
|
|
.width('100%')
|
|
|
|
|
|
.height('16%')
|
|
|
|
|
|
.position({ y: '84%' })
|
|
|
|
|
|
.borderRadius({ topLeft: 0, topRight: 0, bottomLeft: 15, bottomRight: 15 })
|
|
|
|
|
|
.backgroundColor('#CCC4B8')
|
|
|
|
|
|
}
|
|
|
|
|
|
.width('68.75%')
|
|
|
|
|
|
.height('73.2%')
|
|
|
|
|
|
.backgroundColor('#E5E3DF')
|
|
|
|
|
|
.borderRadius(15)
|
|
|
|
|
|
.margin({ top: '4%' })
|
|
|
|
|
|
|
|
|
|
|
|
Column() {
|
|
|
|
|
|
}
|
|
|
|
|
|
.width('8.5%')
|
|
|
|
|
|
.height('15.1%')
|
|
|
|
|
|
.backgroundImage($r('app.media.close'))
|
|
|
|
|
|
.backgroundImageSize({ width: '100%', height: '100%' })
|
|
|
|
|
|
.onClick(() => {
|
|
|
|
|
|
// prompt.showToast({
|
|
|
|
|
|
// message: '人脸比对程序正在执行,请等待程序执行完毕。',
|
|
|
|
|
|
// duration: 3000
|
|
|
|
|
|
// });
|
|
|
|
|
|
this.showFaceCompare = false
|
|
|
|
|
|
this.faceCompareSucess = false
|
|
|
|
|
|
return
|
|
|
|
|
|
console.log('jiangsong close the faceCompare componet');
|
|
|
|
|
|
if (!this.stopClose) {
|
|
|
|
|
|
this.showFaceCompare = false
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
prompt.showToast({
|
|
|
|
|
|
message: '人脸比对程序正在执行,请等待程序执行完毕。',
|
|
|
|
|
|
duration: 3000
|
|
|
|
|
|
});
|
|
|
|
|
|
this.showFaceCompare = false
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
.width('100%')
|
|
|
|
|
|
.height('100%')
|
|
|
|
|
|
.backgroundColor('rgba(0, 0, 0, 0.8)')
|
|
|
|
|
|
.position({ x: 0, y: 0 })
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onPageShow() {
|
|
|
|
|
|
this.cameraModel.initCamera(this.surfaceId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async aboutToAppear() {
|
|
|
|
|
|
// this.cameraModel.initCamera(this.surfaceId);
|
|
|
|
|
|
await grantPermission().then(res => {
|
|
|
|
|
|
if (this.surfaceId) {
|
|
|
|
|
|
this.cameraModel.initCamera(this.surfaceId);
|
|
|
|
|
|
}
|
|
|
|
|
|
}).catch(rej => {
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
this.surfaceId = this.mXComponentController.getXComponentSurfaceId();
|
|
|
|
|
|
// this.cameraModel.setTakePictureHandleCallback(this.takePictureHandle.bind(this));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// takePictureHandle = (thumbnail: string) => {
|
|
|
|
|
|
// this.imageThumbnail = thumbnail;
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
async initCamera(surfaceId: string) {
|
|
|
|
|
|
//动态获取隐私权限
|
|
|
|
|
|
let context = getContext(this) as any
|
|
|
|
|
|
await context.requestPermissionsFromUser(PERMISSIONS)
|
|
|
|
|
|
console.log('grantPermission,requestPermissionsFromUser');
|
|
|
|
|
|
// 创建CameraManager对象
|
|
|
|
|
|
let cameraManager = await camera.getCameraManager(context)
|
|
|
|
|
|
if (!cameraManager) {
|
|
|
|
|
|
console.error('Failed to get the CameraManager instance');
|
|
|
|
|
|
}
|
|
|
|
|
|
// 获取相机列表
|
|
|
|
|
|
let cameraArray = await cameraManager.getSupportedCameras()
|
|
|
|
|
|
if (!cameraArray) {
|
|
|
|
|
|
console.error('Failed to get the cameras');
|
|
|
|
|
|
}
|
|
|
|
|
|
for (let index = 0; index < cameraArray.length; index++) {
|
|
|
|
|
|
console.log('cameraId : ' + cameraArray[index].cameraId) // 获取相机ID
|
|
|
|
|
|
console.log('cameraPosition : ' + cameraArray[index].cameraPosition) // 获取相机位置
|
|
|
|
|
|
console.log('cameraType : ' + cameraArray[index].cameraType) // 获取相机类型
|
|
|
|
|
|
console.log('connectionType : ' + cameraArray[index].connectionType) // 获取相机连接类型
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 创建相机输入流
|
|
|
|
|
|
let cameraInput = await cameraManager.createCameraInput(cameraArray[0])
|
|
|
|
|
|
|
|
|
|
|
|
// 打开相机
|
|
|
|
|
|
await cameraInput.open().then(() => {
|
|
|
|
|
|
console.log('opencamera succ.');
|
|
|
|
|
|
}).catch(function (err) {
|
|
|
|
|
|
console.log("opencamera failed with error:" + err);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 获取相机设备支持的输出流能力
|
|
|
|
|
|
let cameraOutputCap = await cameraManager.getSupportedOutputCapability(cameraArray[0]);
|
|
|
|
|
|
if (!cameraOutputCap) {
|
|
|
|
|
|
console.error("outputCapability outputCapability == null || undefined")
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.log("outputCapability: " + JSON.stringify(cameraOutputCap));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//获取相机支持的输出能力--支持的预览配置信息
|
|
|
|
|
|
let previewProfilesArray = cameraOutputCap.previewProfiles;
|
|
|
|
|
|
if (!previewProfilesArray) {
|
|
|
|
|
|
console.error("createOutput previewProfilesArray == null || undefined")
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.log("previewProfiles:" + JSON.stringify(previewProfilesArray[0]))
|
|
|
|
|
|
previewWidth = previewProfilesArray[0].size.width;
|
|
|
|
|
|
previewHeight = previewProfilesArray[0].size.height;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 创建预览输出流,其中参数 surfaceId 参考下面 XComponent 组件,预览流为XComponent组件提供的surface
|
|
|
|
|
|
let previewOutput = await cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId)
|
|
|
|
|
|
if (!previewOutput) {
|
|
|
|
|
|
console.error("Failed to create the PreviewOutput instance.")
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.log("create the PreviewOutput instance succ.")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//创建会话
|
|
|
|
|
|
let captureSession = await cameraManager.createCaptureSession()
|
|
|
|
|
|
if (!captureSession) {
|
|
|
|
|
|
console.error('Failed to create the CaptureSession instance.');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
console.log('Callback returned with the CaptureSession instance.' + captureSession);
|
|
|
|
|
|
|
|
|
|
|
|
// 开始配置会话
|
|
|
|
|
|
await captureSession.beginConfig()
|
|
|
|
|
|
|
|
|
|
|
|
// 向会话中添加相机输入流
|
|
|
|
|
|
await captureSession.addInput(cameraInput)
|
|
|
|
|
|
|
|
|
|
|
|
// 向会话中添加预览输入流
|
|
|
|
|
|
await captureSession.addOutput(previewOutput)
|
|
|
|
|
|
|
|
|
|
|
|
// 提交会话配置
|
|
|
|
|
|
await captureSession.commitConfig()
|
|
|
|
|
|
// 启动会话
|
|
|
|
|
|
await captureSession.start().then(() => {
|
|
|
|
|
|
console.log('captureSession start success.');
|
|
|
|
|
|
}).catch(function (err) {
|
|
|
|
|
|
console.log("captureSession start failed with error:" + err);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async aboutToDisappear() {
|
|
|
|
|
|
clearTimeout(this.timer)
|
|
|
|
|
|
this.vocObj.releasePlayer()
|
|
|
|
|
|
this.cameraObj.releaseSource()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async readyToTakePhoto(surfaceId) {
|
|
|
|
|
|
this.surfaceId = surfaceId;
|
|
|
|
|
|
this.cameraObj = new cameraService(surfaceId);
|
|
|
|
|
|
|
|
|
|
|
|
setTimeout(()=>{
|
|
|
|
|
|
this.showFaceCompare = false
|
|
|
|
|
|
this.faceCompareSucess = true;
|
|
|
|
|
|
},3000)
|
|
|
|
|
|
return
|
|
|
|
|
|
await this.cameraObj.initCamera((img) => {
|
|
|
|
|
|
console.log('jiangsong getPhoto res.' + img.slice(0, 100))
|
|
|
|
|
|
this.imageBase64 = 'data:image/jpeg;base64,' + img;
|
|
|
|
|
|
const firstImage = this.firstImage.substr(22)
|
|
|
|
|
|
faceCompare({
|
|
|
|
|
|
sfzh: this.sfzh,
|
|
|
|
|
|
firstImage: firstImage,
|
|
|
|
|
|
// sfzh: '321281198605076819',
|
|
|
|
|
|
// firstImage: this.firstImage,
|
|
|
|
|
|
secondImage: img,
|
|
|
|
|
|
type: 2,
|
|
|
|
|
|
verifyType: 1
|
|
|
|
|
|
}).then(res => {
|
|
|
|
|
|
if (res) {
|
|
|
|
|
|
console.log('jiangsong face compare success' + JSON.stringify(res))
|
|
|
|
|
|
this.showFaceCompare = false
|
|
|
|
|
|
this.faceCompareSucess = true;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.log('jiangsong face compare faild')
|
|
|
|
|
|
//3s后开始语音提示
|
|
|
|
|
|
this.timer = setTimeout(() => {
|
|
|
|
|
|
this.takePhoto('face_fail.mp3');
|
|
|
|
|
|
}, 3000)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
});
|
|
|
|
|
|
this.vocObj = new voiceService(async (status) => {
|
|
|
|
|
|
console.log('jiangsong voiceService callback. status == ' + status)
|
|
|
|
|
|
if (status == 'completed') {
|
|
|
|
|
|
this.cameraObj.takePhoto();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
//3s后开始语音提示
|
|
|
|
|
|
this.timer = setTimeout(() => {
|
|
|
|
|
|
this.takePhoto('face_check.mp3');
|
|
|
|
|
|
}, 3000)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
takePhoto(name) {
|
|
|
|
|
|
this.stopClose = true;
|
|
|
|
|
|
if (this.times > 3) {
|
|
|
|
|
|
this.stopClose = false;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
this.times++;
|
|
|
|
|
|
|
|
|
|
|
|
this.vocObj.playAudio({
|
|
|
|
|
|
type: 1,
|
|
|
|
|
|
name: name
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-20 10:22:33 +08:00
|
|
|
|
// export default FaceCompare
|