I am really new to Flutter and C++. So I am struggling with this one. I want to do some transformation to an image through ffi->c++ (opencv). Here's my code on the Flutter side:
class Processvalue extends Struct {
Pointer<Utf8> image;
factory Processvalue.allocate(Pointer image) =>
malloc<Processvalue>().ref
..image = image;
}
I am basically accepting a pointer back from C++ side. Then I call C++ native function like so:
...
final processImage = nativeEdgeDetection
.lookup<NativeFunction<ProcessImageFunction>>("process_image")
.asFunction<ProcessImageNativeFunction>();
...
On the C++ side, in the process_image
function I am using the following:
...
Mat dst = Mat::zeros(maxHeight, maxWidth, CV_8UC1);
Mat transformation_matrix = getPerspectiveTransform(img_pts, dst_pts);
warpPerspective(img, dst, transformation_matrix, dst.size());
uchar * arr = dst.isContinuous()? dst.data: dst.clone().data;
Basically, I am sending image path from Flutter to C++ through ffi package. And in C++ grab the image from this file and create opencv Mat variable dst
, which will hold the new transformed image. Then I convert this Mat variable to uchar * arr
, which I am returning back to Flutter (Pointer to arr). However in Flutter I am not able to convert this back to image data (Uint8List for example).
I have seen people use cv::imwrite(path, dst);
to save transformation directly to the same file. But for me I want to keep the original file and only want to get the new image as data back in Flutter and show it through MemoryImage(...Uint8List variable...)
.
How do I send image data back to Flutter from C++? At the end I would like to have a Uint8List
. Is uchar * arr
even a good option for this?
I have tried the following but it gives me an invalid image data
error:
int imageArrLength = _result.ref.image.length;
^- Pointer back from C++
Uint8List image = _result.ref.image.cast<Uint8>().asTypedList(imageArrLength);