Implemented drawing a border around QR code so that scanner apps can better detect QR code

This commit is contained in:
dankito 2022-03-14 03:02:11 +01:00
parent ab64fbdd2d
commit 583477354e
3 changed files with 46 additions and 9 deletions

View File

@ -41,12 +41,12 @@ open class EpcQrCode(
}
open fun qrCodeAsString(invertColors: Boolean = false): String {
return EpcQrCodeStringFormatter().asString(this, invertColors)
open fun qrCodeAsString(border: Int = 0, invertColors: Boolean = false): String {
return EpcQrCodeStringFormatter().asString(this, border, invertColors)
}
open fun qrCodeAsSmallString(invertColors: Boolean = false): String {
return EpcQrCodeStringFormatter().asSmallString(this, invertColors)
open fun qrCodeAsSmallString(border: Int = 0, invertColors: Boolean = false): String {
return EpcQrCodeStringFormatter().asSmallString(this, border, invertColors)
}

View File

@ -14,14 +14,18 @@ open class EpcQrCodeStringFormatter {
}
open fun asString(epcQrCode: EpcQrCode, invertColors: Boolean = false): String {
open fun asString(epcQrCode: EpcQrCode, border: Int = 0, invertColors: Boolean = false): String {
val bitIsSetColor = if (invertColors) BitIsNotSetColor + BitIsNotSetColor else BitIsSetColor + BitIsSetColor
val bitIsNotSetColor = if (invertColors) BitIsSetColor + BitIsSetColor else BitIsNotSetColor + BitIsNotSetColor
val height = epcQrCode.height
val width = epcQrCode.width
val string = StringBuilder(height * width)
printBorderAboveOrBelow(border, width, string, bitIsNotSetColor)
for (y in 1 until height) {
printBorderBeforeOrAfter(border, string, bitIsNotSetColor)
for (x in 1 until width) {
if (isBitSet(epcQrCode, x, y)) {
string.append(bitIsSetColor)
@ -30,22 +34,30 @@ open class EpcQrCodeStringFormatter {
}
}
printBorderBeforeOrAfter(border, string, bitIsNotSetColor)
string.appendLine()
}
printBorderAboveOrBelow(border, width, string, bitIsNotSetColor)
return string.toString()
}
open fun asSmallString(epcQrCode: EpcQrCode, invertColors: Boolean = false): String {
val bitIsSetColor = if (invertColors) BitIsNotSetColor else BitIsSetColor
open fun asSmallString(epcQrCode: EpcQrCode, border: Int = 0, invertColors: Boolean = false): String {
val bitIsSetColor = if (invertColors) BitIsNotSetColor else BitIsSetColor
val bitIsNotSetColor = if (invertColors) BitIsSetColor else BitIsNotSetColor
val upperBitSetColor = if (invertColors) SmallSizeLowerBitSetColor else SmallSizeUpperBitSetColor
val lowerBitSetColor = if (invertColors) SmallSizeUpperBitSetColor else SmallSizeLowerBitSetColor
val height = epcQrCode.height
val width = epcQrCode.width
val string = StringBuilder(height * width)
val string = StringBuilder((height + border) * (width + border))
printBorderAboveOrBelow(border, width, string, bitIsNotSetColor)
for (y in 1 until height - 1 step 2) {
printBorderBeforeOrAfter(border, string, bitIsNotSetColor)
for (x in 1 until width) {
val currentRowBit = epcQrCode.getColorCodeAt(x, y)
val nextRowBit = epcQrCode.getColorCodeAt(x, y + 1)
@ -65,10 +77,14 @@ open class EpcQrCodeStringFormatter {
}
}
printBorderBeforeOrAfter(border, string, bitIsNotSetColor)
string.appendLine()
}
if (height % 2 == 0) { // if last row is odd
printBorderBeforeOrAfter(border, string, bitIsNotSetColor)
val y = height - 1
for (x in 1 until width) {
if (isBitSet(epcQrCode, x, y)) {
@ -78,9 +94,13 @@ open class EpcQrCodeStringFormatter {
}
}
printBorderBeforeOrAfter(border, string, bitIsNotSetColor)
string.appendLine()
}
printBorderAboveOrBelow(border, width, string, bitIsNotSetColor)
return string.toString()
}
@ -95,4 +115,21 @@ open class EpcQrCodeStringFormatter {
return qrCodeBitColorCode < -1
}
private fun printBorderAboveOrBelow(border: Int, width: Int, string: StringBuilder, bitIsNotSetColor: String) {
for (y in 0 until border) {
for (x in 1 until width + 2 * (border * 2)) {
string.append(bitIsNotSetColor)
}
string.appendLine()
}
}
private fun printBorderBeforeOrAfter(border: Int, string: StringBuilder, bitIsNotSetColor: String) {
for (x in 0 until (border * 2)) {
string.append(bitIsNotSetColor)
}
}
}

View File

@ -6,7 +6,7 @@ class NativeApp {
fun generateAndShowEpcQrCode(config: EpcQrCodeConfig) {
val epcQrCode = EpcQrCodeGenerator().generateEpcQrCode(config, 40)
println(epcQrCode.qrCodeAsSmallString())
println(epcQrCode.qrCodeAsSmallString(4, true))
}
}