subject-two/entry/src/main/ets/pages/compontents/faceCompareByhaikang.ets
2024-02-22 17:33:10 +08:00

557 lines
18 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//@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';
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';
import buffer from '@ohos.buffer';
import mediaLibrary from '@ohos.multimedia.mediaLibrary'
// import MediaModel from '../../common/utils/MediaModel';
let previewWidth;
let previewHeight;
const PERMISSIONS: Array<string> = [
'ohos.permission.CAMERA']
@Component
export default struct FaceCompare {
constructor() {
super()
}
@State imageBase64: string = 'data:image/jpeg;base64,'
@Prop sfzh: string;
@Prop lsh: string;
@Prop firstImage: string;
@Link showFaceCompare: boolean;
@Link faceCompareSucess: number;
@Link getqkFlag: boolean;
@Link faceCatchImg: string;
@State imageThumbnail: string = '';
private timer = null; //人脸比对执行任务定时器
private stopClose = false; // 阻止用户关闭此组件
private times = 1; //人脸比对失败次数, 超过3次将不会自动比对需要点击重新打开重新触发
private vocObj = null;
@State callBackFlag:boolean=false;
@State @Watch('changeFaceCompareSuccess') showFaceCompare2: Boolean = false;
private surfaceId: string = '';
@State video_url: string = 'rtsp://admin:12345qwe@192.168.5.41:8000/h264/ch2/main/av_stream'
private fileAsset: mediaLibrary.FileAsset = undefined;
@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
@State signNum: number = 0;
private fileUtil: FileUtil
private interval: any
private mediaTest: mediaLibrary.MediaLibrary = mediaLibrary.getMediaLibrary(globalThis.abilityContext)
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,
controller: this.controller,
})
.muted(true)
.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()
this.vocObj&&this.vocObj.releasePlayer()
this.showFaceCompare = false
this.showFaceCompare2 = false
this.faceCompareSucess = -1
return
console.log('jiangsong close the faceCompare componet');
if (!this.stopClose) {
this.showFaceCompare = false
this.showFaceCompare2 = 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);
}
changeFaceCompareSuccess(){
this.callBackFlag=false
clearInterval(this.interval)
this.vocObj&&this.vocObj.releasePlayer()
}
async aboutToAppear() {
this.callBackFlag=false
clearInterval(this.interval)
// this.mediaModel = MediaModel.getMediaInstance();
const fileUtil = new FileUtil(this.context)
this.fileUtil = fileUtil
this.getVideoConfig()
// this.cameraModel.setTakePictureHandleCallback(this.takePictureHandle.bind(this));
}
getqkFn(){
let tmpList = [];
// 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])
const param= {id: 41,list:tmpList,carNo: globalThis.carInfo.carNo,placeId: globalThis.carInfo.examinationRoomId}
globalThis.udpClient2.sendMsg(param, this.context)
}
async faceComparFn() {
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);
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);
// let filePath = pathDir + "/test.txt";
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圖片
//
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
this.showFaceCompare2 = false
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);
}
});
});
}
async heartMsg(context) {
let tmpList=[]
const str =this.lsh
for (let i = 0;i < str.length; i++) {
tmpList.push(this.string2Bytes(str.charCodeAt(i), 1 * 8)[0])
}
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)
// return {id: 31,list:tmpList,carNo:489,placeId:62}
}
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) {
this.vocObj&&this.vocObj.playAudio({
type: 1,
name: 'face_checking.wav'
})
// this.showFaceCompare = false
globalThis.statue=3
this.faceCompareSucess = -1;
this.heartMsg(this.context)
}else{
setTimeout(()=>{
this.faceComparFn()
},2000)
}
} else if (val == 'yzcg.wav') {
// this.sfbdinterfaceFn()
this.showFaceCompare = false
this.showFaceCompare2 = false
this.vocObj&&this.vocObj.releasePlayer()
globalThis.statue=4
this.faceCompareSucess = 1;
}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
}
}
});
return
}
this.param = JSON.parse(data)
// 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
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) {
// this.showFaceCompare = false
globalThis.statue=3
this.faceCompareSucess = -1;
this.vocObj&&this.vocObj.playAudio({
type: 1,
name: 'face_checking.wav'
})
this.heartMsg(this.context)
}else{
setTimeout(()=>{
this.faceComparFn()
},2000)
}
} else if (val == 'yzcg.wav') {
// this.sfbdinterfaceFn()
this.showFaceCompare = false
this.showFaceCompare2 = false
globalThis.statue=4
this.faceCompareSucess = 1;
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
}
// else if(val=='face_checking.wav'){
// // console.log('kkkk')
// this.vocObj&&this.vocObj.releasePlayer()
// // globalThis.statue=3
// // this.faceCompareSucess = -1;
// }
}
});
setTimeout(() => {
this.vocObj&&this.vocObj.playAudio({
type: 1,
name: 'face_check.mp3'
})
}, 1000)
this.setUdp()
// this.cameraModel.initCamera(this.surfaceId);
}
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
}
})
}
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;
}
async aboutToDisappear() {
// this.mediaModel = MediaModel.getMediaInstance();
}
takePhoto(name) {
this.stopClose = true;
if (this.times > 3) {
this.stopClose = false;
return;
}
this.times++;
// this.vocObj.playAudio({
// type: 1,
// name: name
// })
}
}