flutter 我想将签名图像(png字节数组)转换为签名控制器点

7ivaypg9  于 2023-02-13  发布在  Flutter
关注(0)|答案(1)|浏览(75)

我正在使用flutter的签名库(https://pub.dev/packages/signature)为用户创建签名板:

signatureController = SignatureController(
  penStrokeWidth: 5,
  penColor: Colors.white,
  onDrawStart: startedDrawing,
);

我可以导出签名控制器到png数组:

final exportSignatureController = SignatureController(
  penStrokeWidth: 2,
  exportBackgroundColor: Colors.black,
  penColor: Colors.white,
  points: signatureController.points,
);

final signature = exportSignatureController.toPngBytes(height: MediaQuery.of(context).size.height.round(), width: MediaQuery.of(context).size.width.round());

此后,签名图像被存储并显示在用户屏幕上:

Image.memory(
   Uint8List.fromList(signature.pngBytes),
   width: MediaQuery.of(context).size.height * 0.1,
   fit: BoxFit.contain,
),

我想创建一个GestureDetector,这样用户就可以点击图像并编辑签名。那么,我如何将png字节转换为signatureController点,以便签名显示在签名板上呢?

x8diyxa7

x8diyxa71#

我想通了:

img.Image image = 
img.decodeImage(widget.previousSignature.pngBytes.toList());
List<Point> whitePixels = [];
for (int x = 0; x < image.width; x++) {
    for (int y = 0; y < image.height; y++) {
      int pixel = image.getPixel(x, y);
      int red   = img.getRed(pixel);
      int green = img.getGreen(pixel);
      int blue  = img.getBlue(pixel);
      int alpha = img.getAlpha(pixel);

      if (red == 255 && green == 255 && blue == 255 && alpha == 255) {
        var offset = Offset(x.toDouble(), y.toDouble());
        whitePixels.add(Point(offset, PointType.tap, 1.0));
      }
    }
  }

然后你只需要将白色像素设置为签名控制器的点

相关问题