preface

The opencv API can be called in a flutter method channel, so there is a long chain of calls to flutter -> Java jni -> c++.

If we want to pass a parameter, it will be copied in the Java and C++ heap, which is prone to memory jitter and inefficient.

This article will describe how to implement gaussian blur effect by using FFI to call the opencv c++ API with the flutter android project using the opencv SDK.

Release tip: FFI has been released as a stable version after Dart 2.12.0 (included with Flutter 2.0 and later versions).

I. Introduction to FFI

Flutter Mobile can use the DART: FFI library to call the native C API. FFI stands for external functional interface. Other terms for similar functionality include local interfaces and language bindings.

Before you can bind native code in a library or program using FFI libraries, you must first ensure that the native code is loaded and its symbols are visible to Dart.

Second, environment building

2.1 download the SDK

Open Cv download address

The decompressed package contains the following contents:

2.2 Importing Files

Create a flutter plugin named opencv_plugin and create a CPP directory under the main directory.

  1. Create the native-lib. CPP file in the CPP directory.

  2. Copy the OpenCv include file to the CPP directory.

    Indlue files in SDK -> native -> jni -> include

  3. Create a jniLibs file and copy the LIBS to jniLibs

    Indlue files in SDK -> Native -> Libs

Directory after copying:

2.3 configuration CmakeLists. TXT

Create cmakelists. TXT under Android directory as follows:

Cmake_minimum_required (VERSION 3.4.1 track) include_directories (${CMAKE_SOURCE_DIR} / SRC/main/CPP/include) add_library(libopencv_java4 SHARED IMPORTED) set_target_properties(libopencv_java4 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/libs/${ANDROID_ABI}/libopencv_java4.so) add_library( # Sets the name of the library. native-lib # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). src/main/cpp/native-lib.cpp ) find_library( # Sets the name of the path variable. log-lib # Specifies the name of the NDK library that # you want CMake to locate. log ) target_link_libraries( # Specifies the target library. native-lib libopencv_java4 # Links the target library to the log library android # included in the NDK. ${log-lib} )Copy the code

Cmakelists. TXT file location:

Note: Include_directories and set_target_properties must be configured with ${CMAKE_SOURCE_DIR} as an absolute path, otherwise.so will not be found during compilation.

2.4 Configuring Gradle compilation

Add some content to the Gradle file under Opencv_plugin

  • The introduction ofc++_shared.so
  • configurationCmakeLists.txtThe path
android { compileSdkVersion 30 sourceSets { main.java.srcDirs += 'src/main/kotlin' } defaultConfig { minSdkVersion 16 ### externalNativeBuild {cmake {cppFlags "" arguments "-dandroid_stl =c++_shared"}} ### ExternalNativeBuild {cmake {path file(' cmakelists.txt ')}}Copy the code

2.5 Checking whether the configuration is successful

Call dynamiclibrary.open (“libnative-lib.so”) on flutter to see if the log returns an error.

final DynamicLibrary _opencvLib =
    Platform.isAndroid ? DynamicLibrary.open("libnative-lib.so") : DynamicLibrary.process();
Copy the code

3 OpenCv Combat – Gaussian blur

The article links

reference

  • Call native code with DART: FFI
  • FFI