0

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 (e.g.: 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.

1 Answers1

1

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

Josep Bové
  • 626
  • 1
  • 8
  • 22
  • Yes, but my problem is to find the pointer to the pixels in order to be able to send it to the C function. How can I get this pointer? – user3751454 Jul 22 '20 at 16:43
  • Right now you have a pixel list, here you have an example of how people work doing the List/Pointer change: https://stackoverflow.com/questions/60888021/dart-c-interoperability-pass-an-array-of-integers-to-c-function – Josep Bové Jul 22 '20 at 16:49