2024-02-22 10:40:35 +08:00
|
|
|
|
import mediaLibrary from '@ohos.multimedia.mediaLibrary'
|
|
|
|
|
|
import onvifclient from '@ohos.onvifclient';
|
|
|
|
|
|
import fs from '@ohos.file.fs'
|
2024-03-12 15:32:48 +08:00
|
|
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
import photoAccessHelper from '@ohos.file.photoAccessHelper'
|
|
|
|
|
|
import dataSharePredicates from '@ohos.data.dataSharePredicates'
|
2024-02-26 15:01:27 +08:00
|
|
|
|
import { getCurrentTime } from '../utils/tools'
|
2024-03-12 15:32:48 +08:00
|
|
|
|
// import rtsp_server from '@ohos.rtsprecord';
|
2024-02-26 15:01:27 +08:00
|
|
|
|
import record from '@ohos.rtsprecord';
|
2024-05-09 13:42:56 +08:00
|
|
|
|
import { FileHelper } from './FileHelper';
|
|
|
|
|
|
|
2024-03-12 15:32:48 +08:00
|
|
|
|
// const rtsp_server = record.createServer();
|
2024-02-22 10:40:35 +08:00
|
|
|
|
//开始录屏
|
2024-03-12 15:32:48 +08:00
|
|
|
|
const FILE_ASSET_FETCH_COLUMNS = [photoAccessHelper.PhotoKeys.URI,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.PHOTO_TYPE,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.DISPLAY_NAME,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.SIZE,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.DATE_ADDED,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.DATE_MODIFIED,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.DURATION,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.WIDTH,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.HEIGHT,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.DATE_TAKEN,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.ORIENTATION,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.FAVORITE,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.TITLE,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.POSITION,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.DATE_TRASHED,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.HIDDEN];
|
|
|
|
|
|
|
2024-02-26 15:01:27 +08:00
|
|
|
|
export async function startRecordVideo(param, td, context, dir) {
|
2024-03-12 15:32:48 +08:00
|
|
|
|
return new Promise((reslove, reject) => {
|
|
|
|
|
|
const rtsp_server = record.createServer();
|
2024-02-22 10:40:35 +08:00
|
|
|
|
var video_uri = `rtsp://${param.userName}:${param.pwd}@${param.ip}:${param.port}/h264/ch${td}/main/av_stream`;
|
2024-02-26 15:01:27 +08:00
|
|
|
|
if (rtsp_server != null) {
|
2024-03-12 15:32:48 +08:00
|
|
|
|
console.log(`Rtsprecord startRecordVideo begin,video_uri`, video_uri);
|
|
|
|
|
|
const num = Math.floor(Math.random() * 10000)
|
|
|
|
|
|
const fileName = `movie_record${num}.mp4`
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
var recordResult = rtsp_server.startRecordVideo(context, video_uri, fileName, dir);
|
2024-02-26 15:01:27 +08:00
|
|
|
|
const handleId = recordResult.dataInt;
|
2024-03-12 15:32:48 +08:00
|
|
|
|
console.log(`Rtsprecord startRecordVideo begin,handleId`, handleId);
|
|
|
|
|
|
|
2024-02-26 15:01:27 +08:00
|
|
|
|
reslove(handleId)
|
|
|
|
|
|
console.log(`Rtsprecord startRecordVideo record_handle` + recordResult.dataInt);
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
console.log(`Rtsprecord startRecordVideo rtsp_server isnull`);
|
|
|
|
|
|
reslove(0)
|
|
|
|
|
|
}
|
|
|
|
|
|
console.log(`Rtsprecord startRecordVideo end`);
|
2024-02-22 10:40:35 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//结束录屏
|
|
|
|
|
|
export async function endRecordVideo(record_handle) {
|
2024-03-12 15:32:48 +08:00
|
|
|
|
const rtsp_server = record.createServer();
|
2024-02-26 15:01:27 +08:00
|
|
|
|
if (rtsp_server != null && record_handle > 0) {
|
|
|
|
|
|
console.log(`Rtsprecord endRecordVideo begin`);
|
2024-03-12 15:32:48 +08:00
|
|
|
|
// @ts-ignore
|
2024-02-26 15:01:27 +08:00
|
|
|
|
var recordResult = rtsp_server.endRecordVideo(record_handle);
|
|
|
|
|
|
console.log(`Rtsprecord startRecordVideo record_handle` + recordResult.dataInt + ` filename:` + recordResult.fileName);
|
2024-02-22 10:40:35 +08:00
|
|
|
|
}
|
2024-02-26 15:01:27 +08:00
|
|
|
|
else {
|
|
|
|
|
|
console.log(`Rtsprecord endRecordVideo handleId isnull`);
|
2024-02-22 10:40:35 +08:00
|
|
|
|
}
|
2024-02-26 15:01:27 +08:00
|
|
|
|
// var result = onvifclient.endRecordVideo(record_handle);
|
|
|
|
|
|
// fs.closeSync(record_handle);
|
|
|
|
|
|
// file_asset.close(record_handle);
|
2024-02-22 10:40:35 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
|
|
|
*拍照
|
|
|
|
|
|
* @param param
|
|
|
|
|
|
* @param context
|
|
|
|
|
|
* @param type=0不获取base64
|
|
|
|
|
|
*/
|
2024-05-09 13:42:56 +08:00
|
|
|
|
interface takePhotoParam {
|
|
|
|
|
|
name?:string,
|
|
|
|
|
|
base64?:string,
|
|
|
|
|
|
fileSize?:number,
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param param
|
|
|
|
|
|
* @param context
|
|
|
|
|
|
* @param dir
|
|
|
|
|
|
* @param flag=1 //1同步调用 0异步线程调用
|
|
|
|
|
|
* @param callback
|
|
|
|
|
|
* @returns
|
|
|
|
|
|
*/
|
|
|
|
|
|
export async function takePhoto(param, context,dir,flag=1,callback?) {
|
2024-03-12 15:32:48 +08:00
|
|
|
|
|
|
|
|
|
|
const rtsp_server = record.createServer();
|
2024-02-22 10:40:35 +08:00
|
|
|
|
var video_uri = `rtsp://${param.userName}:${param.pwd}@${param.ip}:${param.port}/h264/ch${param.pztd}/main/av_stream`;
|
2024-03-12 15:32:48 +08:00
|
|
|
|
|
|
|
|
|
|
const num = Math.floor(Math.random() * 10000)
|
|
|
|
|
|
const fileName = `picture_record${num}.jpg`
|
|
|
|
|
|
console.log(`baoyihu Rtsprecord baohaowen getVideoSnapshot fileName:` + fileName);
|
|
|
|
|
|
// @ts-ignore
|
2024-05-09 13:42:56 +08:00
|
|
|
|
// var snapResult = rtsp_server.getVideoSnapshot(context, video_uri, '', dir);
|
|
|
|
|
|
if(flag==0){
|
|
|
|
|
|
rtsp_server.getVideoSnapshot(context, video_uri, fileName,dir,false,(err,snapResult)=>{
|
|
|
|
|
|
console.log(`baoyihu getVideoSnapshot return `);
|
|
|
|
|
|
console.log(`baoyihu getVideoSnapshot file_name:` + snapResult.fileName);
|
|
|
|
|
|
console.log(`baoyihu getVideoSnapshot file_path:` + snapResult.filePath);
|
|
|
|
|
|
console.log(`baoyihu getVideoSnapshot fileSize:` + snapResult.fileSize);
|
|
|
|
|
|
callback({base64: snapResult.dataString,name:snapResult.fileName,fileSize:snapResult.fileSize })
|
|
|
|
|
|
});
|
|
|
|
|
|
}else{
|
|
|
|
|
|
return new Promise<takePhotoParam>((resolve,reject)=>{
|
|
|
|
|
|
rtsp_server.getVideoSnapshot(context, video_uri, fileName,dir,true,(err,snapResult)=>{
|
|
|
|
|
|
resolve({base64: snapResult.dataString,name:snapResult.fileName,fileSize:snapResult.fileSize })
|
|
|
|
|
|
});
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2024-03-12 15:32:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-02-22 10:40:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-09 13:42:56 +08:00
|
|
|
|
export async function deleteAllFileByPiC(dirName) {
|
|
|
|
|
|
const fileHelper = new FileHelper();
|
|
|
|
|
|
fileHelper.deleteFileOfAlbum(dirName,1);
|
2024-02-26 15:01:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-12 15:32:48 +08:00
|
|
|
|
export async function deleteAllVideos(context,type: photoAccessHelper.AlbumType, subType: photoAccessHelper.AlbumSubtype): Promise<void> {
|
|
|
|
|
|
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
const userFileMgr = photoAccessHelper.getPhotoAccessHelper(context);
|
|
|
|
|
|
fetchResult = await userFileMgr.getAlbums(type, subType);
|
|
|
|
|
|
console.log('baoyihu get Album fetchResult, count: ' + fetchResult.getCount());
|
|
|
|
|
|
for (let i = 0; i < fetchResult.getCount(); i++) {
|
|
|
|
|
|
let albumAsset: photoAccessHelper.Album = await fetchResult.getObjectByPosition(i);
|
|
|
|
|
|
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = null;
|
|
|
|
|
|
let count = 0;
|
|
|
|
|
|
try {
|
|
|
|
|
|
let predicates = new dataSharePredicates.DataSharePredicates();
|
|
|
|
|
|
let fetchOptions = {
|
|
|
|
|
|
fetchColumns: FILE_ASSET_FETCH_COLUMNS,
|
|
|
|
|
|
predicates: predicates
|
|
|
|
|
|
};
|
|
|
|
|
|
photoFetchResult = await albumAsset.getAssets(fetchOptions);
|
|
|
|
|
|
count = photoFetchResult.getCount();
|
|
|
|
|
|
console.log('baoyihu photoFetchResult count: ' + count);
|
|
|
|
|
|
var all_fileAsset = await photoFetchResult.getAllObjects();
|
|
|
|
|
|
var uri_array = []
|
|
|
|
|
|
console.log('baoyihu batch delete begin');
|
|
|
|
|
|
for (let onfile of all_fileAsset) {
|
|
|
|
|
|
console.log('baoyihu push one uri : ' + onfile.uri);
|
|
|
|
|
|
uri_array.push(onfile.uri);
|
|
|
|
|
|
}
|
|
|
|
|
|
await userFileMgr.deleteAssets(uri_array);
|
2024-02-22 10:40:35 +08:00
|
|
|
|
|
2024-03-12 15:32:48 +08:00
|
|
|
|
console.log('baoyihu batch delete end');
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.log('baoyihu get Album getPhotoAssets failed with err: ' + err);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
if (photoFetchResult != null) {
|
|
|
|
|
|
photoFetchResult.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.log('baoyihu get Album fetchResult failed with err: ' + err);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
if (fetchResult != null) {
|
|
|
|
|
|
fetchResult.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// export async function takePhoto(param, context, type = 0, dir) {
|
|
|
|
|
|
// return new Promise((reslove, reject) => {
|
|
|
|
|
|
// console.log('mmmmmmmttt,04', type)
|
|
|
|
|
|
//
|
|
|
|
|
|
// // const rtsp_server = record.createServer();
|
|
|
|
|
|
// var video_uri = `rtsp://${param.userName}:${param.pwd}@${param.ip}:${param.port}/h264/ch${param.pztd}/main/av_stream`;
|
|
|
|
|
|
// // var snapResult = rtsp_server.getVideoSnapshot(context, video_uri, dir);
|
|
|
|
|
|
// // console.log(`baoyihu Rtsprecord getVideoSnapshot file:` + snapResult.dataString);
|
|
|
|
|
|
//
|
|
|
|
|
|
// console.log(`baoyihu Rtsprecord getVideoSnapshot begin:`);
|
|
|
|
|
|
// // const num = Math.floor(Math.random() * 10000)
|
|
|
|
|
|
// console.log('mmmmmmmttt,05', video_uri)
|
|
|
|
|
|
//
|
|
|
|
|
|
// const num = Math.floor(Math.random() * 10000)
|
|
|
|
|
|
// const fileName = `picture_record${num}.jpg`
|
|
|
|
|
|
// console.log(`baoyihu Rtsprecord baohaowen getVideoSnapshot fileName:` + fileName);
|
|
|
|
|
|
// // @ts-ignore
|
|
|
|
|
|
// var snapResult = rtsp_server.getVideoSnapshot(context, video_uri, fileName, dir, (ret) => {
|
|
|
|
|
|
// const str = ret.dataString
|
|
|
|
|
|
// if (type == 1) {
|
|
|
|
|
|
// console.log('mmmmmmmttt',str.length)
|
|
|
|
|
|
// reslove(str)
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// reslove('')
|
|
|
|
|
|
// }
|
|
|
|
|
|
// })
|
|
|
|
|
|
// // console.log('mmmmmmmttt,06',snapResult.dataString)
|
|
|
|
|
|
//
|
|
|
|
|
|
// // console.log(`baoyihu Rtsprecord getVideoSnapshot file:` + snapResult.fileName);
|
|
|
|
|
|
// // console.log(`baoyihu Rtsprecord getVideoSnapshot dataLen:` + snapResult.dataString.length);
|
|
|
|
|
|
// // console.log('datadata1',snapResult.dataString.length)
|
|
|
|
|
|
// })
|
|
|
|
|
|
//
|
|
|
|
|
|
// }
|