convertUiImageToBytes function
Implementation
Future<Uint8List?> convertUiImageToBytes(
ui.Image image, {
ImageCompressionConfig? compression,
}) async {
final ImageCompressionConfig effectiveConfig =
compression ?? const ImageCompressionConfig();
final double scale = effectiveConfig.scale ?? 1.0;
ui.Image targetImage = image;
ui.Image? resizedImage;
if (effectiveConfig.enabled && scale > 0 && scale < 1.0) {
final int targetWidth = math.max(1, (image.width * scale).round());
final int targetHeight = math.max(1, (image.height * scale).round());
if (targetWidth != image.width || targetHeight != image.height) {
resizedImage = await _resizeImage(image, targetWidth, targetHeight);
targetImage = resizedImage;
}
}
final ByteData? byteData =
await targetImage.toByteData(format: effectiveConfig.format);
if (resizedImage != null) {
_disposeImage(resizedImage);
}
if (byteData == null) {
return null;
}
return byteData.buffer.asUint8List();
}