Browse Source

create smaller QR codes with ToSmallString

Use the characters ▄ and ▀ for two pixel rows per character row.
Paul Seyfert 7 years ago
parent
commit
864062d029
1 changed files with 39 additions and 0 deletions
  1. 39 0
      qrcode.go

+ 39 - 0
qrcode.go

@@ -552,3 +552,42 @@ func (q *QRCode) ToString(inverseColor bool) string {
 	}
 	return buf.String()
 }
+
+// ToSmallString produces a multi-line string that forms a QR-code image, a
+// factor two smaller in x and y then ToString.
+func (q *QRCode) ToSmallString(inverseColor bool) string {
+	bits := q.Bitmap()
+	var buf bytes.Buffer
+	// if there is an odd number of rows, the last one needs special treatment
+	for y := 0; y < len(bits)-1; y += 2 {
+		for x := range bits[y] {
+			if bits[y][x] == bits[y+1][x] {
+				if bits[y][x] != inverseColor {
+					buf.WriteString(" ")
+				} else {
+					buf.WriteString("█")
+				}
+			} else {
+				if bits[y][x] != inverseColor {
+					buf.WriteString("▄")
+				} else {
+					buf.WriteString("▀")
+				}
+			}
+		}
+		buf.WriteString("\n")
+	}
+	// special treatment for the last row if odd
+	if len(bits)%2 == 1 {
+		y := len(bits) - 1
+		for x := range bits[y] {
+			if bits[y][x] != inverseColor {
+				buf.WriteString(" ")
+			} else {
+				buf.WriteString("▀")
+			}
+		}
+		buf.WriteString("\n")
+	}
+	return buf.String()
+}