flutter resizeimage 使用
Flutter 的 resizeImage 可以用来调整图片的尺寸。以下是使用示例:
1. 添加依赖
在 `pubspec.yaml` 中添加依赖:
yaml
dependencies:
  flutter:
    sdk: flutter
  flutter_image:
    git:
      url: git:github/brendan-duncan/image.git
      ref: master
2. 导入依赖包
在需要使用的页面导入依赖包:
dart
import 'package:image/image.dart' as img;
3. 加载图片并调整尺寸
dart
Future<Uint8List> resizeImage(File file) async {
    使用 Flutter 的 Image 包加载图片
    final image = await decodeImageFromList(adAsBytes());
   
    获取原始图片尺寸
    final int width = image.width;
    final int height = image.height;
   
    设置新尺寸
    final int newWidth = 500;
    final int newHeight = (height / width * 500).round();
   
    使用 image 包的 copyResize 方法进行尺寸调整
    final resizedImage = pyResize(image, width: newWidth, height: newHeight);
   
    将调整过后的图片转换为字节流
    final Uint8List resizedBytes = Uint8List.deJpg(resizedImage));
    return resizedBytes;
}
以上示例首先使用 Flutter 的 Image 包加载图片,然后获取图片原始尺寸,并根据需求设置新的尺寸。接下来使用 image 包的 copyResize 方法进行尺寸调整,最后将调整过后的图片转换为字节流,返回给调用者。
flutter sdk