【问题标题】:Check image size( kb, mb...) in flutter?在颤动中检查图像大小(kb,mb ...)?
【发布时间】:2021-02-10 17:06:13
【问题描述】:

我知道如何检查图像的宽度和高度:

import 'dart:io';

File image = new File('image.png'); // Or any other way to get a File instance.
var decodedImage = await decodeImageFromList(image.readAsBytesSync());
print(decodedImage.width);
print(decodedImage.height)

但我想检查图像大小,如 100kb、200kb 或类似的有什么办法,请帮助我。

【问题讨论】:

    标签: image flutter dart


    【解决方案1】:

    使用lengthInBytes

    final bytes = image.readAsBytesSync().lengthInBytes;
    final kb = bytes / 1024;
    final mb = kb / 1024;
    

    如果你想async-await,请使用

    final bytes = (await image.readAsBytes()).lengthInBytes;
    

    【讨论】:

      【解决方案2】:
      var file = File('the_path_to_the_image.jpg');
      print(file.lengthSync()); 
      

      【讨论】:

        【解决方案3】:

        这是一个使用函数的解决方案,该函数将为您提供文件大小作为一个整洁的格式化字符串。

        进口:

        import 'dart:io';
        import 'dart:math';
        

        输出:

        File image = new File('image.png');
        
        print(getFilesizeString(bytes: image.lengthSync()}); // Output: 17kb, 30mb, 7gb
        

        功能:

        // Format File Size
        static String getFileSizeString({@required int bytes, int decimals = 0}) {
          const suffixes = ["b", "kb", "mb", "gb", "tb"];
          var i = (log(bytes) / log(1024)).floor();
          return ((bytes / pow(1024, i)).toStringAsFixed(decimals)) + suffixes[i];
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-20
          • 2011-07-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多