dart ffi_使用dart ffi将c库集成到flutter应用程序中

本文介绍了如何使用Dart的Foreign Function Interface (FFI)将C库整合到Flutter应用中,通过Dart FFI实现与C代码的交互。
摘要由CSDN通过智能技术生成

dart ffi

What would you do if you wanted your Flutter app to be as much performant as possible, or there was no package in pub that you need? Probably, you would use native Java/Objective-C libraries and communicate with them using Platform Channels. But what if both platforms share the same business logic? Surely, nobody wants to write the same code twice in two different languages.

如果您希望Flutter应用程序具有尽可能高的性能,或者在酒吧中没有需要的软件包,该怎么办? 可能您将使用本机Java / Objective-C库,并使用平台通道与它们进行通信。 但是,如果两个平台共享相同的业务逻辑怎么办? 当然,没有人愿意用两种不同的语言编写两次相同的代码。

The purpose of this article is to help Flutter developers set up their project to use a native C or C++ library and write a single code base that uses it. As an example, the article describes the process of adding the OpenCV library to a Flutter app project.

本文的目的是帮助Flutter开发人员设置其项目以使用本机C或C ++库,并编写使用该库的单个代码库。 例如,本文介绍了将OpenCV库添加到Flutter应用程序项目的过程。

这是什么一回事呢? (What is it all about?)

Dart Foreign Function Interface (FFI) allows us to call native C/C++ functions on both iOS and Android platforms without using Platform Channels or making Java/Objective-C bindings. That leads to having a single code base written in C with zero platform-specific code.

Dart外部功能接口(FFI)允许我们在iOS和Android平台上调用本机C / C ++函数,而无需使用平台通道或进行Java / Objective-C绑定。 这就导致了用C编写的单一代码库以及零个特定于平台的代码。

OpenCV is a computer vision library that contains a lot of services used for image processing. It is mainly written in C++.

OpenCV是一个计算机视觉库,其中包含许多用于图像处理的服务。 它主要是用C ++编写的。

创建一个插件 (Creating a plugin)

Even though it’s not required, it’s better to create a Flutter plugin to separate all task-specific stuff from the main application. Run flutter create — template=plugin native_opencv command to create it.Next, we need to update the app’s dependency list in pubspec.yaml with a newly created plugin and ffi package:

即使不是必需的,也最好创建Flutter插件以将所有特定于任务的内容与主应用程序分开。 运行flutter create — template=plugin native_opencv命令创建它。接下来,我们需要使用新创建的插件和ffi包更新pubspec.yaml中应用程序的依赖项列表:

dependencies:
native_opencv:
path: native_opencv
ffi: ^0.1.3

ffi package comes in handy for working with C UTF-8/UTF-16 strings.

ffi软件包可用于处理C UTF-8 / UTF-16字符串。

在iOS上设置插件 (Setting up the plugin on iOS)

OpenCV is distributed as a framework and it has to be included in the plugin along with C++ library. OpenCV also requires AVFoundation framework. Place the framework in plugin’s ios folder (or use a symbo

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值