71 lines
2.6 KiB
TypeScript
71 lines
2.6 KiB
TypeScript
import mediaLibrary from '@ohos.multimedia.mediaLibrary'
|
|
import onvifclient from '@ohos.onvifclient';
|
|
import fs from '@ohos.file.fs'
|
|
import util from '@ohos.util';
|
|
import FileUtil from '../../../common/utils/File'
|
|
|
|
interface Params{
|
|
userName:string
|
|
pwd:string
|
|
ip:string
|
|
port:string
|
|
rlls:string
|
|
}
|
|
|
|
export default class FilePhoto{
|
|
|
|
private params:Params
|
|
private context:any
|
|
private fileUtil:FileUtil
|
|
public mediaTest
|
|
|
|
constructor(context) {
|
|
(async ()=>{
|
|
const fileUtil = new FileUtil(context)
|
|
const strConfig = await fileUtil.readFile('/mnt/hmdfs/100/account/device_view/local/files/logs/config/config3.txt');
|
|
const config = JSON.parse(strConfig)
|
|
const {userName,ip,pwd,port,rlls} = config
|
|
this.params = {userName,pwd,ip,port,rlls}
|
|
this.context = context
|
|
this.fileUtil = fileUtil
|
|
})()
|
|
}
|
|
|
|
public async getPhoto(){
|
|
const {params,context,fileUtil} = this;
|
|
const {userName,pwd,ip,port,rlls} = params;
|
|
const mediaTest = mediaLibrary.getMediaLibrary(context);
|
|
let mediaType = mediaLibrary.MediaType.IMAGE;
|
|
let DIR_DOCUMENTS = mediaLibrary.DirectoryType.DIR_IMAGE;
|
|
const path = await mediaTest.getPublicDirectory(DIR_DOCUMENTS);
|
|
|
|
return new Promise(async (resolve)=>{
|
|
mediaTest.createAsset(mediaType, 'judge_face.jpg', path,(error,asset)=>{
|
|
asset.open('rw', (error, fd) => {
|
|
if (fd > 0) {
|
|
const file_path = "/mnt/hmdfs/100/account/device_view/local/files/Pictures/judge_face.jpg"
|
|
const result3 = onvifclient.getVideoSnapshot(`rtsp://${userName}:${pwd}@${ip}:${port}/h264/ch${rlls}/main/av_stream`,file_path,fd);
|
|
fs.closeSync(fd);
|
|
asset.close(fd);
|
|
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);
|
|
const that = new util.Base64();
|
|
const array = new Uint8Array(buf);
|
|
const result = that.encodeToStringSync(array);//base64圖片
|
|
fileUtil.deleteF(file_path,3)
|
|
resolve(result)
|
|
}).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);
|
|
}
|
|
});
|
|
});
|
|
})
|
|
}
|
|
} |