HarmonyOS 如何把C++侧string类型的图片在TS中转换成图片格式

C++侧把图片数据通过字节流转换成string传给TS侧,TS侧如何将这个string转换成pixelMap?

class DHData {
  public:
    explicit DHData(size_t capacity);
  ~DHData();
  size_t Size();
  size_t Offset();
  size_t Capacity();
  uint8_t *Data();
  int32_t SetRange(size_t offset, size_t size);
};
shared_ptr<DHData> data;
std::string controllData(data->Data(), data->Data()+data->Size());

C++侧代码如上,最终是把图片数据转换成了controllData这个string传给TS侧。

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

代码整理如下:

import { util } from '@kit.ArkTS';
import fs from '@ohos.file.fs';
import { BusinessError } from '@kit.BasicServicesKit';

const base64String =
  'iVBORw0KGgoAAAANSUhEUgAAAB4AAAAtCAMAAACOACuTAAAALVBMVEVMaXE2QU03QE02QU48PEs0Q0s2QU42Qk43QE43QU03QU04P043QE43QVA3QU7fFG+6AAAADnRSTlMAu3fuESKPVd1mzESqM0jcJEgAAACOSURBVHjatZBJEsAgCARV3BP5/3Oj3mdIZZlrF9CMs+Jx2sSKE/7FIEMmzYimRTuisS8aYRWTykA060xDtKipVelhLYiGRU+6WhJdHai1QK+6X6ZeFXYt9rBEOhzosCaqnfHPtO2TfpV0JXOxgXB/v5t7q0PZ1CN66I2+D3460q/lodkgpdjibeNm/fVtLk4YEK1O5uSkAAAAAElFTkSuQmCC';

@Entry
@Component
struct Index {
  @State imagestr: string = ''

  base64Image() {
    let begin = 'data:image/jpeg;base64,'
    return begin + base64String
  }

  aboutToAppear(): void {
    this.imagestr = this.base64Image()
  }

  build() {
    Row() {
      Column() {
        Image(this.imagestr).width(300).height(100).backgroundColor(Color.Green)
        Button("保存图片").onClick(() => {
          this.picture()
        })
      }.width('100%')
    }.height('100%')
  }

  picture() {
    let that = new util.Base64Helper();
    let result: Uint8Array = that.decodeSync(base64String, util.Type.MIME);
    let buf: ArrayBuffer = result.buffer as ArrayBuffer
    const context: Context = getContext(this);
    const path1: string = context.cacheDir + "/pixel_map1169.jpg";
    let file = fs.openSync(path1, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
    fs.write(file.fd, buf).then(async (writeLen) => {
      console.info("write data to file succeed and size is:" + writeLen);
      fs.closeSync(file);
    }).catch((err: BusinessError) => {
      console.info("write data to file failed with error message: " + err.message + ", error code: " + err.code);
    });
  }
} 
分享
微博
QQ
微信
回复
3天前
相关问题
HarmonyOS 图片转换成ImageBitmap
19浏览 • 1回复 待解决
Map类型怎么转换成Json string
2221浏览 • 1回复 待解决
如何base64转换成图片
10342浏览 • 2回复 待解决
鸿蒙明确支持图片格式有哪些?
6122浏览 • 1回复 待解决
HarmonyOS 如何将汉字转换成拼音
31浏览 • 1回复 待解决
是否支持avif ,优化后图片格式需要
1201浏览 • 1回复 待解决
c++可以直接调用tsstatic方法吗?
2175浏览 • 1回复 待解决
HarmonyOS Object怎么转换成map
57浏览 • 1回复 待解决
HarmonyOS json转换成对象
43浏览 • 2回复 待解决
PixelMap类型怎么转换成Base64字符串
656浏览 • 1回复 待解决
如何将文件转换成字符串
437浏览 • 1回复 待解决