如何在Flutter中裁剪图像? [英] How do I Crop Images in Flutter?

查看:396
本文介绍了如何在Flutter中裁剪图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了几天时间寻找这个问题.

I searching an days for this question.

我想像这样裁剪图像:

GIF来源: https://github.com/ArthurHub/Android-Image-Cropper

与此解决方案最接近的库是图片库,该库提供了操作图片和裁剪,但我想在gif这样的UI级别裁剪图像.我发现的所有库都没有提供该库.

The closest lib for this solution is the Image Lib, that lib offers manipulate images and crop, but i want to crop images in UI level like this gif. All libs I found dont offers that.

推荐答案

没有可为您执行所有操作的小部件.但是,我相信现在可以将本机内容写成扑朔迷离.在这个特定的时刻,我没有时间为您做这件事,但是我绝对可以为您指明正确的方向.

There is no widget that performs all that for you. However, I believe that it is possible to write that natively in flutter now. I don't have time at this particular moment to do it for you, but I can definitely point you in the right direction.

  1. 您将需要以一种可以将其绘制到画布上或使用RawImage绘制它的方式加载图像,而不是直接使用图像"小部件.
  2. 您需要找出相对于图像的坐标系
  3. 您将需要找到一种绘制crop指示器的方法-您可以直接在画布上绘制,也可以使用GestureDetector/Draggable/DropTarget的某种组合来进行绘制.我建议坚持画布"可能是最简单的开始.
  4. 用户选择了图像的一部分后,需要将屏幕坐标转换为图片坐标.
  5. 然后,您必须创建一个屏幕外画布以将裁剪后的图像绘制到该画布上.您必须进行各种变换,以确保图像最终显示在正确的位置.
  6. 完成屏幕外裁剪后,您将不得不显示新图像.
  1. You're going to need to load the image in such a way that you can either draw it onto a canvas or use a RawImage to draw it rather than using the Image widget directly.
  2. You need to figure out a co-ordinate system relative to the image
  3. You'll need to find a way of drawing the crop indicator - you could do this either by drawing directly on the canvas or possibly using some combination of GestureDetector/Draggable/DropTarget. I'd suggest that sticking to Canvas might be the easiest to start.
  4. Once the user has selected a part of the image, you need to translate the screen co-ordinates to picture co-ordinates.
  5. You then have to create an off-screen canvas to draw the cropped image to. There are various transforms you'll have to do to makes sure the image ends up in the right place.
  6. Once you've made the off-screen crop, you'll have to display the new image.

所有这些工作量很大,并且可能需要做很多细化工作.

All of that is quite a lot of work, and probably a lot of finessing to get right.

以下是您需要执行的几个步骤的示例,但您必须弄清楚如何将它们组合在一起.

Here's examples for a couple of the steps you'll need to do, but you'll have to figure out how to put them together.

加载图像:

var byteData = await rootBundle.load("assets/image.jpg");
Uint8List lst = new Uint8List.view(byteData.buffer);
var codec = await UI.instantiateImageCodec(lst);
var nextFrame = await codec.getNextFrame();
var image = frameInfo.image;

在画布上显示图像:

  • https://docs.flutter.io/flutter/dart-ui/Canvas/drawImageRect.html
  • https://docs.flutter.io/flutter/rendering/CustomPainter-class.html

将图像写到屏幕外的画布上:

Writing an image to a off-screen canvas:

ui.Image getCroppedImage(Image image, Rect src, Rect dst) {
  var pictureRecorder = new ui.PictureRecorder();
  Canvas canvas = new Canvas(pictureRecorder);
  canvas.drawImageRect(image, src, dst, Paint());
  return pictureRecorder.endRecording().toImage(dst.width.floor(), dst.height.floor());
}

您可能需要执行此答案获取鼠标/触摸手势的局部坐标.

You'll probably need to do something like this answer for getting the local coordinates of mouse/touch gestures.

一些建议-我将尽可能简单地开始,而不是考虑要开始的性能(即,根据需要绘制每种涂料的所有内容,等等).然后,一旦您掌握了基础知识,就可以开始考虑优化(即对图像使用RawImage,Transform和Stack并仅重新绘制选择器等).

Some advice - I'd start as simple as possible, not thinking about performance to start (i.e. draw everything each paint if needed, etc). Then once you get the basics working you can start thinking of optimization (i.e. using a RawImage, Transform, and Stack for the image and only re-drawing the selector, etc).

如果您需要任何其他帮助,请在评论中告诉我,我会尽力回答.现在,我已经在写一些有关它的内容,这确实让我有点想尝试实现它,因此我可以在某个时候尝试一下,但是由于我现在时间太短,所以可能不会很快.祝你好运= D

If you need any additional help let me know in a comment and I'll do my best to answer. Now that I've been writing about this a bit it does make me slightly curious to try implementing it so I may try at some point, but it probably won't be soon as I'm quite low on time at the moment. Good luck =D

这篇关于如何在Flutter中裁剪图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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