getImageData method

Future<ByteData?> getImageData()

Captures the current canvas as an image and returns it as ByteData.

Implementation

Future<ByteData?> getImageData() async {
  try {
    final RenderRepaintBoundary boundary =
        painterKey.currentContext!.findRenderObject()!
            as RenderRepaintBoundary;
    final ui.Image originalImage = await boundary.toImage(pixelRatio: 3.0);

    final ui.PictureRecorder recorder = ui.PictureRecorder();
    final Canvas canvas = Canvas(recorder);
    final Paint paint = Paint();
    canvas.drawImage(originalImage, Offset.zero, paint);

    final ui.Image finalImage = await recorder.endRecording().toImage(
      originalImage.width,
      originalImage.height,
    );
    return await finalImage.toByteData(format: ui.ImageByteFormat.png);
  } catch (e) {
    debugPrint('Error capturing or flipping image: $e');
    return null;
  }
}