当前位置: 首页 > news >正文

HarmonyOS:如何将图片转为PixelMap并进行图片缓存策略

前言:在HarmonyOS项目开发中,我们使用Ark-Ts语言开发项目。我们有个功能是拍照,除了正常显示出来,并且上传服务器。我在开发过程中,遇到的问题是,如果离开这个页面再回到当前页面仍要显示图片,那我的思路就是存储在沙盒,重新回到这个页面先去沙盒里面查找照片,如果找到就显示出来。

流程图:

请添加图片描述

1.模拟拍照:
模拟拍照方法
takePhoto(){
    let pixelMap1 = await SnapshotUtil.snapshot()
    //添加水印信息(此处省略)
    //调用步骤2.
    //照片存储到本地,并返回路径
    let filePath:string = await WinPixelImageTool.asyncSavePixelImageToFileCache(waterMarkPixelMap);
    //存储照片存储沙盒路径
    let imageKey = '自定义key值'
    PreferencesManager.set(imageKey,filePath);

}
/**
   * 获取窗口截图,使用Promise异步回调。
   * @param windowClass 不传默认截图主窗口
   * @returns
   */
  static async snapshot(windowClass?: window.Window): Promise<image.PixelMap> {
    return (windowClass ?? AppUtil.getMainWindow()).snapshot();
  }
2.生成图片存储路径
static asyncSavePixelImageToFileCache(pixelIamge:PixelMap){
    return new Promise<string>((resolve)=>{
      const imagePackerApi: image.ImagePacker = image.createImagePacker();
      let packOpts : image.PackingOption = { format:"image/png", quality:100 };
      const context : Context = AppUtil.getContext();
      let fileName = DateUtil.getTodayTime().toString();
      let path : string = context.cacheDir + `/sfa`;
      if (!FileUtil.accessSync(path)) {
        FileUtil.mkdirSync(path)
      }
      path = `${path}/pixel_map_${fileName}.png`
      let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
      imagePackerApi.packToFile(pixelIamge, file.fd, packOpts,(error: BusinessError): void => {
        if (error) {
          resolve('')
        }else {
          // let fileUriPath = fileUri.getUriFromPath(path);
          resolve(path)
        }
      })
    })
  }

//存储方法

static set(key: string, value: dataPreferences.ValueType) {
    if (context==undefined) {
      context = AppUtil.getContext()
    }
    let preferences = dataPreferences.getPreferencesSync(context, { name: preferencesName });
    preferences?.putSync(key, value);
    preferences?.flush();
  }
3.如何将图片转为PixelMap并且显示出来,方法如下:

/*

  • 沙盒目录下的照片转为image.PixelMap
  • imageId是存储照片沙盒路径的key
  • localImagePath 是照片的存储沙盒路径
    */
//通过Imagekey获取图片,比如门头照显示就是此方法
  static getImageByImageId(imageId:string):Promise<image.PixelMap> {
    return new Promise<image.PixelMap>(async (resolve,reject) => {
      let localImagePath =  PreferencesManager.get(imageId) as string;
      if (localImagePath&&localImagePath.length>0) {
        let tmpImagePixelMap =  await WinImagePixelMapUtils.getImagePixelMapWithFilePath(localImagePath);
        resolve(tmpImagePixelMap.pixelMap);
      }else {
        reject();
      }
    })
  }
  
  //WinImagePixelMapUtils类
static async getImagePixelMapWithFilePath(filePath:string){
    let imageSource = image.createImageSource(filePath);
    return await imageSource2PixelMap(imageSource);
  }
  
export  async function imageSource2PixelMap(imageSource: image.ImageSource): Promise<ImagePixelMap> {
  const imageInfo: image.ImageInfo = await imageSource.getImageInfo();
  const height = imageInfo.size.height;
  const width = imageInfo.size.width;
  const options: image.DecodingOptions = {
    editable: true,
    desiredSize: { height, width }
  };
  const pixelMap: PixelMap = await imageSource.createPixelMap(options);
  const result: ImagePixelMap = { pixelMap, width, height };
  return result;
}

getImageByImageId
通过此方法找到沙盒里面的图片,
使用Image组件可以直接加载tmpImagePixelMap.pixelMap

相关文章:

  • linux0.11内核源码修仙传第四章——操作系统的框架代码
  • 删除已加入 .gitignore却仍被git追踪的文件
  • 突破极限:高性能ROCK 220A-M 工业级无人机电调深度测评 —— 无人机动力系统的核心守护者
  • 审批流AntV框架蚂蚁数据可视化X6饼图(附注释)
  • Java 8 Stream API 详解
  • MySQL的安装和配置
  • 【Vue3 Element UI - Plus + Tyscript 实现Tags标签输入及回显】
  • 阿里云QwQ-32B模型发布:AI领域的新突破
  • 机器学习数学基础:43.外生变量与内生变量
  • linux固定IP并解决虚拟机无法ping其他电脑问题
  • Java面试第八山!《Spring框架》
  • 中学学习难点管理思维魔方
  • Qt的QTextBrowser`的样式设计
  • Spring Boot API 项目中 HAProxy 与 Nginx 的选择与实践
  • 《V8 引擎狂飙,Node.js 续写 JavaScript 传奇》
  • nodejs关于后端服务开发的探究
  • 豪越智慧消防安全:电气火灾监控系统守护用电安全
  • upload-labs详解(1-12)文件上传分析
  • 算法系列之数据结构-Huffman树
  • 【够用就好006】如何从零开发游戏上架steam面向AI编程的godot独立游戏制作实录001流程
  • 免费网站建站申请/班级优化大师学生版
  • 购物网站制作教程/今天的新闻是什么
  • 衡阳网站建设开发价格/代运营公司可靠吗
  • 哪里做网站比较快/网站怎样做推广
  • 网站设计属于什么经营范围/aso优化怎么做
  • 大兴区社会建设工作办公室网站/网络营销方式