【问题标题】:Flip a Ui.Image in Dart在 Dart 中翻转 Ui.Image
【发布时间】:2021-12-20 06:02:00
【问题描述】:

我正在构建一个使用画布呈现图像的应用。为此,我需要使用 Ui.Image 对象加载它们。当我加载图像时,是否有水平翻转图像?也许通过反转字节数组或其他方式?

这是我加载图片的函数:

static Future<ui.Image> loadImage(String imagePath) async {
    ByteData bd = await rootBundle.load(imagePath);

    final Uint8List bytes = Uint8List.view(bd.buffer);

    final ui.Codec codec = await ui.instantiateImageCodec(bytes);

    final ui.Image image = (await codec.getNextFrame()).image;

    return image;
  }

我听说有一种方法可以使用画布水平翻转图像,但这是一项昂贵的操作,重绘画布的效率对我的项目很重要。有什么方法可以在加载时翻转图像吗?

【问题讨论】:

    标签: image flutter dart canvas flip


    【解决方案1】:

    请试试这个

    Transform(
    alignment: Alignment.center,
    transform: Matrix4.rotationY(math.pi),
    child: Image.asset('image path.png'),
    )
    

    【讨论】:

    • 我正在尝试在画布上渲染图像,所以很遗憾我无法使用其他小部件
    • 你能把你的代码贴在这里吗?
    • @JohanDonkersloot 如果你不喜欢玩Matrix4 api 你也可以直接用canvas.scale(-1, 1)缩放画布
    • @pskink 感谢您的回复,但由于画布操作可能很昂贵,我真的希望在字节级别翻转图像。
    【解决方案2】:

    你可以使用paintImage()函数来绘制你的图像,该函数可以在flutter画布中使用,这样可以更容易地做任何图像。

    paintImage(
        canvas: canvas,
        rect: _rect,
        image: _img,
        flipHorizontally: true,
    );
    

    其中参数_rect描述了图像将被绘制的位置和大小。

    【讨论】:

      猜你喜欢
      • 2020-08-09
      • 1970-01-01
      • 2018-09-11
      • 1970-01-01
      • 2014-01-19
      • 1970-01-01
      • 2019-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多