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

439 lines
14 KiB
Plaintext
Raw Normal View History

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';
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
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 faceCatchImg: string;
@State imageThumbnail: string = '';
private timer = null; //人脸比对执行任务定时器
private stopClose = false; // 阻止用户关闭此组件
private times = 1; //人脸比对失败次数, 超过3次将不会自动比对需要点击重新打开重新触发
private vocObj = null;
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
private fileUtil: FileUtil
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
})
.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.releasePlayer()
this.showFaceCompare = false
this.faceCompareSucess = 0
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.mediaModel = MediaModel.getMediaInstance();
const fileUtil = new FileUtil(this.context)
this.fileUtil = fileUtil
setTimeout(() => {
this.vocObj.playAudio({
type: 1,
name: 'face_check.mp3'
})
}, 1000)
this.getVideoConfig()
// this.cameraModel.setTakePictureHandleCallback(this.takePictureHandle.bind(this));
}
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'
console.log('llllllll',fileName,path,JSON.stringify(loginInfo1))
mediaTest.createAsset(mediaType, fileName, path, (error, asset) => {
console.log('llllllll',JSON.stringify(error))
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";
console.log('tttttt111')
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.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 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.showFaceCompare = false
globalThis.statue=3
this.faceCompareSucess = -1;
}else{
setTimeout(()=>{
this.faceComparFn()
},2000)
}
// this.showFaceCompare = false
// this.faceCompareSucess = true;
} else if (val == 'yzcg.wav') {
this.showFaceCompare = false
globalThis.statue=4
this.faceCompareSucess = 1;
}
}
});
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;
}else{
setTimeout(()=>{
this.faceComparFn()
},2000)
}
// this.showFaceCompare = false
// this.faceCompareSucess = true;
} else if (val == 'yzcg.wav') {
this.showFaceCompare = false
globalThis.statue=4
this.faceCompareSucess = 1;
}
}
});
// this.cameraModel.initCamera(this.surfaceId);
}
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
}
}
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
// })
}
}
export default FaceCompare