HarmonyOS 鸿蒙Next图片ArrayBuffer字节流转16进制很耗时

HarmonyOS 鸿蒙Next图片ArrayBuffer字节流转16进制很耗时 【设备信息】Mate60
【API版本】Api12
【DevEco Studio版本】5.0.5.300
【问题描述】
图片ArrayBuffer字节流 转16进制很耗时

2 回复

Uint8Array转十六进制,采取的优化方案应该放在图片处理上

  1. 对大图进行压缩处理推荐

    demo:https://gitee.com/harmonyos-cases/cases/blob/master/CommonAppDevelopment/feature/imagecompression/README.md

  2. 对耗时操作放在taskpool 里面进行处理

    文档地址:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V14/cpu-intensive-task-development-V14

经过测试耗时不是很高呀 可以尝试使用buff来转

let buff_start = new Date().getTime()
console.log("buff-start:" + buff_start.toString())
let buf = buffer.from(arrayBuffer);
console.info(`The content of file: ${buf.toString("hex")}`);
let buff_end = new Date().getTime()
console.log("buff-end:" + buff_end.toString())
console.log("buff-time:" + `${buff_end - buff_start}`)
let unit8_start = new Date().getTime()
console.log("unit8-start:" + unit8_start.toString())
let arry = new Uint8Array(arrayBuffer);
console.log(buffer.from(arry).toString('hex'))
let unit8_end = new Date().getTime()
console.log("unit8-end:" + unit8_end.toString())
console.log("unit8-time:" + `${unit8_end - unit8_start}`)

更多关于HarmonyOS 鸿蒙Next图片ArrayBuffer字节流转16进制很耗时的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,将图片的ArrayBuffer字节流转换为16进制字符串可能会耗时较长,主要原因包括:

  1. 数据量较大:图片文件通常包含大量像素数据,字节流较长,转换过程需要逐字节处理。

  2. 转换算法复杂度:每个字节需要转换为两个16进制字符,涉及多次计算和字符串拼接。

  3. 系统资源限制:在资源受限的设备上,处理大文件可能导致性能瓶颈。

  4. 缺乏原生支持:鸿蒙系统可能没有针对此类操作进行特别优化。

解决方法包括使用更高效的算法或工具库进行转换,或减少处理的数据量。

回到顶部