鸿蒙选取相册视频,获取封面图并上传阿里云OSS

继上一次完成了鸿蒙选取图片上传阿里云OSS后, 这次记录下选择相册视频,并生成PixelMap 缩略图显示在UI中,并在上传的时候生成沙箱地址用于上传。

1、通过picker 选择本地视频返回的视频地址生成缩略图

复制代码
const getVideoThumb = async (filePath: string) => {
  // 创建AVImageGenerator对象
  let avImageGenerator: media.AVImageGenerator = await media.createAVImageGenerator()
  let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY);
  let avFileDescriptor: media.AVFileDescriptor = { fd: file.fd };
  avImageGenerator.fdSrc = avFileDescriptor;
  // 初始化入参
  let timeUs = 0
  let queryOption = media.AVImageQueryOptions.AV_IMAGE_QUERY_NEXT_SYNC
  let param: media.PixelMapParams = {
    width : 300,
    height : 400,
  }
  // 获取缩略图(promise模式)
  let pixelMap = await avImageGenerator.fetchFrameByTime(timeUs, queryOption, param)
  // 释放资源(promise模式)
  avImageGenerator.release()
  console.info(`release success.`)
  fs.closeSync(file)

  return pixelMap
}

2、通过pixelMap 生成封面图的沙箱地址用于OSS上传

复制代码
let packOpts: image.PackingOption = { format: "image/jpeg", quality: 98 }
const imagePackerApi = image.createImagePacker();
imagePackerApi.packing(pixelMap, packOpts).then(async (buffer: ArrayBuffer) => {
  //文件操作
  let filePath = context.cacheDir + name
  let file = fs.openSync(filePath,fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE)
  fs.writeSync(file.fd,buffer)
  //获取uri,使用uri来通过image展示,因为image组件目前不支持直接用路径
  let urlStr = fileUri.getUriFromPath(filePath)
  console.log("保存成功:" + filePath)
})

3、阿里云OSS上传,这一步不会的可以看下我上一篇文章

鸿蒙APP 上传文件到阿里云OSS

相关推荐
Geek-Chow21 小时前
01 · Why AWS Lambda: Idle Cost, Capacity Planning and Risky In-Place Deploys
云计算
workflower1 天前
超算中心聚焦高性能计算场景(HPC)
大数据·网络·人工智能·机器人·云计算·汽车
ha_lydms1 天前
Datetime Functions(日期时间函数)
大数据·hadoop·阿里云·spark·数据处理·dataworks·maxcompute
Wang's Blog1 天前
Java 项目实战: 交友项目 - 阿里云 OSS 入门案例:从创建 Bucket 到上传拿到可访问 URL
java·阿里云·交友
workflower1 天前
Agentic AI 时代的企业ICT 基础设施运维
运维·人工智能·机器学习·重构·云计算·汽车
ha_lydms1 天前
Bitwise Functions(位运算函数)
大数据·hadoop·阿里云·spark·数据处理·dataworks·maxcompute
Geek-Chow2 天前
00 · AWS Lambda 心智模型学习指南:总览、思维导图与阅读路线(专栏目录)
云计算
ha_lydms2 天前
St Functions(空间函数)
大数据·hadoop·阿里云·spark·数据处理·dataworks·maxcompute
Geek-Chow2 天前
07 · AWS Lambda 自测题与动手实验:用 AWS CLI 验证版本、别名、预置并发与冷启动
云计算
ha_lydms2 天前
Table Functions(表函数)
大数据·hadoop·阿里云·spark·数据处理·dataworks·maxcompute