PhotoHelper.ets 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C) 2024 桃花镇童长老 @pura/harmony-utils
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. *
  15. * https://ohpm.openharmony.cn/#/cn/detail/@pura%2Fharmony-utils
  16. */
  17. import { photoAccessHelper } from '@kit.MediaLibraryKit';
  18. import { dataSharePredicates } from '@kit.ArkData';
  19. import { BusinessError } from '@kit.BasicServicesKit';
  20. import { LogUtil } from '../utils/LogUtil';
  21. const PHOTO_DEFAULT_SELECT_NUMBER: number = 9; //数量
  22. /**
  23. * TODO 相册选择和保存,工具类
  24. * 需要权限:
  25. * ohos.permission.READ_IMAGEVIDEO
  26. * ohos.permission.WRITE_IMAGEVIDEO
  27. *
  28. * author: 桃花镇童长老ᥫ᭡
  29. * since: 2024/05/01
  30. * 仓库主页:https://ohpm.openharmony.cn/#/cn/detail/@pura%2Fharmony-utils
  31. * github: https://github.com/787107497
  32. * gitee: https://gitee.com/tongyuyan/harmony-utils
  33. * CSDN: https://blog.csdn.net/qq_32922545
  34. * QQ交流群: 569512366
  35. */
  36. export class PhotoHelper {
  37. /**
  38. * 通过选择模式拉起photoPicker界面,用户可以选择一个或多个图片/视频。
  39. * @param options
  40. * @returns
  41. */
  42. static async select(options?: photoAccessHelper.PhotoSelectOptions): Promise<Array<string>> {
  43. try {
  44. if (!options) {
  45. options = new photoAccessHelper.PhotoSelectOptions();
  46. }
  47. if (!options.MIMEType) { //可选择的媒体文件类型,若无此参数,则默认为图片和视频类型。
  48. options.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
  49. }
  50. if (!options.maxSelectNumber) { //选择媒体文件数量的最大值,默认9
  51. options.maxSelectNumber = PHOTO_DEFAULT_SELECT_NUMBER;
  52. }
  53. if (options.isPhotoTakingSupported == undefined) {
  54. options.isPhotoTakingSupported = true; //支持拍照。
  55. }
  56. if (options.isEditSupported == undefined) {
  57. options.isEditSupported = true; //支持编辑照片。
  58. }
  59. let photoPicker = new photoAccessHelper.PhotoViewPicker();
  60. let photoSelectResult: photoAccessHelper.PhotoSelectResult = await photoPicker.select(options);
  61. if (photoSelectResult && photoSelectResult.photoUris && photoSelectResult.photoUris.length > 0) {
  62. return photoSelectResult.photoUris
  63. } else {
  64. return [];
  65. }
  66. } catch (err) {
  67. let error = err as BusinessError;
  68. LogUtil.error(`PhotoHelper-select-异常 ~ code: ${error.code} -·- message: ${error.message}`);
  69. return [];
  70. }
  71. }
  72. /**
  73. * 保存图片或视频到相册
  74. * @param photoType 创建的文件类型,IMAGE或者VIDEO类型。
  75. * @param extension 文件名后缀参数,例如:'jpg'。
  76. * @param options 创建选项,例如{title: 'testPhoto'}。
  77. * @returns
  78. */
  79. static async save(photoType: photoAccessHelper.PhotoType, extension: string, options?: photoAccessHelper.CreateOptions): Promise<string | null> {
  80. try {
  81. let photoHelper = photoAccessHelper.getPhotoAccessHelper(getContext());
  82. return await photoHelper.createAsset(photoType, extension, options);
  83. } catch (err) {
  84. let error = err as BusinessError;
  85. LogUtil.error(`PhotoHelper-save-异常 ~ code: ${error.code} -·- message: ${error.message}`);
  86. return null;
  87. }
  88. }
  89. /**
  90. * 获取对应uri的PhotoAsset对象,用于读取文件信息。
  91. * @param uri 文件uri
  92. * PhotoAsset - photoAccessHelper.PhotoKeys:
  93. * URI 'uri' 文件uri。
  94. * PHOTO_TYPE 'media_type' 媒体文件类型。
  95. * DISPLAY_NAME 'display_name' 显示名字。
  96. * SIZE 'size' 文件大小。
  97. * DURATION 'duration' 持续时间(单位:毫秒)。
  98. * WIDTH 'width' 图片宽度(单位:像素)。
  99. * HEIGHT 'height' 图片高度(单位:像素)。
  100. * DATE_TAKEN 'date_taken' 拍摄日期(文件拍照时间距1970年1月1日的秒数值)。
  101. * ORIENTATION 'orientation' 图片文件的方向。
  102. * TITLE 'title' 文件标题。
  103. * getThumbnail 获取缩略图
  104. * @returns
  105. */
  106. static async getPhotoAsset(uri: string): Promise<photoAccessHelper.PhotoAsset | null> {
  107. try {
  108. let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(getContext());
  109. let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
  110. // 配置查询条件,使用PhotoViewPicker选择图片返回的uri进行查询
  111. predicates.equalTo('uri', uri);
  112. let fetchOption: photoAccessHelper.FetchOptions = {
  113. fetchColumns: ['media_type', 'display_name', 'size', 'title', 'width', 'height', 'date_taken',
  114. 'uri', 'duration', 'orientation', 'date_added', 'date_modified', 'is_favorite'],
  115. predicates: predicates
  116. };
  117. let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption);
  118. // 得到uri对应的PhotoAsset对象,读取文件的部分信息
  119. const photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
  120. return photoAsset;
  121. } catch (err) {
  122. let error = err as BusinessError;
  123. LogUtil.error(`PhotoHelper-getPhotoAsset-异常 ~ code: ${error.code} -·- message: ${error.message}`);
  124. return null;
  125. }
  126. }
  127. }