renderQrMatrix static method
Rastert die QR-Modul-Matrix von data in ein reines schwarz/weiss-Bild
(RGBA, Werte nur 0 oder 255 — kein Anti-Aliasing, keine Graustufen).
Quiet-Zone = 4 Module rundum. Der ganzzahlige Modul-Scale wird aus size
abgeleitet (floor, mind. 2, max. 64), sodass jedes Modul exakt
scale×scale Pixel belegt. Ergebnis ist quadratisch mit Kantenlaenge
(moduleCount + 8) * scale.
Implementation
static RasterImage renderQrMatrix(String data, {int size = 280}) {
final QrCode qr = QrCode.fromData(
data: data,
errorCorrectLevel: QrErrorCorrectLevel.M,
);
final QrImage qi = QrImage(qr);
final int n = qr.moduleCount;
const int quiet = 4; // Module Quiet-Zone rundum
final int full = n + quiet * 2;
final int scale = (size / full).floor().clamp(2, 64);
final int dim = full * scale;
final RasterImage img = RasterImage.filled(dim, dim, 255, 255, 255, 255);
for (int r = 0; r < n; r++) {
for (int c = 0; c < n; c++) {
if (!qi.isDark(r, c)) continue;
final int x0 = (c + quiet) * scale;
final int y0 = (r + quiet) * scale;
for (int dy = 0; dy < scale; dy++) {
int idx = ((y0 + dy) * dim + x0) * 4;
for (int dx = 0; dx < scale; dx++) {
img.rgba[idx] = 0; // R
img.rgba[idx + 1] = 0; // G
img.rgba[idx + 2] = 0; // B
img.rgba[idx + 3] = 255; // A (voll deckend schwarz)
idx += 4;
}
}
}
}
return img;
}