FFI_PLUGIN_EXPORT float **createDoubleArrayFromVectorOfDoubleArrays(void *vector) {
std::vector<std::array<std::array<float, 160>, 160>> *vectorOfDoubleArrays = (std::vector<std::array<std::array<float, 160>, 160>> *) vector;
float **array = new float *[vectorOfDoubleArrays->size()];
for (int i = 0; i < vectorOfDoubleArrays->size(); i++) {
array[i] = new float[160 * 160];
for (int j = 0; j < 160; j++) {
for (int k = 0; k < 160; k++) {
array[i][j * 160 + k] = (*vectorOfDoubleArrays)[i][j][k];
}
}
}
return array;
}
在省道侧(在生成绑定之后):
List<List<Float64List>> generateRandomArrayOfDoubleArrays(int count){
Pointer<Void> nativeVector = _bindings.createRandomVectorOfDoubleArrays(count); // create a vector of double arrays in native code and return a pointer to it (casted to void*)
final Pointer<Pointer<Float>> nativeGeneratedArray = _bindings.createDoubleArrayFromVectorOfDoubleArrays(nativeVector); // generate a list of list of double arrays from nativeVector, each double array is a Float64List of length 160
// now we convert them to dart lists by iterating and copying the data
final List<List<Float64List>> generatedArray = [];
for (int i = 0; i < count; i++) {
final List<Float64List> generatedArrayRow = [];
for (int j = 0; j < 160; j++) {
final Pointer<Float> nativeGeneratedArrayRow = nativeGeneratedArray.elementAt(i).value.elementAt(j);
final Float64List generatedArrayRowElement = Float64List.fromList(nativeGeneratedArrayRow.asTypedList(160));
generatedArrayRow.add(generatedArrayRowElement);
}
generatedArray.add(generatedArrayRow);
}
return generatedArray;
}
import 'dart:async';
import 'dart:ffi';
import 'dart:io';
import 'dart:isolate';
import 'dart:math';
import 'dart:typed_data';
import 'sample_ffi_plugin_bindings_generated.dart';
const String _libName = 'sample_ffi_plugin';
/// The dynamic library in which the symbols for [SampleFfiPluginBindings] can be found.
final DynamicLibrary _dylib = () {
if (Platform.isMacOS || Platform.isIOS) {
return DynamicLibrary.open('$_libName.framework/$_libName');
}
if (Platform.isAndroid || Platform.isLinux) {
return DynamicLibrary.open('lib$_libName.so');
}
if (Platform.isWindows) {
return DynamicLibrary.open('$_libName.dll');
}
throw UnsupportedError('Unknown platform: ${Platform.operatingSystem}');
}();
/// The bindings to the native functions in [_dylib].
final SampleFfiPluginBindings _bindings = SampleFfiPluginBindings(_dylib);
extension Float64ListExtension on Float64List {
/// Creates a `double` array from this list by copying the list's
/// data, and returns a pointer to it.
///
/// `nullptr` is returned if the list is empty.
Pointer<Double> toDoubleArrayPointer({required Allocator allocator}) {
if (isEmpty) {
return nullptr;
}
final Pointer<Double> ptr = allocator(sizeOf<Double>() * length);
for (int i = 0; i < length; i++) {
ptr[i] = this[i];
}
return ptr;
}
}
List<List<Float64List>> generateRandomArrayOfDoubleArrays(int count){
Pointer<Void> nativeVector = _bindings.createRandomVectorOfDoubleArrays(count); // create a vector of double arrays in native code and return a pointer to it (casted to void*)
final Pointer<Pointer<Float>> nativeGeneratedArray = _bindings.createDoubleArrayFromVectorOfDoubleArrays(nativeVector); // generate a list of list of double arrays from nativeVector, each double array is a Float64List of length 160
// now we convert them to dart lists by iterating and copying the data
final List<List<Float64List>> generatedArray = [];
for (int i = 0; i < count; i++) {
final List<Float64List> generatedArrayRow = [];
for (int j = 0; j < 160; j++) {
final Pointer<Float> nativeGeneratedArrayRow = nativeGeneratedArray.elementAt(i).value.elementAt(j);
final Float64List generatedArrayRowElement = Float64List.fromList(nativeGeneratedArrayRow.asTypedList(160));
generatedArrayRow.add(generatedArrayRowElement);
}
generatedArray.add(generatedArrayRow);
}
return generatedArray;
}
1条答案
按热度按时间wztqucjr1#
溶液:
您可以使用此代码作为开始,但不要忘记释放分配的内存(甚至根据您的特定情况对其进行更多优化,因为它可能不会被优化,它只是为了演示):
结果:
示例应用:完整的工作示例步骤:
flutter create --template=plugin_ffi --platforms=android,ios,windows,macos,linux sample_ffi_plugin
创建dart ffi项目src/
中的sample_ffi_plugin.c
重命名为sample_ffi_plugin.cpp
(在CMakeLists.txt
中也重命名sample_ffi_plugin.cpp
的内容替换为:sample_ffi_plugin.h
的内容替换为:/lib/
中的sample_ffi_plugin.dart
中的代码替换为:/example
并将main.dart
替换为: