You can implement the qrcode.Writer interface to create custom output formats (e.g., specific image formats or network streams).
Logic for rendering
When implementing Write(mat matrix.Matrix), you should iterate through the 2D array of matrix.State. You can determine the color of a pixel/block by checking the state of each cell.
Generally, states that represent 'set' data (like StateTrue, StateInit, StateVersion, StateFormat, or StateFinder) are treated as the foreground color (e.g., BLACK), while StateFalse or ZERO are treated as the background (e.g., WHITE).
| State Expr | value | representation |
|---|
StateFalse | 0 | unset (data and etc) |
ZERO | 0 | same as StateFalse |
StateTrue | 1 | set (data) |
StateInit | 1 | not changed since initialized (temporary state) |
StateVersion | 1 | set (qr version) |
StateFormat | 1 | set (qr format) |
StateFinder | 1 | set (qr finder) |
Implementation Pseudocode
// define your own writer structure to implement `Writer` interface.
object writer {};
writer.Write(matrix.Matrix):
// these should be `IMAGE` stream controller, it receives matrix
object paint;
// set use BLACK, unset use WHITE. Or provides matrix.State to colors mapping
// so that you can control QR Image output intensively.
foreach row in matrix:
foreach column in row:
if column.State in [StateFalse]:
// paint a WHITE square block
paint.draw(x, y, WHITE);
else:
// paint a BLACK square block
paint.draw(x, y, BLACK);
// loop end;
// output paint;