normalizeBuffer method

Future<NormalizedImage?> normalizeBuffer(
  1. Uint8List bytes,
  2. int width,
  3. int height,
  4. int stride,
  5. int format,
  6. dynamic points,
)

Normalize documents. bytes - bytes of the image. width - width of the image. height - height of the image. stride - stride of the image. format - format of the image. points - points of the document. Returns a NormalizedImage.

Implementation

Future<NormalizedImage?> normalizeBuffer(Uint8List bytes, int width,
    int height, int stride, int format, dynamic points) async {
  List<dynamic> jsOffsets = points.map((Offset offset) {
    return {'x': offset.dx, 'y': offset.dy};
  }).toList();

  dynamic jsonObj = jsonEncode({
    "quad": {"points": jsOffsets}
  });

  String pixelFormat = 'rgba';
  if (format == ImagePixelFormat.IPF_GRAYSCALED.index) {
    pixelFormat = 'grey';
  } else if (format == ImagePixelFormat.IPF_RGB_888.index) {
    pixelFormat = 'rgb';
  } else if (format == ImagePixelFormat.IPF_BGR_888.index) {
    pixelFormat = 'bgr';
  } else if (format == ImagePixelFormat.IPF_ARGB_8888.index) {
    pixelFormat = 'rgba';
  } else if (format == ImagePixelFormat.IPF_ABGR_8888.index) {
    pixelFormat = 'bgra';
  }

  NormalizedImage? image;
  if (_normalizer != null) {
    _normalizedDocument = await handleThenable(_normalizer!.normalize(
        parse(jsonEncode({
          'data': bytes,
          'width': width,
          'height': height,
          'pixelFormat': pixelFormat,
        })),
        parse(jsonObj)));

    if (_normalizedDocument != null) {
      Image result = _normalizedDocument!.image;
      dynamic data = result.data;
      Uint8List bytes = Uint8List.fromList(data);
      image = NormalizedImage(bytes, result.width, result.height);
      return image;
    }
  }

  return image;
}