Extracted EpcQrCodeStringFormatter

This commit is contained in:
dankito 2022-03-14 02:21:37 +01:00
parent 510e6bf1ce
commit 66690c4ec3
2 changed files with 94 additions and 62 deletions

View File

@ -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)
}
}

View File

@ -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
}
}