国产精品久久久aaaa,日日干夜夜操天天插,亚洲乱熟女香蕉一区二区三区少妇,99精品国产高清一区二区三区,国产成人精品一区二区色戒,久久久国产精品成人免费,亚洲精品毛片久久久久,99久久婷婷国产综合精品电影,国产一区二区三区任你鲁

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

harmony-utils之PhotoHelper,相冊相關工具類

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

掃碼添加小助手

加入工程師交流群

harmony-utils之PhotoHelper,相冊相關工具類

harmony-utils 簡介與說明


harmony-utils 一款功能豐富且極易上手的HarmonyOS工具庫,借助眾多實用工具類,致力于助力開發者迅速構建鴻蒙應用。其封裝的工具涵蓋了APP、設備、屏幕、授權、通知、線程間通信、彈框、吐司、生物認證、用戶首選項、拍照、相冊、掃碼、文件、日志,異常捕獲、字符、字符串、數字、集合、日期、隨機、base64、加密、解密、JSON等一系列的功能和操作,能夠滿足各種不同的開發需求。
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 = `調用相冊,返回uris:n${uris.join('n')}`;
}).catch((err: BusinessError) = > {
  let str = `調用相冊,異常: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 = `調用相冊,返回uris:n${uris.join('n')}`;
}).catch((err: BusinessError) = > {
  let str = `調用相冊,異常:n${JSON.stringify(err)}`;
});
 
//相冊選擇圖片(單選)
let options: photoAccessHelper.PhotoSelectOptions = {
  MIMEType: photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE,
  maxSelectNumber: 1,
  isOriginalSupported: true,
  isPreviewForSingleSelectionSupported: true //單選模式下是否需要進大圖預覽
}
PhotoHelper.selectEasy(options).then((uris) = > {
  let uriStr = `調用相冊,返回uris:n${uris.join('n')}`;
}).catch((err: BusinessError) = > {
  let str = `調用相冊,異常:n${JSON.stringify(err)}`;
});
save 申請權限保存,保存圖片或視頻到相冊
//圖片保存進相冊(已申請權限使用該方法)
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 = `調用保存圖片,異常:n${JSON.stringify(err)}`;
    })
  } else {
    ToastUtil.showLong("請在設置中打開權限");
    WantUtil.toAppSetting();
  }
});
showAssetsCreationDialog 彈窗授權保存,調用接口拉起保存確認彈窗
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++) {
  //將來源于應用沙箱的照片內容寫入媒體庫的目標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 彈窗授權保存,調用接口拉起保存確認彈窗,并保存
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 = `保存圖片失敗:${JSON.stringify(err)}`;
});
getPhotoAsset 獲取對應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文件標題:${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)}`;
});

創作不易,請給童長老點贊

審核編輯 黃宇

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • HarmonyOS
    +關注

    關注

    80

    文章

    2153

    瀏覽量

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

掃碼添加小助手

加入工程師交流群

    評論

    相關推薦
    熱點推薦

    harmony-utilsAuthUtil,生物認證相關工具

    # harmony-utilsAuthUtil,生物認證相關工具 ## harmony-utils
    的頭像 發表于 06-26 17:43 ?452次閱讀

    harmony-utilsCacheUtil,緩存工具

    harmony-utilsCacheUtil,緩存工具
    的頭像 發表于 07-04 16:36 ?495次閱讀

    harmony-utilsCharUtil,字符工具

    harmony-utilsCharUtil,字符工具
    的頭像 發表于 07-04 16:34 ?486次閱讀

    harmony-utilsCrashUtil,異常相關工具

    harmony-utilsCrashUtil,異常相關工具
    的頭像 發表于 07-04 16:33 ?534次閱讀

    harmony-utilsDeviceUtil,設備相關工具

    harmony-utilsDeviceUtil,設備相關工具
    的頭像 發表于 07-03 18:27 ?632次閱讀

    harmony-utilsDisplayUtil,屏幕相關工具

    harmony-utilsDisplayUtil,屏幕相關工具
    的頭像 發表于 07-03 18:26 ?496次閱讀

    harmony-utilsFileUtil,文件相關工具

    harmony-utilsFileUtil,文件相關工具
    的頭像 發表于 07-03 18:23 ?564次閱讀

    harmony-utilsImageUtil,圖片相關工具

    harmony-utilsImageUtil,圖片相關工具
    的頭像 發表于 07-03 18:22 ?883次閱讀

    harmony-utilsLocationUtil,定位相關工具

    harmony-utilsLocationUtil,定位相關工具 harmony-utils
    的頭像 發表于 07-03 18:13 ?519次閱讀

    harmony-utilsNetworkUtil,網絡相關工具

    harmony-utilsNetworkUtil,網絡相關工具 harmony-utils
    的頭像 發表于 06-25 23:46 ?345次閱讀

    harmony-utilsPreviewUtil,文件預覽工具

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

    harmony-utilsSnapshotUtil,截圖相關工具

    harmony-utilsSnapshotUtil,截圖相關工具 harmony-utils
    的頭像 發表于 07-03 11:36 ?518次閱讀

    harmony-utilsStrUtil,字符串工具

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

    harmony-utilsTypeUtil,類型檢查工具

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

    harmony-utilsWindowUtil,窗口相關工具

    harmony-utilsWindowUtil,窗口相關工具 harmony-utils
    的頭像 發表于 06-30 17:33 ?531次閱讀