formatImage static method

Uint8List formatImage(
  1. CameraImage cameraImage
)

Implementation

static Uint8List formatImage(CameraImage cameraImage) {
  late imglib.Image img;
  if (Platform.isAndroid) {
    final p = calloc.allocate(cameraImage.planes[0].bytes.length) as Pointer<Uint8>;
    final p1 = calloc.allocate(cameraImage.planes[1].bytes.length) as Pointer<Uint8>;
    final p2 = calloc.allocate(cameraImage.planes[2].bytes.length) as Pointer<Uint8>;
    final pointerList = p.asTypedList(cameraImage.planes[0].bytes.length);
    final pointerList1 = p1.asTypedList(cameraImage.planes[1].bytes.length);
    final pointerList2 = p2.asTypedList(cameraImage.planes[2].bytes.length);
    pointerList.setRange(0, cameraImage.planes[0].bytes.length, cameraImage.planes[0].bytes);
    pointerList1.setRange(0, cameraImage.planes[1].bytes.length, cameraImage.planes[1].bytes);
    pointerList2.setRange(0, cameraImage.planes[2].bytes.length, cameraImage.planes[2].bytes);
    final imgP = conv(p, p1, p2, cameraImage.planes[1].bytesPerRow, cameraImage.planes[1].bytesPerPixel!, cameraImage.planes[0].bytesPerRow, cameraImage.height);
    var imgData = imgP.asTypedList((cameraImage.planes[0].bytesPerRow * cameraImage.height));
    img = imglib.Image.fromBytes(cameraImage.height, cameraImage.planes[0].bytesPerRow, imgData);
    calloc.free(p);
    calloc.free(p1);
    calloc.free(p2);
    calloc.free(imgP);
  } else if (Platform.isIOS) {
    img = imglib.Image.fromBytes(
      cameraImage.planes[0].bytesPerRow,
      cameraImage.height,
      cameraImage.planes[0].bytes,
      format: imglib.Format.bgra,
    );
  }
  return Uint8List.fromList(imglib.encodePng(img));
}