一区二区三区三上|欧美在线视频五区|国产午夜无码在线观看视频|亚洲国产裸体网站|无码成年人影视|亚洲AV亚洲AV|成人开心激情五月|欧美性爱内射视频|超碰人人干人人上|一区二区无码三区亚洲人区久久精品

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

完善資料讓更多小伙伴認識你,還能領(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)、通知、線程間通信、彈框、吐司、生物認證、用戶首選項、拍照、相冊、掃碼、文件、日志,異常捕獲、字符、字符串、數(shù)字、集合、日期、隨機、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 //單選模式下是否需要進大圖預(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)限保存,保存圖片或視頻到相冊
//圖片保存進相冊(已申請權(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)用接口拉起保存確認彈窗
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)容寫入媒體庫的目標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)用接口拉起保存確認彈窗,并保存
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 安全控件保存,提交媒體變更請求,插入圖片/視頻
//安全控件保存,圖片保存進相冊。
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="unquzmj"    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文件大?。?span id="ppstwz3"    class="hljs-variable">${size}n圖片寬度:${with1}n圖片高度:${height}n拍攝日期:${date}n文件標題:${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

    文章

    2146

    瀏覽量

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

掃碼添加小助手

加入工程師交流群

    評論

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

    玩 High API 系列:智能云相冊

    摘要: 相冊是手機中的一個非常重要的功能,然而目前的大部分手機的相冊非常普通,僅僅有查看照片,編輯相片的等幾個簡單功能。我們來看看如果在API時代,如何通過調(diào)用API快速實現(xiàn)一個好玩的,具備智能分析
    發(fā)表于 02-05 13:27

    如何使用imu_utils工具生成IMU的Allan方差標定曲線?

    如何使用imu_utils工具生成IMU的Allan方差標定曲線?
    發(fā)表于 11-22 07:23

    軟件商推谷歌Docs與Outlook同步工具Harmony

    軟件商推谷歌Docs與Outlook同步工具Harmony 北京時間3月10日消息,據(jù)國外媒體報道,美國軟件開發(fā)商Mainsoft周二推出一款名為Harmony的側(cè)邊欄工具,幫助微軟
    發(fā)表于 03-10 09:22 ?839次閱讀

    Microchip Minutes - MPLAB? Harmony專輯 - 第5集 - MPLAB Harmony中的項目恢復(fù)和備份選項

    歡迎觀看Microchip MinutesMPLAB? Harmony專輯。本視頻將介紹如何利用MPLAB Harmony的應(yīng)用程序演示縮短您的開發(fā)時間。
    的頭像 發(fā)表于 06-06 02:45 ?2352次閱讀

    Microchip Minutes - MPLAB? Harmony專輯 - 第5集 - MPLAB Harmony中的項目恢復(fù)和備份選項

    歡迎觀看Microchip MinutesMPLAB? Harmony專輯。本視頻將介紹如何利用MPLAB Harmony的應(yīng)用程序演示縮短您的開發(fā)時間。
    的頭像 發(fā)表于 06-06 13:46 ?2392次閱讀

    MPLAB? Harmony圖形設(shè)計器

    本視頻將介紹MPLAB? Harmony圖形設(shè)計器(MHGC),這款GUI設(shè)計工具現(xiàn)在已作為MPLAB Harmony配置器的一部分集成到了我們獲獎的MPLAB Harmony軟件框架
    的頭像 發(fā)表于 06-07 13:46 ?3380次閱讀
    MPLAB? <b class='flag-5'>Harmony</b>圖形設(shè)計器

    backup-utils Github企業(yè)備份工具

    backup-utils.zip
    發(fā)表于 04-28 15:34 ?0次下載
    backup-<b class='flag-5'>utils</b> Github企業(yè)備份<b class='flag-5'>工具</b>

    如何使用第三方庫中的@pura/harmony-utils(V1.3.3)申請授權(quán)工具

    1. 安裝依賴 ohpm install @pura/harmony-utils 在oh-package.json5中看到以下代碼即表示安裝成功 2. 配置權(quán)限 在 module.json5 文件中
    的頭像 發(fā)表于 06-24 17:31 ?138次閱讀
    如何使用第三方庫中的@pura/<b class='flag-5'>harmony-utils</b>(V1.3.3)申請授權(quán)<b class='flag-5'>工具</b><b class='flag-5'>類</b>一

    如何使用第三方庫中的@pura/harmony-utils(V1.3.3)申請授權(quán)工具

    5.申請授權(quán)(可組合申請)接上文 let p: Permissions[] = [ 'ohos.permission.ACTIVITY_MOTION' , 'ohos.permission.CAMERA' , 'ohos.permission.LOCATION' , 'ohos.permission.APPROXIMATELY_LOCATION' ];PermissionUtil.requestPermissions(p).then((grant) = > { if (grant) { ToastUtil.showToast( `申請授權(quán),已通過...` ); }}) 如果第一次申請用戶沒有同意,可二次申請 PermissionUtil .requestPermissionOnSettingEasy (p) .then ((result) = > { ToastUtil .showToast ( `申請授權(quán),結(jié)果:${result}` );}); 6.申請授權(quán),拒絕后可二次申請 let p: Permissions[] = [ 'ohos
    的頭像 發(fā)表于 06-24 17:32 ?139次閱讀

    harmony-utilsAppUtil,APP相關(guān)工具

    harmony-utils一款功能豐富且極易上手的HarmonyOS工具庫,借助眾多實用工具,致力于助力開發(fā)者迅速構(gòu)建鴻蒙應(yīng)用。
    的頭像 發(fā)表于 06-26 09:24 ?80次閱讀
    <b class='flag-5'>harmony-utils</b><b class='flag-5'>之</b>AppUtil,APP<b class='flag-5'>相關(guān)</b><b class='flag-5'>工具</b><b class='flag-5'>類</b>

    harmony-utilsArrayUtil,集合工具

    # harmony-utilsArrayUtil,集合工具 ## harmony-utils 簡介與說明 ------[
    的頭像 發(fā)表于 06-26 17:38 ?69次閱讀

    harmony-utilsAssetUtil,關(guān)鍵資產(chǎn)存儲服務(wù)工具

    harmony-utilsAssetUtil,關(guān)鍵資產(chǎn)存儲服務(wù)工具
    的頭像 發(fā)表于 06-26 17:42 ?67次閱讀

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

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

    harmony-utilsPickerUtil,拍照、文件選擇和保存,工具

    harmony-utilsPickerUtil,拍照、文件選擇和保存,工具 harmony-utils 簡介與說明
    的頭像 發(fā)表于 06-27 16:03 ?59次閱讀

    HarmonyOS入門指南

    上手的HarmonyOS工具庫,借助眾多實用工具,致力于助力開發(fā)者迅速構(gòu)建鴻蒙應(yīng)用。 2、開源框架 harmony-utils 一款功能豐富且極易上手的HarmonyOS
    的頭像 發(fā)表于 06-27 00:11 ?18次閱讀