Масштабирование изображения с помощью paintImage рисует только список смещения в углу изображения.
В приложении Flutter, над которым я работаю, я пытаюсь сделать это, чтобы вы могли взять изображение и нарисовать его, а затем загрузить. Однако я столкнулся с проблемой, когда нарисованные линии находятся только в верхнем левом углу изображения.
Для этого я использую dart:UI CustomPainter с функцией paintImage. Я не могу найти, где я создаю проблему масштабирования.
Вот моя функция:
final ui.Image image;
onPressed: () async {
var recorder = ui.PictureRecorder();
var imageCanvas = Canvas(recorder);
var painter = Sketcher(lines: lines, image: widget.image, context: context, showImage: false);
paintImage(
canvas: imageCanvas,
rect: Rect.fromLTWH(0, 0, image.width, image.height),
image: image,
fit: BoxFit.scaleDown,
repeat: ImageRepeat.noRepeat,
scale: 1.0,
alignment: Alignment.center,
flipHorizontally: false,
filterQuality: FilterQuality.high
);
painter.paint(imageCanvas, widget.size);
var picture = recorder.endRecording();
ui.Image im = await picture.toImage(widget.size.width.floor(), widget.size.height.floor());
Navigation.of(context).push(MaterialPageRoute(builder: (context) => UploadScreen(image: im)));
}
класс художника:
class DrawnLine {
final List<Offset> path;
final Color color;
final double width;
DrawnLine(this.path, this.color, this.width);
}
class Sketcher extends CustomPainter {
final List<DrawnLine> lines;
final ui.Image image;
final BuildContext context;
final bool showImage;
Sketcher({required this.lines, required this.image, required this.context, required this.showImage,});
@override
void paint(Canvas canvas, Size size) {
Paint paint = Paint()
..color = Colors.redAccent
..strokeCap = StrokeCap.round
..strokeWidth = 5.0;
for (int i = 0; i < lines.length; ++i) {
if (lines[i] == null) continue;
for (int j = 0; j < lines[i].path.length - 1; ++j) {
if (lines[i].path[j] != null && lines[i].path[j + 1] != null) {
paint.color = lines[i].color;
paint.strokeWidth = lines[i].width;
canvas.drawLine(lines[i].path[j], lines[i].path[j + 1], paint);
}
}
}
canvas.save();
canvas.restore();
}
@override
bool shouldRepaint(Sketcher oldDelegate) {
return true;
}
}