将 Uint8List 传递给 dart:ffi 中的 Pointer<Void>

Thé*_*ion 2 dart

我有一个具有以下签名的 C 函数

bool sendFrame(int width, int height, int channelNumber, void *data, bool clone);
Run Code Online (Sandbox Code Playgroud)

这里的数据是原始图像。

我的 ffi 函数签名是:

bool sendFrame(int width, int height, int channelNumber, void *data, bool clone);
Run Code Online (Sandbox Code Playgroud)

(布尔值必须转换为整数,因为尚不支持布尔类型(对吗?))

我试图从 dart 调用这个方法,并使用Uint8List来自相机流的帧https://pub.dev/documentation/camera/latest/camera/Plane-class.html但我不知道如何转换/将我的分配Uint8Listvoid *

任何想法?

干杯!

Mar*_*nic 5

我不确定您是否仍然遇到这个问题,但我最近找到了一种将相机数据从图像流 api 传递到 c++ 的方法。

\n\n

2020/01/08 更新

\n\n

可以在此处找到一个工作示例https://github.com/martin-labanic/camera_preview_ffi_image_processing。警告:这里有一个问题目前正在调查https://github.com/flutter/flutter/issues/48360

\n\n

初步答复

\n\n

我的设置:\n Flutter(频道测试版,v1.12.13+hotfix.6)\n \xe2\x80\xa2 Flutter 版本 1.12.13+hotfix.6\n \xe2\x80\xa2 框架修订版 18cd7a3601(30 小时前) ), 2019-12-11 06:35:39 -0800\n \xe2\x80\xa2 引擎修订版 2994f7e1e6\n \xe2\x80\xa2 Dart 版本 2.7.0\n ffi 版本 0.1.3 ( https:// pub.dev/packages/ffi )

\n\n

在你的 flutter 代码中准备图像数据:

\n\n
import "dart:ffi" as ffi;\n\n// \'data\' is a Uint8List created by concatenating the planes received from the CameraImage the camera puts out.\n// Based on the code found here https://github.com/renancaraujo/bitmap/blob/master/lib/ffi.dart in the execute function\n// https://groups.google.com/forum/#!searchin/dart-ffi/list%7Csort:date/dart-ffi/V_6g5hpABec/U9we6UyvBAAJ\n  final ffi.Pointer<ffi.Uint8> frameData = allocate<ffi.Uint8>(count: data.length); // Allocate a pointer large enough.\n  final pointerList = frameData.asTypedList(data.length); // Create a list that uses our pointer and copy in the image data.\n  pointerList.setAll(0, data);\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是使用ffi 示例存储库allocation.dart中找到的文件

\n\n

这是我在 flutter 中使用的绑定:

\n\n
final int Function(ffi.Pointer<ffi.Uint8>, int, int, int) cppFunc = dynamicLibrary\n    .lookup<ffi.NativeFunction<ffi.Int32 Function(ffi.Pointer<ffi.Uint8>, ffi.Int32, ffi.Int32, ffi.Int32)>>("cppFunc")\n    .asFunction();\n
Run Code Online (Sandbox Code Playgroud)\n\n

C++ 函数如下所示:

\n\n
extern "C" {\n__attribute__((visibility("default"))) __attribute__((used))\n    int cppFunc(unsigned char *data, int width, int height, int scanline) {\n.\n.\n.\n
Run Code Online (Sandbox Code Playgroud)\n\n

我不知道随着 dart:ffi 向 1.0 发展,这是否会继续有效,但我会尽力使这个答案保持最新。

\n