2024-08-08 18:09:06 +08:00

179 lines
6.3 KiB
TypeScript
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.

import mediaLibrary from '@ohos.multimedia.mediaLibrary'
import onvifclient from '@ohos.onvifclient';
import fs from '@ohos.file.fs'
// @ts-ignore
import photoAccessHelper from '@ohos.file.photoAccessHelper'
import dataSharePredicates from '@ohos.data.dataSharePredicates'
import { getCurrentTime } from '../utils/tools'
// import rtsp_server from '@ohos.rtsprecord';
import record from '@ohos.rtsprecord';
import { FileHelper } from './FileHelper';
const rtsp_server = record.createServer();
//开始录屏
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];
// const rtsp_server = record.createServer();
export async function startRecordVideo(param, td, context, dir) {
return new Promise((reslove, reject) => {
var video_uri = `rtsp://${param.userName}:${param.pwd}@${param.ip}:${param.port}/h264/ch${td}/main/av_stream`;
if (rtsp_server != null) {
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);
const handleId = recordResult.dataInt;
console.log(`Rtsprecord startRecordVideo begin,handleId`, handleId);
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`);
})
}
//结束录屏
export async function endRecordVideo(record_handle) {
if (rtsp_server != null && record_handle > 0) {
console.log(`Rtsprecord endRecordVideo begin`);
// @ts-ignore
var recordResult = rtsp_server.endRecordVideo(record_handle);
console.log(`Rtsprecord startRecordVideo record_handle` + recordResult.dataInt + ` filename:` + recordResult.fileName);
}
else {
console.log(`Rtsprecord endRecordVideo handleId isnull`);
}
// var result = onvifclient.endRecordVideo(record_handle);
// fs.closeSync(record_handle);
// file_asset.close(record_handle);
}
/**
*拍照
* @param param
* @param context
* @param type=0不获取base64
*/
interface takePhotoParam {
name?:string,
base64?:string,
fileSize?:number,
errorCode?:number
}
/**
*
* @param param
* @param context
* @param dir
* @param flag=1 //1同步调用 0异步线程调用
* @param callback
* @returns
*/
const fileHelper = new FileHelper();
export async function takePhoto(param, context,dir,flag=1,callback?) {
var video_uri = `rtsp://${param.userName}:${param.pwd}@${param.ip}:${param.port}/h264/ch${param.pztd}/main/av_stream`;
// var video_uri = `rtsp://admin:openharmony1@192.168.1.66:554/Streaming/Channels/3`;
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, '', dir);
if(flag==0){
rtsp_server.detectVideoSnapshotSize(video_uri,fileName,(err,snapResult)=>{
console.log("baohaowen_detectLoop round end size1:"+snapResult.fileSize);
callback({fileSize:snapResult.fileSize,errorCode:snapResult.errorCode })
});
}
else{
return new Promise<takePhotoParam>((resolve,reject)=>{
rtsp_server.getVideoSnapshot(context, video_uri, fileName,dir,true,(err,snapResult)=>{
console.log('getPhtot',JSON.stringify(snapResult))
resolve({base64: snapResult.dataString,name:snapResult.fileName,fileSize:snapResult.fileSize,errorCode:snapResult.errorCode })
});
})
console.log('getmyLog02')
}
console.log('getmyLog03')
}
//type:1 是图片 2是视频
export async function deleteAllFileByPiC(dirName,type=1) {
// const fileHelper = new FileHelper();
fileHelper.deleteFileOfAlbum(dirName,type);
}
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);
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();
}
}
}