compressImageBytes method

  1. @override
Future<ImageBytesResult> compressImageBytes(
  1. Uint8List source,
  2. ImageCompressConfig config
)
override

Compress bytes in memory. Images only — a video would mean copying tens of megabytes across the platform channel in one message.

Implementation

@override
Future<ImageBytesResult> compressImageBytes(
  Uint8List source,
  ImageCompressConfig config,
) async {
  await _ensureImageLoaded();
  try {
    final cfg = config.toMap().jsify()! as JSObject;
    final res = await _jsCompressImageBytes(source.toJS, cfg).toDart;
    return ImageBytesResult(
      bytes: res.bytes.toDart,
      originalSizeBytes: res.originalSizeBytes,
      width: res.width,
      height: res.height,
      format: res.format,
      skipped: res.skipped,
    );
  } catch (e) {
    throw ImageCompressException(
        CompressErrorCode.imageCompressFailed, e.toString());
  }
}