paint method
Called whenever the object needs to paint. The given Canvas has its
coordinate space configured such that the origin is at the top left of the
box. The area of the box is the size of the size argument.
Paint operations should remain inside the given area. Graphical operations outside the bounds may be silently ignored, clipped, or not clipped. It may sometimes be difficult to guarantee that a certain operation is inside the bounds (e.g., drawing a rectangle whose size is determined by user inputs). In that case, consider calling Canvas.clipRect at the beginning of paint so everything that follows will be guaranteed to only draw within the clipped area.
Implementations should be wary of correctly pairing any calls to Canvas.save/Canvas.saveLayer and Canvas.restore, otherwise all subsequent painting on this canvas may be affected, with potentially hilarious but confusing results.
To paint text on a Canvas, use a TextPainter.
To paint an image on a Canvas:
-
Obtain an ImageStream, for example by calling ImageProvider.resolve on an AssetImage or NetworkImage object.
-
Whenever the ImageStream's underlying ImageInfo object changes (see ImageStream.addListener), create a new instance of your custom paint delegate, giving it the new ImageInfo object.
-
In your delegate's paint method, call the Canvas.drawImage, Canvas.drawImageRect, or Canvas.drawImageNine methods to paint the ImageInfo.image object, applying the ImageInfo.scale value to obtain the correct rendering size.
Implementation
@override
void paint(Canvas canvas, Size size) {
if (faces.isEmpty) return;
final boxPaint = Paint()
..style = PaintingStyle.stroke
..strokeWidth = 3.0
..color = const Color(0xFF00FFCC);
final landmarkPaint = Paint()
..style = PaintingStyle.fill
..color = const Color(0xFF89CFF0);
final meshPaint = Paint()
..style = PaintingStyle.fill
..color = const Color(0xFFF4C2C2);
final irisFill = Paint()
..style = PaintingStyle.fill
..color = const Color(0xFF22AAFF).withAlpha(153)
..blendMode = BlendMode.srcOver;
final irisStroke = Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1.5
..color = const Color(0xFF22AAFF).withAlpha(230);
final double displayWidth = size.width;
final double displayHeight = size.height;
final t = CoverFitTransform.cover(
sourceWidth: imageSize.width,
sourceHeight: imageSize.height,
viewWidth: displayWidth,
viewHeight: displayHeight,
mirror: mirrorHorizontally,
);
for (final face in faces) {
final boundingBox = face.boundingBox;
final p1 = t.map(boundingBox.topLeft.x, boundingBox.topLeft.y);
final p2 = t.map(boundingBox.bottomRight.x, boundingBox.bottomRight.y);
final rect = Rect.fromLTRB(
math.min(p1.dx, p2.dx),
math.min(p1.dy, p2.dy),
math.max(p1.dx, p2.dx),
math.max(p1.dy, p2.dy),
);
canvas.drawRect(rect, boxPaint);
if (showPoseAndScores || showClassification) {
drawFaceInfoLabel(
canvas,
size,
face,
rect,
showClassification: showClassification,
);
}
for (final landmark in face.landmarks.values) {
final transformed = t.map(landmark.x, landmark.y);
canvas.drawCircle(transformed, 4.0, landmarkPaint);
}
if (detectionMode == FaceDetectionMode.standard ||
detectionMode == FaceDetectionMode.full) {
final FaceMesh? faceMesh = face.mesh;
if (faceMesh != null) {
final mesh = faceMesh.points;
final double imgArea = displayWidth * displayHeight;
final double radius = 1.25 + math.sqrt(imgArea) / 1000.0;
for (final p in mesh) {
final transformed = t.map(p.x, p.y);
canvas.drawCircle(transformed, radius, meshPaint);
}
}
}
if (detectionMode == FaceDetectionMode.full) {
final eyePair = face.eyes;
if (eyePair != null) {
for (final iris in [eyePair.leftEye, eyePair.rightEye]) {
if (iris == null) continue;
final oval = boundsOf(
[
iris.irisCenter,
...iris.irisContour,
].map((p) => t.map(p.x, p.y)),
);
canvas.drawOval(oval, irisFill);
canvas.drawOval(oval, irisStroke);
if (iris.mesh.isNotEmpty) {
final Paint eyeOutlinePaint = Paint()
..color = const Color(0xFF22AAFF)
..style = PaintingStyle.stroke
..strokeWidth = 1.5;
final eyelidContour = iris.contour;
for (final connection in eyeLandmarkConnections) {
if (connection[0] < eyelidContour.length &&
connection[1] < eyelidContour.length) {
final p1 = eyelidContour[connection[0]];
final p2 = eyelidContour[connection[1]];
final t1 = t.map(p1.x, p1.y);
final t2 = t.map(p2.x, p2.y);
canvas.drawLine(t1, t2, eyeOutlinePaint);
}
}
final Paint eyeMeshPointPaint = Paint()
..color = const Color(0xFFFFAA22)
..style = PaintingStyle.fill;
for (final p in iris.mesh) {
final transformed = t.map(p.x, p.y);
canvas.drawCircle(transformed, 0.8, eyeMeshPointPaint);
}
}
}
}
}
}
}