HarmonyOS 从图库获取图片后,如何计算该图片的md5(16位字符串)

let photoSelectOptions = new picker.PhotoSelectOptions();
photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
photoSelectOptions.maxSelectNumber = maxSelectNumber;
let photoPicker = new picker.PhotoViewPicker();
photoPicker.select(photoSelectOptions).then((photoSelectResult: picker.PhotoSelectResult) => {
  console.info('PhotoViewPicker.select successfully, photoSelectResult uri: ' + JSON.stringify(photoSelectResult));
  onSuccess(photoSelectResult)
}).catch((err: BusinessError) => {
  console.error('PhotoViewPicker.select failed with err: ' + JSON.stringify(err));
  onError(err)
});

获取图库图片后,拿到图片uri,如何计算图片md5值?

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

请参考以下demo:

import common from '@ohos.app.ability.common';
import picker from '@ohos.file.picker';
import { BusinessError } from '@kit.BasicServicesKit';
import Hash from '@ohos.file.hash';
import fs from '@ohos.file.fs';

@Entry
@Component
struct Index {
  @State message: string = 'hello World';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(()=>{
            let context = getContext(this) as common.Context; // 请确保 getContext(this) 返回结果为 UIAbilityContext
            let photoSelectOptions = new picker.PhotoSelectOptions();
            photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
            photoSelectOptions.maxSelectNumber = 1;
            let photoPicker = new picker.PhotoViewPicker();
            photoPicker.select(photoSelectOptions).then((photoSelectResult: picker.PhotoSelectResult) => {
              console.info('PhotoViewPicker.select successfully, photoSelectResult uri: ' + JSON.stringify(photoSelectResult));

              let uriString: string = photoSelectResult.photoUris[0]
              let pathDir = context.filesDir;

              let file1 = fs.openSync(uriString)
              let filePath = pathDir + '/' + file1.name
              fs.copyFileSync(file1.fd, filePath)

              console.info("calculate filePath: " + filePath);

              Hash.hash(filePath, "md5").then((str: string) => {
                console.info("calculate file hash succeed:" + str);
              }).catch((err: BusinessError) => {
                console.error("calculate file hash failed with error message: " + err.message + ", error code: " + err.code);
              });

            }).catch((err: BusinessError) => {
              console.error('PhotoViewPicker.select failed with err: ' + JSON.stringify(err));

            });
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

md5计算参考如下:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-hash-V5#hashhash

分享
微博
QQ
微信
回复
3天前
相关问题
HarmonyOS 图片转为base64字符
208浏览 • 1回复 待解决
如何字符串进行MD5哈希处理
2702浏览 • 1回复 待解决
HarmonyOS 如何md5变成字符串
315浏览 • 1回复 待解决
base64字符如何转为图片并保存
2710浏览 • 1回复 待解决
如何将PixelMap转图片base64字符
554浏览 • 1回复 待解决
如何图片base64字符转PixelMap?
641浏览 • 1回复 待解决
将base64字符保存为图片方法
909浏览 • 1回复 待解决
HarmonyOS 给一个字符串MD5demo
34浏览 • 1回复 待解决
HarmonyOS 如何计算文件md5
707浏览 • 1回复 待解决
HarmonyOS 文件md5计算问题
36浏览 • 1回复 待解决
HarmonyOS 字符串图片
78浏览 • 1回复 待解决
PixelMap类型怎么转换成Base64字符
661浏览 • 1回复 待解决