Using OpenCV natively in C++ in a Flutter app with Dart FFI. Tested with Flutter 2.5.2.
Read the full articles:
- Mobile platorms: https://medium.com/flutter-community/integrating-c-library-in-a-flutter-app-using-dart-ffi-38a15e16bc14
- Desktop platorms: https://medium.com/flutter-community/integrating-c-library-in-a-desktop-flutter-app-using-dart-ffi-32560cb1169b
Run init.sh
script from a scripts
folder or do the following steps manually:
- Download OpenCV for Android and iOS: https://opencv.org/releases/
- Copy or create symlinks:
opencv2.framework
tonative_opencv/ios
OpenCV-android-sdk/sdk/native/jni/include
tonative_opencv
- Contents of
OpenCV-android-sdk/sdk/native/libs/**
tonative_opencv/android/src/main/jniLibs/**
Run init_windows.ps1
PowerShell script from a scripts
folder or do the following steps manually:
- Download OpenCV for Windows: https://opencv.org/releases/
- Unpack it. Set env. variable
OpenCV_DIR
to unpacked...\opencv\build
folder - Create a hard link from
native_opencv\ios\Classes\native_opencv.cpp
tonative_opencv_windows\windows\native_opencv.cpp
- Make sure
native_opencv_windows\windows\CMakeLists.txt
contains correct .dll names (OpenCV_DEBUG_DLL_NAME,OpenCV_RELEASE_DLL_NAME)
Before doing anything else, you need to download OpenCV source code and
build a framework by running opencv/platforms/apple/build_xcframework.py
script.
Run init_macos.sh
script from a scripts
folder or do the following steps manually:
- Create a hard link from
native_opencv/ios/Classes/native_opencv.cpp
tonative_opencv_macos/macos/Classes/native_opencv.cpp
- Copy
opencv2.xcframework
tonative_opencv/macos
Before doing anything else, you need to download OpenCV source code and build libraries by following the official tutorial.
After making a build, set up an environmental variable OpenCV_DIR
to the build's folder.
Run init_linux.sh
script from a scripts
folder or do the following steps manually:
- Create a hard link from
native_opencv/ios/Classes/native_opencv.cpp
tonative_opencv_linux/linux/native_opencv.cpp
/snap/flutter/current/usr/include/x86_64-linux-gnu/gnu/stubs.h:7:11: fatal error: 'gnu/stubs-32.h' file not found
# include <gnu/stubs-32.h>
^~~~~~~~~~~~~~~~
1 error generated.
ninja: build stopped: subcommand failed.
or
/snap/flutter/current/usr/bin/ld: warning: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/crt1.o: unsupported GNU_PROPERTY_TYPE (5) type: 0xc0008002
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Exception: Build process failed
Install flutter manually instead of from snap, see: https://flutter.dev/docs/get-started/install/linux#install-flutter-manually