如何在 Flutter/Dart 中读取(从磁盘)和调整图像大小 [英] How can I read (from disk) and resize an image, in Flutter/Dart

查看:44
本文介绍了如何在 Flutter/Dart 中读取(从磁盘)和调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Flutter/Dart 中,我如何执行以下 3 个步骤:

In Flutter/Dart, how can I perform the following 3 steps:

  1. 从磁盘读取图像,
  2. 读取其原始尺寸(宽度和高度),
  3. 调整大小.

注意:我必须能够使用常规 Flutter Image 小部件显示最终结果.

Note: I must be able to display the final result with a regular Flutter Image widget.

澄清:我不想保存图像,但我确实想在内存中实际调整它的大小.

CLARIFICATION: I don't want to save the image, but I do want to actually resize it in memory.

推荐答案

您可以使用 image.file 构造函数.

You can read image from the disk using the image.file constructor.

有关更多功能,您可以使用 图像库

For more features you can use the Image library

一个提供加载、保存和操作能力的 Dart 库各种不同文件格式的图像.

A Dart library providing the ability to load, save and manipulate images in a variety of different file formats.

来自文档的示例 examples

加载 jpeg,调整大小并将其另存为 png

Load a jpeg, resize it and save it as a png

    import 'dart:io' as Io;
    import 'package:image/image.dart';
    void main() {
      // Read a jpeg image from file.
      Image image = decodeImage(new Io.File('test.jpg').readAsBytesSync());

      // Resize the image to a 120x? thumbnail (maintaining the aspect ratio).
      Image thumbnail = copyResize(image, width: 120);

      // Save the thumbnail as a PNG.
      new Io.File('out/thumbnail-test.png')
            ..writeAsBytesSync(encodePng(thumbnail));
    }

这篇关于如何在 Flutter/Dart 中读取(从磁盘)和调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆