Dart 如何在Flatter中从画布获取PNG图像数据?
我有一个颤振小部件,它接受用户输入并使用自定义画师绘制画布:Dart 如何在Flatter中从画布获取PNG图像数据?,dart,flutter,Dart,Flutter,我有一个颤振小部件,它接受用户输入并使用自定义画师绘制画布: class SPPoint { final Point point; final double size; SPPoint(this.point, this.size); String toString() => "SPPoint $point $size"; } class SignaturePadPainter extends CustomPainter { final List<SPPoint&
class SPPoint {
final Point point;
final double size;
SPPoint(this.point, this.size);
String toString() => "SPPoint $point $size";
}
class SignaturePadPainter extends CustomPainter {
final List<SPPoint> allPoints;
final SignaturePadOptions opts;
Canvas _lastCanvas;
Size _lastSize;
SignaturePadPainter(this.allPoints, this.opts);
ui.Image getPng() {
if (_lastCanvas == null) {
return null;
}
if (_lastSize == null) {
return null;
}
var recorder = new ui.PictureRecorder();
var origin = new Offset(0.0, 0.0);
var paintBounds = new Rect.fromPoints(_lastSize.topLeft(origin), _lastSize.bottomRight(origin));
var canvas = new Canvas(recorder, paintBounds);
paint(canvas, _lastSize);
var picture = recorder.endRecording();
return picture.toImage(_lastSize.width.round(), _lastSize.height.round());
}
paint(Canvas canvas, Size size) {
_lastCanvas = canvas;
_lastSize = size;
for (var point in this.allPoints) {
var paint = new Paint()..color = colorFromColorString(opts.penColor);
paint.strokeWidth = 5.0;
var path = new Path();
var offset = new Offset(point.point.x, point.point.y);
path.moveTo(point.point.x, point.point.y);
var pointSize = point.size;
if (pointSize == null || pointSize.isNaN) {
pointSize = opts.dotSize;
}
canvas.drawCircle(offset, pointSize, paint);
paint.style = PaintingStyle.stroke;
canvas.drawPath(path, paint);
}
}
bool shouldRepaint(SignaturePadPainter oldDelegate) {
return true;
}
}
类SPPoint{
终点;
最终双倍尺寸;
SPPoint(这个点,这个大小);
字符串toString()=>“SPPoint$point$size”;
}
类SignaturePadPaint扩展了CustomPainter{
最后列出所有要点;
最终签字选择;
帆布,帆布;
大小(lastSize);;
签名绘制者(this.allPoints,this.opts);
ui.Image getPng(){
如果(_lastCanvas==null){
返回null;
}
如果(_lastSize==null){
返回null;
}
var recorder=newui.picturererecorder();
var原点=新偏移量(0.0,0.0);
var paintBounds=new Rect.fromPoints(_lastSize.topLeft(原点),_lastSize.bottomRight(原点));
var canvas=新画布(记录器、画框);
油漆(帆布,最新尺寸);
var picture=recorder.endRecording();
返回picture.toImage(_lastSize.width.round(),_lastSize.height.round());
}
油漆(帆布,尺寸){
_lastCanvas=画布;
_lastSize=大小;
for(此.allPoints中的变量点){
var paint=new paint()…color=colorFromColorString(opts.penColor);
paint.strokeWidth=5.0;
var path=新路径();
var偏移=新偏移(点.x点,点.y点);
移动到(点.点.x,点.点.y);
var pointSize=point.size;
if(pointSize==null | | pointSize.isNaN){
pointSize=opts.dotSize;
}
画布.画圈(偏移、点大小、绘制);
paint.style=PaintingStyle.stroke;
画布.绘制路径(路径,绘制);
}
}
bool应重新喷漆(签名油漆工oldDelegate){
返回true;
}
}
当前,当前
getPng()
返回一个dart:ui图像对象,但我不知道如何从图像数据中获取字节(如果这是可能的)以下是我提出的解决方案,现在toByteData()
已添加到SDK中:
var picture = recorder.endRecording();
var image =
picture.toImage(lastSize.width.round(), lastSize.height.round());
ByteData data = await image.toByteData(format: ui.ImageByteFormat.png);
return data.buffer.asUint8List();
此解决方案正在运行,现在已作为签名\u pad\u flatter软件包的一部分发布到pub:这不可能直接实现,但您可以通过本机实现,然后在位图中裁剪不需要的数据。然后将位图另存为png。