compressImageBytes static method

Future<Uint8List> compressImageBytes(
  1. Uint8List bytes, {
  2. int quality = 80,
})

压缩图片数据

Implementation

static Future<Uint8List> compressImageBytes(Uint8List bytes,
    {int quality = 80}) async {
  final codec = await ui.instantiateImageCodec(bytes);
  final frameInfo = await codec.getNextFrame();
  final width = frameInfo.image.width;
  final height = frameInfo.image.height;
  final pictureRecorder = ui.PictureRecorder();
  final canvas = Canvas(pictureRecorder);
  final paint = Paint()..filterQuality = FilterQuality.high;
  canvas.drawImage(frameInfo.image, Offset.zero, paint);
  final picture = pictureRecorder.endRecording();
  final image = await picture.toImage(width, height);
  final data = await image.toByteData(format: ui.ImageByteFormat.png);
  return data?.buffer.asUint8List() ?? Uint8List(0);
}