125 lines
4.6 KiB
TypeScript
125 lines
4.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 configAddress from '../../common/utils/FileConfig'
|
||
|
|
import {getCurrentTime} from '../utils/tools'
|
||
|
|
//开始录屏
|
||
|
|
export async function startRecordVideo(param,td,context) {
|
||
|
|
return new Promise<videoPlayRes>(async (reslove,reject)=>{
|
||
|
|
var video_uri = `rtsp://${param.userName}:${param.pwd}@${param.ip}:${param.port}/h264/ch${td}/main/av_stream`;
|
||
|
|
// var result3 = onvifclient.startRecordVideo(video_uri, "video/" );
|
||
|
|
// const record_handle = result3.dataInt;
|
||
|
|
// console.info( `baoyihu startRecordVideo result:`+result3.result+ ` VideoFile:`+result3.dataString );
|
||
|
|
// reslove(record_handle)
|
||
|
|
// return
|
||
|
|
var mediaTest = mediaLibrary.getMediaLibrary(context);
|
||
|
|
let mediaType = mediaLibrary.MediaType.VIDEO;
|
||
|
|
let DIR_DOCUMENTS = mediaLibrary.DirectoryType.DIR_VIDEO;
|
||
|
|
const num = Math.floor(Math.random() * 10000)
|
||
|
|
const name = `movie_record${getCurrentTime(1)}.mp4`
|
||
|
|
const path = await mediaTest.getPublicDirectory(DIR_DOCUMENTS);
|
||
|
|
mediaTest.createAsset(mediaType, name, path, (error, asset) => {
|
||
|
|
const file_asset = asset; // 记录 ,关闭时用到
|
||
|
|
asset.open('rw', (error, fd) => {
|
||
|
|
if (fd > 0) {
|
||
|
|
var file_path = configAddress.videoSavePath + name;
|
||
|
|
var result = onvifclient.startRecordVideo(video_uri, file_path, fd);
|
||
|
|
var rocord_handle = result.dataInt; // 记录 ,关闭时用到
|
||
|
|
reslove({fd,rocord_handle,file_asset})
|
||
|
|
} else {
|
||
|
|
reslove({fd:0,rocord_handle:0,file_asset:0})
|
||
|
|
console.error('baoyihu startRecordVideo File Open failed with error: ' + error);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
})
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
//结束录屏
|
||
|
|
export async function endRecordVideo(record_handle) {
|
||
|
|
if(this.record_handle >0)
|
||
|
|
{
|
||
|
|
var result = onvifclient.endRecordVideo(record_handle);
|
||
|
|
// this.record_handle = -1;
|
||
|
|
console.info( `baoyihu endRecordVideo result:`+result.result+ ` VideoFile:`+result.dataString );
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
console.info( `baoyihu endRecordVideo no task error`);
|
||
|
|
}
|
||
|
|
// var result = onvifclient.endRecordVideo(filehandle);
|
||
|
|
// fs.closeSync(filehandle);
|
||
|
|
// file_asset.close(filehandle);
|
||
|
|
|
||
|
|
}
|
||
|
|
/**
|
||
|
|
*拍照
|
||
|
|
* @param param
|
||
|
|
* @param context
|
||
|
|
* @param type=0不获取base64
|
||
|
|
*/
|
||
|
|
export async function takePhoto(param,context,type=0) {
|
||
|
|
return new Promise(async (reslove,reject)=>{
|
||
|
|
var video_uri = `rtsp://${param.userName}:${param.pwd}@${param.ip}:${param.port}/h264/ch${param.pztd}/main/av_stream`;
|
||
|
|
|
||
|
|
var mediaTest = mediaLibrary.getMediaLibrary(context);
|
||
|
|
let mediaType = mediaLibrary.MediaType.IMAGE;
|
||
|
|
let DIR_DOCUMENTS = mediaLibrary.DirectoryType.DIR_IMAGE;
|
||
|
|
const path = await mediaTest.getPublicDirectory(DIR_DOCUMENTS);
|
||
|
|
const name="picture_record" + getCurrentTime(1) + ".jpg"
|
||
|
|
// if(type==1){
|
||
|
|
// var result = onvifclient.getVideoSnapshot(video_uri,"")
|
||
|
|
// reslove(result.dataString)
|
||
|
|
// }else{
|
||
|
|
// var result = onvifclient.getVideoSnapshot(video_uri,"")
|
||
|
|
// reslove('')
|
||
|
|
// }
|
||
|
|
// return
|
||
|
|
mediaTest.createAsset(mediaType, name, path, (error, asset) => {
|
||
|
|
asset.open('rw', (error, fd) => {
|
||
|
|
if (fd > 0) {
|
||
|
|
var file_path =configAddress.picSavePath + name
|
||
|
|
// var result3 = onvifclient.getVideoSnapshot(video_uri,"");
|
||
|
|
var result3 = onvifclient.getVideoSnapshot(video_uri, file_path, fd);
|
||
|
|
fs.closeSync(fd);
|
||
|
|
asset.close(fd);
|
||
|
|
if(type==1){
|
||
|
|
fs.lstat(file_path).then((stat) => {
|
||
|
|
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)
|
||
|
|
reslove(result)
|
||
|
|
}).catch((err) => {
|
||
|
|
console.info("get link status failed with error message: " + err.message + ", error code: " + err.code);
|
||
|
|
});
|
||
|
|
}else{
|
||
|
|
reslove('')
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
console.error('baoyihu getVideoSnapshot File Open failed with error: ' + error);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
})
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface videoPlayRes {
|
||
|
|
rocord_handle?: number;
|
||
|
|
fd?: number;
|
||
|
|
file_asset?:any
|
||
|
|
}
|
||
|
|
|