99精品伊人亚洲|最近国产中文炮友|九草在线视频支援|AV网站大全最新|美女黄片免费观看|国产精品资源视频|精彩无码视频一区|91大神在线后入|伊人终合在线播放|久草综合久久中文

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

harmony-utils之PhotoHelper,相冊相關(guān)工具類

童長老 ? 來源:jf_14594073 ? 作者:jf_14594073 ? 2025-06-27 10:24 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

harmony-utils之PhotoHelper,相冊相關(guān)工具類

harmony-utils 簡介與說明


harmony-utils 一款功能豐富且極易上手的HarmonyOS工具庫,借助眾多實用工具類,致力于助力開發(fā)者迅速構(gòu)建鴻蒙應(yīng)用。其封裝的工具涵蓋了APP、設(shè)備、屏幕、授權(quán)、通知、線程間通信、彈框、吐司、生物認(rèn)證、用戶首選項、拍照、相冊、掃碼、文件、日志,異常捕獲、字符、字符串、數(shù)字、集合、日期、隨機(jī)、base64、加密、解密、JSON等一系列的功能和操作,能夠滿足各種不同的開發(fā)需求。
picker_utils 是harmony-utils拆分出來的一個子庫,包含PickerUtil、PhotoHelper、ScanUtil。

下載安裝
ohpm i @pura/harmony-utils
ohpm i @pura/picker_utils

//全局初始化方法,在UIAbility的onCreate方法中初始化 AppUtil.init()
 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
   AppUtil.init(this.context);
 }

API方法與使用


select 通過選擇模式拉起photoPicker界面,用戶可以選擇一個或多個圖片/視頻
//相冊選擇圖片
PhotoHelper.select().then((result) = > {
  let uris = result.photoUris;
  let uriStr = `調(diào)用相冊,返回uris:n${uris.join('n')}`;
}).catch((err: BusinessError) = > {
  let str = `調(diào)用相冊,異常:n${JSON.stringify(err)}`;
});
selectEasy 通過選擇模式拉起photoPicker界面,用戶可以選擇一個或多個圖片/視頻
//相冊選擇圖片/視頻(多選)
let options: photoAccessHelper.PhotoSelectOptions = {
  MIMEType: photoAccessHelper.PhotoViewMIMETypes.IMAGE_VIDEO_TYPE,
  maxSelectNumber: 12,
  isPhotoTakingSupported: false,
  isSearchSupported: false,
  isEditSupported: false,
  isOriginalSupported: true
}
PhotoHelper.selectEasy(options).then((uris) = > {
  let uriStr = `調(diào)用相冊,返回uris:n${uris.join('n')}`;
}).catch((err: BusinessError) = > {
  let str = `調(diào)用相冊,異常:n${JSON.stringify(err)}`;
});
 
//相冊選擇圖片(單選)
let options: photoAccessHelper.PhotoSelectOptions = {
  MIMEType: photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE,
  maxSelectNumber: 1,
  isOriginalSupported: true,
  isPreviewForSingleSelectionSupported: true //單選模式下是否需要進(jìn)大圖預(yù)覽
}
PhotoHelper.selectEasy(options).then((uris) = > {
  let uriStr = `調(diào)用相冊,返回uris:n${uris.join('n')}`;
}).catch((err: BusinessError) = > {
  let str = `調(diào)用相冊,異常:n${JSON.stringify(err)}`;
});
save 申請權(quán)限保存,保存圖片或視頻到相冊
//圖片保存進(jìn)相冊(已申請權(quán)限使用該方法)
let ps: Permissions[] = ['ohos.permission.WRITE_IMAGEVIDEO'];
PermissionUtil.requestPermissions(ps).then((result) = > {
  if (result) {
    let imgName = `測試圖片_${DateUtil.getTodayTime()}`;
    PhotoHelper.save(photoAccessHelper.PhotoType.IMAGE, 'jpg', { title: imgName }).then(async (uri) = > {
      if (uri) {
        let uriStr = `保存圖片成功,返回uris:n${uri}`;
        let file = FileUtil.openSync(uri);
        FileUtil.copyFile(this.filePath, file.fd).then(() = > {
          FileUtil.close(file.fd);
          ToastUtil.showToast("圖片保存成功");
        })
      }
    }).catch((err: BusinessError) = > {
      let str = `調(diào)用保存圖片,異常:n${JSON.stringify(err)}`;
    })
  } else {
    ToastUtil.showLong("請在設(shè)置中打開權(quán)限");
    WantUtil.toAppSetting();
  }
});
showAssetsCreationDialog 彈窗授權(quán)保存,調(diào)用接口拉起保存確認(rèn)彈窗
let pixelMap = await ImageUtil.getPixelMapFromMedia($r("app.media.test_as4"));
let filePath = await ImageUtil.savePixelMap(pixelMap, FileUtil.getFilesDirPath(""), "測試圖片.png");
let uri = FileUtil.getUriFromPath(this.filePath);
let srcFileUris: Array< string >=[uri];

let desFileUris: Array< string > = await PhotoHelper.showAssetsCreationDialog(srcFileUris);
for (let index = 0; index < desFileUris.length; index++) {
  //將來源于應(yīng)用沙箱的照片內(nèi)容寫入媒體庫的目標(biāo)uri
  let srcFile: fs.File = await Utils.open(srcFileUris[index], fs.OpenMode.READ_ONLY);
  let desFile: fs.File = await Utils.open(desFileUris[index], fs.OpenMode.WRITE_ONLY);
  await Utils.copyFile(srcFile.fd, desFile.fd);
  await Utils.close(srcFile);
  await Utils.close(desFile);
}
showAssetsCreationDialogEasy 彈窗授權(quán)保存,調(diào)用接口拉起保存確認(rèn)彈窗,并保存
let pixelMap = await ImageUtil.getPixelMapFromMedia($r("app.media.test_as4"));
let filePath = await ImageUtil.savePixelMap(pixelMap, FileUtil.getFilesDirPath(""), "測試圖片.png");
let uri = FileUtil.getUriFromPath(this.filePath);

PhotoHelper.showAssetsCreationDialogEasy([uri, uri2]).then((result) = > {
  let uriStr = `圖片保存成功,返回uris:n${JSON.stringify(result, null, 2)}`;
  DialogHelper.showToast("圖片保存成功!");
}).catch((error: BusinessError) = > {
  DialogHelper.showToast("圖片保存失??!");
});
applyChanges 安全控件保存,提交媒體變更請求,插入圖片/視頻
//安全控件保存,圖片保存進(jìn)相冊。
let pixelMap = await ImageUtil.getPixelMapFromMedia($r("app.media.test_as4"));
let filePath = await ImageUtil.savePixelMap(pixelMap, FileUtil.getFilesDirPath(""), "測試圖片.png");

let uri = FileUtil.getUriFromPath(this.filePath);
PhotoHelper.applyChanges(uri).then((result) = > {
  let uriStr = `保存圖片成功:${result.uri}`;
}).catch((err: BusinessError) = > {
  let str = `保存圖片失?。?span id="qncq9lr"    class="hljs-subst">${JSON.stringify(err)}`;
});
getPhotoAsset 獲取對應(yīng)uri的PhotoAsset對象,用于讀取文件信息
PickerUtil.selectPhoto().then(async (uris) = > {
  if (uris && uris.length > 0) {
    PhotoHelper.getPhotoAsset(uris[0]).then((photoAsset) = > {
      try {
        let name = photoAsset?.get(photoAccessHelper.PhotoKeys.DISPLAY_NAME);
        let type = photoAsset?.get(photoAccessHelper.PhotoKeys.PHOTO_TYPE);
        let title = photoAsset?.get(photoAccessHelper.PhotoKeys.TITLE.toString());
        let size = photoAsset?.get(photoAccessHelper.PhotoKeys.SIZE.toString());
        let with1 = photoAsset?.get(photoAccessHelper.PhotoKeys.WIDTH.toString());
        let height = photoAsset?.get(photoAccessHelper.PhotoKeys.HEIGHT.toString());
        let date = photoAsset?.get(photoAccessHelper.PhotoKeys.DATE_TAKEN.toString());
        let orientation = photoAsset?.get(photoAccessHelper.PhotoKeys.ORIENTATION.toString());
        let uriStr = `圖片信息:n文件名:${name}n文件類型:${type}n文件大小:${size}n圖片寬度:${with1}n圖片高度:${height}n拍攝日期:${date}n文件標(biāo)題:${title}n圖片文件的方向:${orientation}`
      } catch (err) {
        LogUtil.error("讀取圖片信息失?。? + JSON.stringify(err));
      }
      photoAsset?.getThumbnail((err, pixelMap) = > {
        if (err) {
          LogUtil.error("縮略圖-異常:" + JSON.stringify(err));
          return;
        }
        // this.pixelMap = pixelMap;
      })
    }).catch((err: BusinessError) = > {
      let str = `讀取圖片異常:n${JSON.stringify(err)}`;
    });
  } else {
    ToastUtil.showToast("請選擇圖片");
  }
}).catch((err: BusinessError) = > {
  let str = `異常:n${JSON.stringify(err)}`;
});

創(chuàng)作不易,請給童長老點贊

審核編輯 黃宇

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • HarmonyOS
    +關(guān)注

    關(guān)注

    80

    文章

    2126

    瀏覽量

    33018
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關(guān)推薦
    熱點推薦

    harmony-utilsAuthUtil,生物認(rèn)證相關(guān)工具

    # harmony-utilsAuthUtil,生物認(rèn)證相關(guān)工具 ## harmony-utils
    的頭像 發(fā)表于 06-26 17:43 ?106次閱讀

    harmony-utilsCacheUtil,緩存工具

    harmony-utilsCacheUtil,緩存工具
    的頭像 發(fā)表于 07-04 16:36 ?118次閱讀

    harmony-utilsCharUtil,字符工具

    harmony-utilsCharUtil,字符工具
    的頭像 發(fā)表于 07-04 16:34 ?120次閱讀

    harmony-utilsCrashUtil,異常相關(guān)工具

    harmony-utilsCrashUtil,異常相關(guān)工具
    的頭像 發(fā)表于 07-04 16:33 ?121次閱讀

    harmony-utilsDeviceUtil,設(shè)備相關(guān)工具

    harmony-utilsDeviceUtil,設(shè)備相關(guān)工具
    的頭像 發(fā)表于 07-03 18:27 ?156次閱讀

    harmony-utilsDisplayUtil,屏幕相關(guān)工具

    harmony-utilsDisplayUtil,屏幕相關(guān)工具
    的頭像 發(fā)表于 07-03 18:26 ?134次閱讀

    harmony-utilsFileUtil,文件相關(guān)工具

    harmony-utilsFileUtil,文件相關(guān)工具
    的頭像 發(fā)表于 07-03 18:23 ?134次閱讀

    harmony-utilsImageUtil,圖片相關(guān)工具

    harmony-utilsImageUtil,圖片相關(guān)工具
    的頭像 發(fā)表于 07-03 18:22 ?167次閱讀

    harmony-utilsLocationUtil,定位相關(guān)工具

    harmony-utilsLocationUtil,定位相關(guān)工具 harmony-utils
    的頭像 發(fā)表于 07-03 18:13 ?138次閱讀

    harmony-utilsNetworkUtil,網(wǎng)絡(luò)相關(guān)工具

    harmony-utilsNetworkUtil,網(wǎng)絡(luò)相關(guān)工具 harmony-utils
    的頭像 發(fā)表于 06-25 23:46 ?48次閱讀

    harmony-utilsPreviewUtil,文件預(yù)覽工具

    harmony-utilsPreviewUtil,文件預(yù)覽工具 harmony-utils 簡介與說明 [
    的頭像 發(fā)表于 07-03 11:40 ?117次閱讀

    harmony-utilsSnapshotUtil,截圖相關(guān)工具

    harmony-utilsSnapshotUtil,截圖相關(guān)工具 harmony-utils
    的頭像 發(fā)表于 07-03 11:36 ?112次閱讀

    harmony-utilsStrUtil,字符串工具

    harmony-utilsStrUtil,字符串工具 harmony-utils 簡介與說明 [ha
    的頭像 發(fā)表于 07-03 11:32 ?95次閱讀

    harmony-utilsTypeUtil,類型檢查工具

    harmony-utilsTypeUtil,類型檢查工具 harmony-utils 簡介與說明 [
    的頭像 發(fā)表于 06-30 17:35 ?110次閱讀

    harmony-utilsWindowUtil,窗口相關(guān)工具

    harmony-utilsWindowUtil,窗口相關(guān)工具 harmony-utils
    的頭像 發(fā)表于 06-30 17:33 ?112次閱讀