Код: Выделить всё
Future _saveUpdatedImage() async {
try {
/// PNG
///
final recorder = ui.PictureRecorder();
final canvas = Canvas(recorder);
final image = File(widget.imagePath);
final ui.Image decodedImage = await decodeImageFromFile(image);
// Draw the image first:
canvas.drawImage(decodedImage, Offset.zero, Paint());
// Then draw the painter on top:
final painter = DrawingPainter(_points, _lines);
painter.paint(canvas,
Size(decodedImage.width.toDouble(), decodedImage.height.toDouble()));
final picture = recorder.endRecording();
final ui.Image updatedImage =
await picture.toImage(decodedImage.width, decodedImage.height);
final byteData =
await updatedImage.toByteData(format: ui.ImageByteFormat.png);
final pngBytes = byteData!.buffer.asUint8List();
final file = File(widget.imagePath);
await file.writeAsBytes(pngBytes);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Image updated and saved!')),
);
} catch (e) {
print('Error saving image: $e');
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error saving image!')),
);
}
}
Код: Выделить всё
class DrawingPainter extends CustomPainter {
final List points;
final List lines;
DrawingPainter(this.points, this.lines);
@override
void paint(Canvas canvas, Size size) {
final pointPaint = Paint()
..color = Colors.red
..strokeWidth = 5.0;
final linePaint = Paint()
..color = Colors.blue
..strokeWidth = 3.0
..style = PaintingStyle.stroke;
for (final point in points) {
canvas.drawPoints(PointMode.points, [point], pointPaint);
}
for (final line in lines) {
if (line.isNotEmpty) {
for (int i = 0; i < line.length - 1; i++) {
canvas.drawLine(line[i], line[i + 1], linePaint);
}
}
}
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... ustompaint