subject-two/entry/src/main/ets/pages/compontents/faceCompareByhaikang.ets

557 lines
18 KiB
Plaintext
Raw Normal View History

2024-01-05 11:11:15 +08:00
//@ts-ignore
2024-01-31 14:42:14 +08:00
import camera from '@ohos.multimedia.camera';
import image from '@ohos.multimedia.image'
2024-01-05 11:11:15 +08:00
import util from '@ohos.util';
2024-01-31 14:42:14 +08:00
import { writeFile } from '../../common/service/fileService'
import TopLogo from '../compontents/topLogo'
import { cameraService } from '../../common/service/cameraService'
2024-01-05 11:11:15 +08:00
import { voiceService } from '../../common/service/voiceService'
import { faceCompare } from '../../api/userInfo'
import prompt from '@ohos.prompt'
2024-01-31 14:42:14 +08:00
import grantPermission from '../../common/utils/PermissionUtils';
import CameraModel from '../../common/service/CameraModel';
2024-01-05 11:11:15 +08:00
import FileUtil from '../../common/utils/File'
import { VideoConfig } from '../interfaces'
import common from '@ohos.app.ability.common';
import fs from '@ohos.file.fs'
import onvifclient from '@ohos.onvifclient';
2024-01-31 14:42:14 +08:00
import buffer from '@ohos.buffer';
2024-01-05 11:11:15 +08:00
import mediaLibrary from '@ohos.multimedia.mediaLibrary'
2024-01-31 14:42:14 +08:00
// import MediaModel from '../../common/utils/MediaModel';
let previewWidth;
let previewHeight;
const PERMISSIONS: Array<string> = [
'ohos.permission.CAMERA']
2024-01-05 11:11:15 +08:00
@Component
2024-02-22 17:33:10 +08:00
export default struct FaceCompare {
2024-01-05 11:11:15 +08:00
constructor() {
super()
}
@State imageBase64: string = 'data:image/jpeg;base64,'
@Prop sfzh: string;
@Prop lsh: string;
@Prop firstImage: string;
@Link showFaceCompare: boolean;
@Link faceCompareSucess: number;
2024-01-31 14:35:16 +08:00
@Link getqkFlag: boolean;
2024-01-05 11:11:15 +08:00
@Link faceCatchImg: string;
@State imageThumbnail: string = '';
private timer = null; //人脸比对执行任务定时器
private stopClose = false; // 阻止用户关闭此组件
private times = 1; //人脸比对失败次数, 超过3次将不会自动比对需要点击重新打开重新触发
private vocObj = null;
2024-01-31 14:35:16 +08:00
@State callBackFlag:boolean=false;
2024-02-22 17:33:10 +08:00
@State @Watch('changeFaceCompareSuccess') showFaceCompare2: Boolean = false;
2024-01-31 14:35:16 +08:00
2024-01-31 14:42:14 +08:00
private surfaceId: string = '';
2024-01-05 11:11:15 +08:00
@State video_url: string = 'rtsp://admin:12345qwe@192.168.5.41:8000/h264/ch2/main/av_stream'
2024-01-31 14:42:14 +08:00
private fileAsset: mediaLibrary.FileAsset = undefined;
2024-01-05 11:11:15 +08:00
@State previewUri: Resource = $r('app.media.2_nor')
@State curRate: PlaybackSpeed = PlaybackSpeed.Speed_Forward_1_00_X
@State showControls: boolean = false
private controller: VideoController = new VideoController()
@State isAutoPlay: boolean = true
2024-01-31 14:35:16 +08:00
@State signNum: number = 0;
2024-01-05 11:11:15 +08:00
private fileUtil: FileUtil
2024-01-31 14:35:16 +08:00
private interval: any
2024-01-31 14:42:14 +08:00
private mediaTest: mediaLibrary.MediaLibrary = mediaLibrary.getMediaLibrary(globalThis.abilityContext)
2024-01-05 11:11:15 +08:00
private fd: number = -1;
@State param: VideoConfig = {
spls: '',
faceFlag: false,
pztd: '1',
ljlx: '',
ip: '192.168.7.112',
port: '554',
userName: 'admin',
pwd: '12345qwe',
td1: '1',
td2: '2',
td3: '3',
td4: '4',
videoRecord1: false,
videoRecord2: false,
videoRecord3: false,
videoRecord4: false,
rlls: '1'
}
private context = getContext(this) as common.UIAbilityContext;
build() {
Column() {
Column() {
Text('照片比对')
.lineHeight('16%')
.height('16%')
.fontWeight(FontWeight.Bold)
.fontSize(24)
Row() {
Row() {
Video({
src: `rtsp://${this.param.userName}:${this.param.pwd}@${this.param.ip}:${this.param.port}/h264/ch${this.param.rlls}/main/av_stream`,
previewUri: this.previewUri,
currentProgressRate: PlaybackSpeed.Speed_Forward_1_00_X,
2024-01-31 14:35:16 +08:00
controller: this.controller,
2024-01-05 11:11:15 +08:00
})
2024-01-31 14:35:16 +08:00
.muted(true)
2024-01-05 11:11:15 +08:00
.width('100%')
.height('100%')
.autoPlay(this.isAutoPlay)
.controls(this.showControls)
}
.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.controller.stop()
2024-01-31 14:35:16 +08:00
this.vocObj&&this.vocObj.releasePlayer()
2024-01-05 11:11:15 +08:00
this.showFaceCompare = false
2024-01-31 14:35:16 +08:00
this.showFaceCompare2 = false
this.faceCompareSucess = -1
2024-01-05 11:11:15 +08:00
return
console.log('jiangsong close the faceCompare componet');
if (!this.stopClose) {
this.showFaceCompare = false
2024-01-31 14:35:16 +08:00
this.showFaceCompare2 = false
2024-01-05 11:11:15 +08:00
} 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);
}
2024-02-22 17:33:10 +08:00
changeFaceCompareSuccess(){
2024-01-31 14:35:16 +08:00
this.callBackFlag=false
clearInterval(this.interval)
this.vocObj&&this.vocObj.releasePlayer()
}
2024-01-05 11:11:15 +08:00
async aboutToAppear() {
2024-01-31 14:35:16 +08:00
this.callBackFlag=false
clearInterval(this.interval)
2024-01-05 11:11:15 +08:00
// this.mediaModel = MediaModel.getMediaInstance();
const fileUtil = new FileUtil(this.context)
this.fileUtil = fileUtil
this.getVideoConfig()
// this.cameraModel.setTakePictureHandleCallback(this.takePictureHandle.bind(this));
}
2024-01-31 14:35:16 +08:00
getqkFn(){
let tmpList = [];
2024-01-31 14:42:14 +08:00
// const str =globalThis.signNum
// for (let i = 0;i < str.length; i++) {
// tmpList.push(this.string2Bytes(str.charCodeAt(i), 1 * 8)[0])
// }
// const arr = [globalThis.signNum||0, globalThis.statue||1]
// let tmpList = [];
tmpList.push(this.string2Bytes(globalThis.signNum, 1 * 8)[0])
2024-01-31 14:35:16 +08:00
const param= {id: 41,list:tmpList,carNo: globalThis.carInfo.carNo,placeId: globalThis.carInfo.examinationRoomId}
2024-01-31 14:42:14 +08:00
2024-01-31 14:35:16 +08:00
globalThis.udpClient2.sendMsg(param, this.context)
}
2024-02-21 13:34:14 +08:00
2024-01-05 11:11:15 +08:00
async faceComparFn() {
2024-01-31 14:42:14 +08:00
var loginInfo1 = {
host: `http://${this.param.ip}:80`, user: "administrator", pass: this.param.pwd }
// var file_dir = "/data/service/el1/public/netmanager/ethernet/";
// var result3 = onvifclient.getVideoSnapshot(loginInfo1, video_uri, file_dir);
// console.info(`baoyihu endRecordVideo result:` + result3.result + ` pictureFile:` + result3.dataString);
2024-01-05 11:11:15 +08:00
var mediaTest = mediaLibrary.getMediaLibrary(this.context);
let mediaType = mediaLibrary.MediaType.IMAGE;
let DIR_DOCUMENTS = mediaLibrary.DirectoryType.DIR_IMAGE;
const path = await mediaTest.getPublicDirectory(DIR_DOCUMENTS);
const fileName = 'FaceCompar' + Math.floor(Math.random() * 1000) + '.jpg'
mediaTest.createAsset(mediaType, fileName, path, (error, asset) => {
asset.open('rw', (error, fd) => {
if (fd > 0) {
var file_path = "/mnt/hmdfs/100/account/device_view/local/files/Pictures/" + fileName
var result3 = onvifclient.getVideoSnapshot(`rtsp://${this.param.userName}:${this.param.pwd}@${this.param.ip}:${this.param.port}/h264/ch${this.param.rlls}/main/av_stream`, file_path, fd);
fs.closeSync(fd);
asset.close(fd);
2024-01-31 14:42:14 +08:00
// let filePath = pathDir + "/test.txt";
2024-01-05 11:11:15 +08:00
fs.lstat(file_path).then((stat) => {
console.info("get link status succeed, the size of file is" + stat.size);
let file = fs.openSync(file_path, fs.OpenMode.READ_WRITE);
const size = Number(stat.size) + 100
let buf = new ArrayBuffer(size);
let num = fs.readSync(file.fd, buf);
var that = new util.Base64();
var array = new Uint8Array(buf);
var result = that.encodeToStringSync(array); //base64圖片
2024-01-31 14:35:16 +08:00
//
2024-01-05 11:11:15 +08:00
mediaTest.deleteAsset(file_path).then(() => {
console.log('delSuccess')
}).catch(() => {
console.log('delError')
})
this.fileUtil.deleteF(file_path, 3)
if(this.firstImage){
this.faceCatchImg = result
}else{
this.faceCatchImg = ''
}
faceCompare({
sfzh: this.sfzh,
firstImage:this.firstImage.substr(22),
// sfzh: '321281198605076819',
// firstImage: this.firstImage,
secondImage: result,
type: 2,
verifyType: 1
}).then(res => {
if (res) {
this.controller.stop()
console.log('jiangsong face compare success' + JSON.stringify(res))
this.showFaceCompare = false
2024-01-31 14:35:16 +08:00
this.showFaceCompare2 = false
2024-01-05 11:11:15 +08:00
this.faceCompareSucess = 1;
// this.faceCatchImg = result
this.vocObj.playAudio({
type: 1,
name: 'yzcg.wav'
})
} else {
console.log('jiangsong face compare faild')
// this.showFaceCompare = false
// this.faceCompareSucess = false;
this.times++;
//3s后开始语音提示
this.vocObj.playAudio({
type: 1,
name: 'face_fail.mp3'
})
// this.timer = setTimeout(() => {
// this.takePhoto('face_fail.mp3');
// }, 3000)
}
})
// console.log(result)
//result base64
// this.fileUtil.deleteF(file_path,3)
}).catch((err) => {
console.info("get link status failed with error message: " + err.message + ", error code: " + err.code);
});
} else {
console.error('baoyihu getVideoSnapshot File Open failed with error: ' + error);
}
});
});
}
2024-01-31 14:35:16 +08:00
async heartMsg(context) {
let tmpList=[]
const str =this.lsh
for (let i = 0;i < str.length; i++) {
2024-01-31 14:42:14 +08:00
tmpList.push(this.string2Bytes(str.charCodeAt(i), 1 * 8)[0])
2024-01-31 14:35:16 +08:00
}
const param = { id: 46, list: tmpList, carNo: globalThis.carInfo.carNo, placeId: globalThis.carInfo.examinationRoomId,callback:()=>{
console.log('46send')
this.callBackFlag=true
}}
this.interval=setInterval(()=>{
console.log('klklklk',this.callBackFlag)
if(this.callBackFlag){
console.log('klklklk')
const param2 = { id: 47, list: tmpList, carNo: globalThis.carInfo.carNo, placeId: globalThis.carInfo.examinationRoomId }
globalThis.udpClient2&&globalThis.udpClient2.sendMsg(param2,this.context)
}
},1000)
// const param = { id: 31, list: tmpList, carNo: 489, placeId: 62 }
globalThis.udpClient2&&globalThis.udpClient2.sendMsg(param, this.context)
2024-01-05 11:11:15 +08:00
2024-01-31 14:35:16 +08:00
// return {id: 31,list:tmpList,carNo:489,placeId:62}
}
2024-01-05 11:11:15 +08:00
async getVideoConfig() {
const data = await this.fileUtil.readFile('/mnt/hmdfs/100/account/device_view/localfiles/files/config/config3.txt');
console.log('data,data',data)
if(!data){
this.vocObj = new voiceService(async (status, val, next) => {
if (status == 'idle') {
if (val == 'face_check.mp3' || val == 'face_fail.mp3') {
console.log('this.time',this.times)
if (this.times >=3) {
2024-01-31 14:35:16 +08:00
this.vocObj&&this.vocObj.playAudio({
type: 1,
name: 'face_checking.wav'
})
// this.showFaceCompare = false
2024-01-05 11:11:15 +08:00
globalThis.statue=3
this.faceCompareSucess = -1;
2024-01-31 14:35:16 +08:00
this.heartMsg(this.context)
2024-01-05 11:11:15 +08:00
}else{
setTimeout(()=>{
this.faceComparFn()
},2000)
}
} else if (val == 'yzcg.wav') {
2024-01-31 14:35:16 +08:00
// this.sfbdinterfaceFn()
2024-01-05 11:11:15 +08:00
this.showFaceCompare = false
2024-01-31 14:35:16 +08:00
this.showFaceCompare2 = false
this.vocObj&&this.vocObj.releasePlayer()
2024-01-05 11:11:15 +08:00
globalThis.statue=4
this.faceCompareSucess = 1;
2024-01-31 14:35:16 +08:00
}else if(val=='face_checking.wav'){
// globalThis.statue=3
this.faceCompareSucess = -1;
this.callBackFlag=false
this.vocObj&&this.vocObj.releasePlayer()
}else if(val=='face_chekc_fail.wav'){
this.vocObj&&this.vocObj.releasePlayer()
this.faceCompareSucess=-1
this.showFaceCompare = false
this.showFaceCompare2 = false
this.callBackFlag=false
2024-01-05 11:11:15 +08:00
}
}
});
return
}
this.param = JSON.parse(data)
2024-02-20 10:22:33 +08:00
// this.param.userName = JSON.parse(data).userName
// this.param.ip = JSON.parse(data).ip
// this.param.pwd = JSON.parse(data).pwd
// this.param.port = JSON.parse(data).port
// this.param.rlls = JSON.parse(data).rlls
2024-01-05 11:11:15 +08:00
console.log('this.parm', this.param.userName, this.param.pwd, this.param.ip, this.param.port, this.param.rlls)
this.controller.start()
// this.video_url = `rtsp://${this.param.userName}:${this.param.pwd}@${this.param.ip}:${this.param.port}/h264/ch${this.param.rlls}/main/av_stream`;
this.vocObj = new voiceService(async (status, val, next) => {
if (status == 'idle') {
if (val == 'face_check.mp3' || val == 'face_fail.mp3') {
if (this.times >= 3) {
2024-01-31 14:35:16 +08:00
// this.showFaceCompare = false
2024-01-05 11:11:15 +08:00
globalThis.statue=3
this.faceCompareSucess = -1;
2024-01-31 14:35:16 +08:00
this.vocObj&&this.vocObj.playAudio({
type: 1,
name: 'face_checking.wav'
})
this.heartMsg(this.context)
2024-01-05 11:11:15 +08:00
}else{
setTimeout(()=>{
this.faceComparFn()
},2000)
}
} else if (val == 'yzcg.wav') {
2024-01-31 14:35:16 +08:00
// this.sfbdinterfaceFn()
2024-01-05 11:11:15 +08:00
this.showFaceCompare = false
2024-01-31 14:35:16 +08:00
this.showFaceCompare2 = false
2024-01-05 11:11:15 +08:00
globalThis.statue=4
this.faceCompareSucess = 1;
2024-01-31 14:35:16 +08:00
this.vocObj&&this.vocObj.releasePlayer()
} else if(val=='face_chekc_fail.wav'){
this.vocObj&&this.vocObj.releasePlayer()
this.faceCompareSucess=-1
this.showFaceCompare = false
this.showFaceCompare2 = false
this.callBackFlag=false
2024-01-05 11:11:15 +08:00
}
2024-01-31 14:35:16 +08:00
// else if(val=='face_checking.wav'){
// // console.log('kkkk')
// this.vocObj&&this.vocObj.releasePlayer()
// // globalThis.statue=3
// // this.faceCompareSucess = -1;
// }
2024-01-05 11:11:15 +08:00
}
});
2024-01-31 14:35:16 +08:00
setTimeout(() => {
this.vocObj&&this.vocObj.playAudio({
type: 1,
name: 'face_check.mp3'
})
}, 1000)
this.setUdp()
2024-01-05 11:11:15 +08:00
// this.cameraModel.initCamera(this.surfaceId);
}
2024-01-31 14:35:16 +08:00
setUdp(){
const that=this
globalThis.udpClient2&&globalThis.udpClient2.onMessage((val)=>{
console.log('qkqkqk',val.id,val.body)
if(val.id=='32'){
globalThis.signNum=val.body[1]
if(val.body[0]=='7'){
this.getqkFn()
this.signNum=val.body[1]
}
}else if(val.id=='48'){
if(val.body[13]=='1'){
that.showFaceCompare = false
that.showFaceCompare2 = false
that.vocObj&&that.vocObj.releasePlayer()
that.faceCompareSucess=1
this.callBackFlag=false
clearInterval(that.interval)
}else if(val.body[13]=='0'&&this.callBackFlag){
this.callBackFlag=false
globalThis.statue=2
console.log('callBackFlag',this.callBackFlag)
that.vocObj&&that.vocObj.playAudio({
type: 1,
name: 'face_chekc_fail.wav'
})
}
}else if(val.id=='42'){
console.log('qkqkqk')
this.getqkFlag=!this.getqkFlag
2024-01-05 11:11:15 +08:00
}
2024-01-31 14:35:16 +08:00
})
2024-01-05 11:11:15 +08:00
}
2024-01-31 14:42:14 +08:00
fillZero(str, len) {
str = str + '';
if (str.length > len || !len) {
return str
}
let num = len - str.length;
let zeroStr = '';
for (var i = 0; i < num; i++) {
zeroStr = zeroStr + '0'
}
return zeroStr + str;
}
// takePictureHandle = (thumbnail: string) => {
// this.imageThumbnail = thumbnail;
// };
string2Bytes(number, len) {
// console.log('string2Bytes == ', number)
let str = (+number).toString(2);
if(str.length > len) {
console.log('数据长度不对~~');
return
}
var byteString = this.fillZero(str, len);
var arrBytes = new Array();
for (var i = byteString.length; i > 0;) {
let j = i - 8;
if (j < 0) {
j = 0
}
var s = byteString.slice(j, i);
var v = parseInt(s, 2);
arrBytes.push(v);
i = i - 8
}
return arrBytes;
}
2024-01-05 11:11:15 +08:00
async aboutToDisappear() {
2024-01-31 14:42:14 +08:00
// this.mediaModel = MediaModel.getMediaInstance();
2024-01-05 11:11:15 +08:00
}
takePhoto(name) {
this.stopClose = true;
if (this.times > 3) {
this.stopClose = false;
return;
}
this.times++;
// this.vocObj.playAudio({
// type: 1,
// name: name
// })
}
}