【问题标题】:Crop and save an image in Flutter without ui在没有 ui 的情况下在 Flutter 中裁剪和保存图像
【发布时间】:2021-12-11 21:02:18
【问题描述】:

我想制作一个可以以特定纵横比(设备比例)裁剪图像的应用。 但是,我不希望任何 ui 显示裁剪选项。

好的,这是一个例子,

如果用户从应用程序的图像列表中点击图像,则所选图像会自动(后台处理)裁剪(设备比例)并保存在设备上,而不显示任何裁剪相关的用户界面。我怎样才能做到这一点! dart 中的任何函数都可以裁剪图像而不显示任何内容。

【问题讨论】:

    标签: image flutter crop


    【解决方案1】:

    您可以将 copyCrop() 与 image 一起使用:

    Image copyCrop(Image src, int x, int y, int w, int h);

    import 'dart:io';
    import 'package:image/image.dart';
    void main(List<String> argv) {
      String path = argv[0];
      Directory dir = Directory(path);
      List files = dir.listSync();
      List<int> trimRect;
      for (var f in files) {
        if (f is! File) {
          continue;
        }
        List<int> bytes = f.readBytesSync();
        Image image = decodeImage(bytes);
        if (image == null) {
          continue;
        }
        if (trimRect == null) {
          trimRect = findTrim(image, mode: TrimMode.transparent);
        }
        Image trimmed = copyCrop(image, trimRect[0], trimRect[1], 
                                 trimRect[2], trimRect[3]);
        String name = f.path.split(RegExp(r'(/|\\)')).last;
        File('$path/trimmed-$name').writeBytesSync(encodeNamedImage(image, f.path));
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-13
      相关资源
      最近更新 更多