Implemented drawing a border around QR code so that scanner apps can better detect QR code
This commit is contained in:
parent
ab64fbdd2d
commit
583477354e
|
@ -41,12 +41,12 @@ open class EpcQrCode(
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
open fun qrCodeAsString(invertColors: Boolean = false): String {
|
open fun qrCodeAsString(border: Int = 0, invertColors: Boolean = false): String {
|
||||||
return EpcQrCodeStringFormatter().asString(this, invertColors)
|
return EpcQrCodeStringFormatter().asString(this, border, invertColors)
|
||||||
}
|
}
|
||||||
|
|
||||||
open fun qrCodeAsSmallString(invertColors: Boolean = false): String {
|
open fun qrCodeAsSmallString(border: Int = 0, invertColors: Boolean = false): String {
|
||||||
return EpcQrCodeStringFormatter().asSmallString(this, invertColors)
|
return EpcQrCodeStringFormatter().asSmallString(this, border, invertColors)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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 bitIsSetColor = if (invertColors) BitIsNotSetColor + BitIsNotSetColor else BitIsSetColor + BitIsSetColor
|
||||||
val bitIsNotSetColor = if (invertColors) BitIsSetColor + BitIsSetColor else BitIsNotSetColor + BitIsNotSetColor
|
val bitIsNotSetColor = if (invertColors) BitIsSetColor + BitIsSetColor else BitIsNotSetColor + BitIsNotSetColor
|
||||||
val height = epcQrCode.height
|
val height = epcQrCode.height
|
||||||
val width = epcQrCode.width
|
val width = epcQrCode.width
|
||||||
val string = StringBuilder(height * width)
|
val string = StringBuilder(height * width)
|
||||||
|
|
||||||
|
printBorderAboveOrBelow(border, width, string, bitIsNotSetColor)
|
||||||
|
|
||||||
for (y in 1 until height) {
|
for (y in 1 until height) {
|
||||||
|
printBorderBeforeOrAfter(border, string, bitIsNotSetColor)
|
||||||
|
|
||||||
for (x in 1 until width) {
|
for (x in 1 until width) {
|
||||||
if (isBitSet(epcQrCode, x, y)) {
|
if (isBitSet(epcQrCode, x, y)) {
|
||||||
string.append(bitIsSetColor)
|
string.append(bitIsSetColor)
|
||||||
|
@ -30,22 +34,30 @@ open class EpcQrCodeStringFormatter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printBorderBeforeOrAfter(border, string, bitIsNotSetColor)
|
||||||
|
|
||||||
string.appendLine()
|
string.appendLine()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printBorderAboveOrBelow(border, width, string, bitIsNotSetColor)
|
||||||
|
|
||||||
return string.toString()
|
return string.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
open fun asSmallString(epcQrCode: EpcQrCode, invertColors: Boolean = false): String {
|
open fun asSmallString(epcQrCode: EpcQrCode, border: Int = 0, invertColors: Boolean = false): String {
|
||||||
val bitIsSetColor = if (invertColors) BitIsNotSetColor else BitIsSetColor
|
val bitIsSetColor = if (invertColors) BitIsNotSetColor else BitIsSetColor
|
||||||
val bitIsNotSetColor = if (invertColors) BitIsSetColor else BitIsNotSetColor
|
val bitIsNotSetColor = if (invertColors) BitIsSetColor else BitIsNotSetColor
|
||||||
val upperBitSetColor = if (invertColors) SmallSizeLowerBitSetColor else SmallSizeUpperBitSetColor
|
val upperBitSetColor = if (invertColors) SmallSizeLowerBitSetColor else SmallSizeUpperBitSetColor
|
||||||
val lowerBitSetColor = if (invertColors) SmallSizeUpperBitSetColor else SmallSizeLowerBitSetColor
|
val lowerBitSetColor = if (invertColors) SmallSizeUpperBitSetColor else SmallSizeLowerBitSetColor
|
||||||
val height = epcQrCode.height
|
val height = epcQrCode.height
|
||||||
val width = epcQrCode.width
|
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) {
|
for (y in 1 until height - 1 step 2) {
|
||||||
|
printBorderBeforeOrAfter(border, string, bitIsNotSetColor)
|
||||||
|
|
||||||
for (x in 1 until width) {
|
for (x in 1 until width) {
|
||||||
val currentRowBit = epcQrCode.getColorCodeAt(x, y)
|
val currentRowBit = epcQrCode.getColorCodeAt(x, y)
|
||||||
val nextRowBit = epcQrCode.getColorCodeAt(x, y + 1)
|
val nextRowBit = epcQrCode.getColorCodeAt(x, y + 1)
|
||||||
|
@ -65,10 +77,14 @@ open class EpcQrCodeStringFormatter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printBorderBeforeOrAfter(border, string, bitIsNotSetColor)
|
||||||
|
|
||||||
string.appendLine()
|
string.appendLine()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (height % 2 == 0) { // if last row is odd
|
if (height % 2 == 0) { // if last row is odd
|
||||||
|
printBorderBeforeOrAfter(border, string, bitIsNotSetColor)
|
||||||
|
|
||||||
val y = height - 1
|
val y = height - 1
|
||||||
for (x in 1 until width) {
|
for (x in 1 until width) {
|
||||||
if (isBitSet(epcQrCode, x, y)) {
|
if (isBitSet(epcQrCode, x, y)) {
|
||||||
|
@ -78,9 +94,13 @@ open class EpcQrCodeStringFormatter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printBorderBeforeOrAfter(border, string, bitIsNotSetColor)
|
||||||
|
|
||||||
string.appendLine()
|
string.appendLine()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printBorderAboveOrBelow(border, width, string, bitIsNotSetColor)
|
||||||
|
|
||||||
return string.toString()
|
return string.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,4 +115,21 @@ open class EpcQrCodeStringFormatter {
|
||||||
return qrCodeBitColorCode < -1
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -6,7 +6,7 @@ class NativeApp {
|
||||||
fun generateAndShowEpcQrCode(config: EpcQrCodeConfig) {
|
fun generateAndShowEpcQrCode(config: EpcQrCodeConfig) {
|
||||||
val epcQrCode = EpcQrCodeGenerator().generateEpcQrCode(config, 40)
|
val epcQrCode = EpcQrCodeGenerator().generateEpcQrCode(config, 40)
|
||||||
|
|
||||||
println(epcQrCode.qrCodeAsSmallString())
|
println(epcQrCode.qrCodeAsSmallString(4, true))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue