My mission is to take a picture using the camera, send the pixels to C function, and provide a message to the user if the function returns an indication of a problematic image (eg: poor focus, picture too dark, etc).
I want to call a C function and send a pointer to the function which includes the pixels (and other parameters like image width/height). The idea is to use the C code and check the image quality.
The C code is ready. I found samples how to bind it into Flutter. But I do not know how to get the Bitmap data and pixels and send them to the C function.
You can do that binding to native code using dart:ffi.
Imagine that you have a C function that returns the sum of two numbers with this code:
#include <stdint.h>
extern "C" __attribute__((visibility("default"))) __attribute__((used))
int32_t native_add(int32_t x, int32_t y) {
return x + y;
}
And this C Makefile
cmake_minimum_required(VERSION 3.4.1) # for example
add_library( native_add
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
../native_add.cpp )
Now you need to encapsulate this CMake file into the externalNativeBuild inside build.gradle
, this could be an example:
android {
// ...
externalNativeBuild {
// Encapsulates your CMake build configurations.
cmake {
// Provides a relative path to your CMake build script.
path "CMakeLists.txt"
}
}
// ...
}
Now that you have the library and you have encapsulated the code you can load the code using the FFI library like this:
import 'dart:ffi'; // For FFI
import 'dart:io'; // For Platform.isX
final DynamicLibrary nativeAddLib = Platform.isAndroid
? DynamicLibrary.open("libnative_add.so")
: DynamicLibrary.process();
And with a handle to an enclosing library you can resolve the native_add symbol as we do here:
final int Function(int x, int y) nativeAdd =
nativeAddLib
.lookup<NativeFunction<Int32 Function(Int32, Int32)>>("native_add")
.asFunction();
Now you could use it on your application calling the nativeAdd function, here you have an example:
body: Center(
child: Text('1 + 2 == ${nativeAdd(1, 2)}'),
),
You can learn how flutter native code binding works in the following url: flutter docs c-interop
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.