绘制函数CustomPaint和PictureRecorder以不同的大小工作



我想水印一个图像,绘制功能是不同的自定义油漆,它在PictureRecorder中给出了不同的尺寸。随着照片尺寸的增大,画线会变小。我怎么修理它?

CustomPaint(
painter: painter.ImagePainter(
image: snapshot.data,
text: text,
style: TextStyle(
fontSize: fontSize,
color:
Color.fromRGBO(255, 255, 255, fontOpacity),
),
logo: _logo),
willChange: true,
)

png:

ui.PictureRecorder recorder = ui.PictureRecorder();
Canvas canvas = Canvas(recorder);
ui.Image image = await _retrieveImage();
Watermark.draw(
canvas,
image,
text,
TextStyle(
fontSize: fontSize,
color: Color.fromRGBO(255, 255, 255, fontOpacity)),
Size(image.width.roundToDouble(), image.height.roundToDouble()),
_logo);
ui.Image pic =
await recorder.endRecording().toImage(image.width, image.height);
ByteData data = await pic.toByteData(format: ui.ImageByteFormat.png);

我修复了这个函数:

Matrix4 sizeToRect(Size src, Rect dst,
{BoxFit fit = BoxFit.contain, Alignment alignment = Alignment.center}) {
FittedSizes fs = applyBoxFit(fit, src, dst.size);
double scaleX = fs.destination.width / fs.source.width;
double scaleY = fs.destination.height / fs.source.height;
Size fittedSrc = Size(src.width * scaleX, src.height * scaleY);
Rect out = alignment.inscribe(fittedSrc, dst);
return Matrix4.identity()
..translate(out.left, out.top)
..scale(scaleX, scaleY);
} 

我也删除了画布。翻译功能

感谢@pslink

最新更新