From 66690c4ec300e923ac1acf04f8375b31266d42a6 Mon Sep 17 00:00:00 2001 From: dankito Date: Mon, 14 Mar 2022 02:21:37 +0100 Subject: [PATCH] Extracted EpcQrCodeStringFormatter --- .../codinux/banking/epcqrcode/EpcQrCode.kt | 76 ++++-------------- .../epcqrcode/EpcQrCodeStringFormatter.kt | 80 +++++++++++++++++++ 2 files changed, 94 insertions(+), 62 deletions(-) create mode 100644 EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeStringFormatter.kt diff --git a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt index 3d9cc18..ed79c96 100644 --- a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt @@ -21,6 +21,13 @@ open class EpcQrCode( open val bytes: ByteArray get() = bitmap.extractBytes() + open val height: Int + get() = bitmap.height + + open val width: Int + get() = bitmap.width + + constructor(bitmap: Bitmap32) : this(bitmap, DefaultHeightAndWidth) // convenience constructor for languages that don't support default parameters @@ -35,74 +42,19 @@ open class EpcQrCode( open fun qrCodeAsString(): String { - val string = StringBuilder(bitmap.height * bitmap.width) - - for (y in 1 until bitmap.height) { - for (x in 1 until bitmap.width) { - if (isBitSet(x, y)) { - string.append("██") - } else { - string.append(" ") - } - } - - string.appendLine() - } - - return string.toString() + return EpcQrCodeStringFormatter().asString(this) } open fun qrCodeAsSmallString(): String { - val string = StringBuilder(bitmap.height * bitmap.width) - - for (y in 1 until bitmap.height - 1 step 2) { - for (x in 1 until bitmap.width) { - val currentRowBit = bitmap.getInt(x, y) - val nextRowBit = bitmap.getInt(x, y + 1) - - if (currentRowBit == nextRowBit) { - if (isBitSet(currentRowBit)) { - string.append("█") - } else { - string.append(" ") - } - } else { - if (isBitSet(currentRowBit)) { - string.append("▀") - } else { - string.append("▄") - } - } - } - - string.appendLine() - } - - if (bitmap.height % 2 == 0) { // if last row is odd - val y = bitmap.height - 1 - for (x in 1 until bitmap.width) { - if (isBitSet(x, y)) { - string.append("▀") - } else { - string.append(" ") - } - } - - string.appendLine() - } - - return string.toString() + return EpcQrCodeStringFormatter().asSmallString(this) } - protected open fun isBitSet(x: Int, y: Int): Boolean { - val qrCodeBit = bitmap.getInt(x, y) - return isBitSet(qrCodeBit) - } - - protected open fun isBitSet(qrCodeBitColorCode: Int): Boolean { - // black = -1, white = -16777216 - return qrCodeBitColorCode < -1 + /** + * black = -1, white = -16777216 + */ + open fun getColorCodeAt(x: Int, y: Int): Int { + return bitmap.getInt(x, y) } } \ No newline at end of file diff --git a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeStringFormatter.kt b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeStringFormatter.kt new file mode 100644 index 0000000..0b88e94 --- /dev/null +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeStringFormatter.kt @@ -0,0 +1,80 @@ +package net.codinux.banking.epcqrcode + +open class EpcQrCodeStringFormatter { + + open fun asString(epcQrCode: EpcQrCode): String { + val height = epcQrCode.height + val width = epcQrCode.width + val string = StringBuilder(height * width) + + for (y in 1 until height) { + for (x in 1 until width) { + if (isBitSet(epcQrCode, x, y)) { + string.append("██") + } else { + string.append(" ") + } + } + + string.appendLine() + } + + return string.toString() + } + + open fun asSmallString(epcQrCode: EpcQrCode): String { + val height = epcQrCode.height + val width = epcQrCode.width + val string = StringBuilder(height * width) + + for (y in 1 until height - 1 step 2) { + for (x in 1 until width) { + val currentRowBit = epcQrCode.getColorCodeAt(x, y) + val nextRowBit = epcQrCode.getColorCodeAt(x, y + 1) + + if (currentRowBit == nextRowBit) { + if (isBitSet(currentRowBit)) { + string.append("█") + } else { + string.append(" ") + } + } else { + if (isBitSet(currentRowBit)) { + string.append("▀") + } else { + string.append("▄") + } + } + } + + string.appendLine() + } + + if (height % 2 == 0) { // if last row is odd + val y = height - 1 + for (x in 1 until width) { + if (isBitSet(epcQrCode, x, y)) { + string.append("▀") + } else { + string.append(" ") + } + } + + string.appendLine() + } + + return string.toString() + } + + protected open fun isBitSet(epcQrCode: EpcQrCode, x: Int, y: Int): Boolean { + val qrCodeBit = epcQrCode.getColorCodeAt(x, y) + + return isBitSet(qrCodeBit) + } + + protected open fun isBitSet(qrCodeBitColorCode: Int): Boolean { + // black = -1, white = -16777216 + return qrCodeBitColorCode < -1 + } + +} \ No newline at end of file