HarmonyOS鸿蒙Next中base64编码

HarmonyOS鸿蒙Next中base64编码

import { buffer, util } from '@kit.ArkTS';

@Entry
@Component
struct Base64Encode {
  @State textValue: string = '';
  @State base64Str: string = '';
  @State base64Str2: string = '';

  build() {
    Row() {
      Column() {
        TextInput({ text: $$this.textValue, placeholder: '请输入要编码的字符串' })
        Button("base64解码")
          .fontSize(25)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            let base64 = new util.Base64Helper();
            // 字符串转成字节流
            let arr = new Uint8Array(buffer.from(this.textValue, 'utf-8').buffer);
            this.base64Str = base64.encodeToStringSync(arr); // Uint8Array转base64
          })
        Text(`编码内容:${this.base64Str}`).fontSize(25)
          .fontWeight(FontWeight.Bold)

        //解码
        Button("base64解码")
          .fontSize(25)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            let base64 = new util.Base64Helper();
            // base64转Uint8Array
            let array = base64.decodeSync(this.base64Str);

            let textDecoderOptions: util.TextDecoderOptions = {
              fatal: false,
              ignoreBOM: true
            }
            let decodeToStringOptions: util.DecodeToStringOptions = {
              stream: false
            }
            let textDecoder = util.TextDecoder.create('utf-8', textDecoderOptions);
            this.base64Str2 = textDecoder.decodeToString(array, decodeToStringOptions);
          })

        Text(`解码内容:${this.base64Str2}`).fontSize(25)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
}

Base64格式图片保存

https://developer.huawei.com/consumer/cn/doc/architecture-guides/base64_image_save-0000002271203733

// 格式转换
uint8Array: Uint8Array = await base64.decode(ImageBase64Str);
imageBuffer: ArrayBuffer = uint8Array.buffer.slice(0, uint8Array.byteLength);
// 完成图片文件创建
helper = photoAccessHelper.getPhotoAccessHelper(context);
uri = await helper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'png');
file = await fs.open(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
// 进行数据写入
fs.write(file.fd, imageBuffer);

更多关于HarmonyOS鸿蒙Next中base64编码的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

HarmonyOS Next中base64编码通过系统提供的工具类实现。可使用util模块的Base64类进行编解码操作,支持标准base64及URL安全格式。编码调用encodeToString()方法,解码使用decodeToString()。数据以Uint8Array类型处理,确保二进制数据兼容性。具体API参考ArkTS开发文档的密码学基础章节。

更多关于HarmonyOS鸿蒙Next中base64编码的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next中,Base64编码和解码可以通过@kit.ArkTS模块中的util.Base64Helper类实现。你的代码示例已经正确展示了编码和解码的基本流程:

  • 编码过程:使用buffer.from将字符串转换为Uint8Array,然后调用base64.encodeToStringSync生成Base64字符串。
  • 解码过程:使用base64.decodeSync将Base64字符串转换回Uint8Array,再通过TextDecoder转换为原始字符串。

对于Base64格式的图片保存,代码片段展示了如何将Base64字符串解码为Uint8Array,转换为ArrayBuffer,并通过文件系统API保存为图片文件。具体步骤包括:

  1. 使用base64.decode异步解码Base64字符串。
  2. 通过slice获取有效的ArrayBuffer数据。
  3. 使用photoAccessHelper创建图片文件并写入数据。

这些方法覆盖了常见的Base64处理场景,代码结构清晰且符合HarmonyOS Next的API规范。

回到顶部