imencode function

(bool, Uint8List) imencode(
  1. String ext,
  2. InputArray img, {
  3. VecI32? params,
})

IMEncode encodes an image Mat into a memory buffer. This function compresses the image and stores it in the returned memory buffer, using the image format passed in in the form of a file extension string.

For further details, please see: http://docs.opencv.org/master/d4/da8/group__imgcodecs.html#ga461f9ac09887e47797a54567df3b8b63

Implementation

(bool, Uint8List) imencode(
  String ext,
  InputArray img, {
  VecI32? params,
}) {
  final buffer = calloc<cimgcodecs.VecUChar>();
  final pSuccess = calloc<ffi.Bool>();
  final cExt = ext.toNativeUtf8().cast<ffi.Char>();

  params == null
      ? cvRun(() => cimgcodecs.Image_IMEncode(cExt, img.ref, pSuccess, buffer))
      : cvRun(() => cimgcodecs.Image_IMEncode_WithParams(cExt, img.ref, params.ref, pSuccess, buffer));
  final success = pSuccess.value;
  calloc.free(cExt);
  calloc.free(pSuccess);

  final vec = VecUChar.fromPointer(buffer);
  final u8List = vec.toU8List(); // will copy data
  vec.dispose();
  return (success, u8List);
}