Bucket filling in flutter

2019-04-26 05:18发布

问题:

I am working on drawing app, which needs bucket filling also. Any idea on how to perform bucket filling in Flutter?

回答1:

You would have to write your own algorithm. I think you could port this one to dart.

One fundamental you need is how to get color of a pixel of an image:

Color getPixelColor(ByteData rgbaImageData, int imageWidth, int imageHeight, int x, int y) {
  assert(x >= 0 && x < imageWidth);
  assert(y >= 0 && y < imageHeight);

  final byteOffset = x * 4 + y * imageWidth * 4;

  final r = rgbaImageData.getUint8(byteOffset);
  final g = rgbaImageData.getUint8(byteOffset + 1);
  final b = rgbaImageData.getUint8(byteOffset + 2);
  final a = rgbaImageData.getUint8(byteOffset + 3);

  return Color.fromARGB(a, r, g, b);
}

You can use it like this:

Image image = ...;

final rgbaImageData = await image.toByteData(format: ui.ImageByteFormat.rawRgba);

print(getPixelColor(rgbaImageData, image.width, image.height, x, y));

Manipulating it follows the same scheme (setUint8).



标签: dart flutter