flutter_image_compress的使用

1.flutter_image_compress

官方链接见https://pub-web.flutter-io.cn/packages/flutter_image_compress

支持Android/iOS/MacOS/Web

2.pubspec.yaml添加依赖

1
     flutter_image_compress: ^2.2.0

3.示例

image_compress_page.dart

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
import 'dart:io';
import 'dart:typed_data';

import 'package:base_demo/common/file_mgr.dart';
import 'package:flutter/material.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:flutter/services.dart' show rootBundle;

class ImageCompressPage extends StatefulWidget {
  const ImageCompressPage({super.key});

  @override
  State<ImageCompressPage> createState() => _ImageCompressPageState();
}

class _ImageCompressPageState extends State<ImageCompressPage> {
  File? _imageFile;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('flutter_image_compress'),
      ),
      body: SingleChildScrollView(
        child: Column(children: [
          _imageFile != null
              ? Image.file(
            _imageFile!,
            width: 500,
            height: 500,
            fit: BoxFit.contain,
          )
              : Container(),
          ElevatedButton(
              onPressed: () {
                compressImageAndDisplay();
              },
              child: const Text('图片压缩:compressImageAndDisplay')),
          const SizedBox(
            height: 20,
          ),
          ElevatedButton(
              onPressed: () {
                compressImageWithQualityAndDisplay();
              },
              child: const Text('图片压缩:compressImageWithQualityAndDisplay')),
        ]),
      ),
    );
  }

  Future<void> compressImageAndDisplay() async {
    ByteData data = await rootBundle.load('assets/images/large_image.png');
    List<int> compressedImage = await FlutterImageCompress.compressWithList(
      data.buffer.asUint8List(),
      minHeight: 1920,
      minWidth: 1080,
      quality: 80,
    );

    debugPrint(
        'before compressImage data:${data.lengthInBytes}, after compressImage data:${compressedImage.length} ');

    final tempPath = await FileMgr.instance.getDocumentsDirectory();
    final File imgFile = await File('$tempPath/compressed_image.jpg')
        .writeAsBytes(compressedImage);

    setState(() {
      _imageFile = imgFile;
    });
  }

  // 1. compress file and get Uint8List
  Future<void> compressImageWithQualityAndDisplay() async {
    ByteData data = await rootBundle.load('assets/images/large_image.png');
    List<int> compressedImage = await FlutterImageCompress.compressWithList(
      data.buffer.asUint8List(),
      minHeight: 1920,
      minWidth: 1080,
      quality: 80,
      rotate: 90,
    );

    debugPrint(
        'before compressImage data:${data.lengthInBytes}, after compressImage data:${compressedImage.length} ');

    final tempPath = await FileMgr.instance.getDocumentsDirectory();
    final File imgFile = await File('$tempPath/compressed_image1.jpg')
        .writeAsBytes(compressedImage);

    setState(() {
      _imageFile = imgFile;
    });
  }

  Future<Widget> _compressImageToWidget() async {
    ByteData data = await rootBundle.load('assets/images/large_image.png');

    ImageProvider provider = MemoryImage(data.buffer.asUint8List());
    return Image(image: provider);
  }
}

4.效果

image

image