简体   繁体   English

将 Dart 中的 ByteData 转换为 C++ 中的 unsigned char* 的有效方法?

[英]An efficient way to convert ByteData in Dart to unsigned char* in C++?

I'm doing it in this way:我是这样做的:

Pointer<Uint8> _byteDataToPointer(ByteData byteData) {
  final uint8List = byteData.buffer.asUint8List();
  final length = uint8List.lengthInBytes;
  final result = allocate<Uint8>(count: length);

  for (var i = 0; i < length; ++i) {
    result[i] = uint8List[i];
  }

  return result;
}

Is there a more efficient way like JNIEnv::GetByteArrayRegion in JNI?在 JNI 中有没有像JNIEnv::GetByteArrayRegion这样更有效的方法?

Thank you very much!非常感谢!

This is always going to involve a copy, so is there something more efficient than your for loop?这总是会涉及一个副本,所以有什么比你的 for 循环更有效的吗? Maybe.也许。

After allocating your Uint8Pointer use asTypedList to convert it to a Uint8List .分配Uint8Pointer使用asTypedList将其转换为Uint8List Now you have access to its copy primitive like setAll and setRange to copy from the source to the destination.现在您可以访问其复制原语,如setAllsetRange以从源复制到目标。

What's the original source of the ByteData ? ByteData的原始来源是ByteData If you knew the size in advance, could you allocate the Uint8Pointer first, convert to typed data and then read directly into that?如果你事先知道大小,你能不能先分配Uint8Pointer,转换成类型数据,然后直接读进去?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM