Implemented converting QR code to resulting image format

This commit is contained in:
dankito 2023-04-10 23:51:56 +02:00
parent 14edcc6aae
commit b0f118cfad
5 changed files with 53 additions and 13 deletions

View File

@ -4,7 +4,8 @@ import com.soywiz.korim.bitmap.Bitmap32
open class EpcQrCode( open class EpcQrCode(
bitmap: Bitmap32, open val bitmap: Bitmap32,
open val bytes: ByteArray,
heightAndWidth: Int heightAndWidth: Int
) { ) {
@ -15,12 +16,6 @@ open class EpcQrCode(
} }
open var bitmap: Bitmap32 = bitmap
protected set
open val bytes: ByteArray
get() = bitmap.extractBytes()
open val height: Int = heightAndWidth open val height: Int = heightAndWidth
open val width: Int = heightAndWidth open val width: Int = heightAndWidth

View File

@ -17,7 +17,9 @@ open class EpcQrCodeConfig(
open val qrCodeHeightAndWidth: Int = EpcQrCode.DefaultHeightAndWidth, open val qrCodeHeightAndWidth: Int = EpcQrCode.DefaultHeightAndWidth,
open val encoding: EpcQrCodeCharacterSet = EpcQrCodeCharacterSet.UTF_8 open val encoding: EpcQrCodeCharacterSet = EpcQrCodeCharacterSet.UTF_8,
open val imageFormat: ImageFormat = ImageFormat.PNG
) { ) {

View File

@ -1,5 +1,9 @@
package net.codinux.banking.epcqrcode package net.codinux.banking.epcqrcode
import com.soywiz.korim.bitmap.Bitmap32
import com.soywiz.korim.format.BMP
import com.soywiz.korim.format.PNG
import com.soywiz.korim.format.TGA
import com.soywiz.korim.qr.QR import com.soywiz.korim.qr.QR
import com.soywiz.korim.qr.QRErrorCorrectLevel import com.soywiz.korim.qr.QRErrorCorrectLevel
@ -16,7 +20,19 @@ open class EpcQrCodeGenerator {
val qrCodeBitmap = qrCode.msg(epcQrCodeContent) val qrCodeBitmap = qrCode.msg(epcQrCodeContent)
.scaled(heightAndWidth, heightAndWidth, true) .scaled(heightAndWidth, heightAndWidth, true)
return EpcQrCode(qrCodeBitmap, heightAndWidth) val qrCodeBytes = convertToFormat(qrCodeBitmap, config.imageFormat)
return EpcQrCode(qrCodeBitmap, qrCodeBytes, heightAndWidth)
}
private fun convertToFormat(qrCodeBitmap: Bitmap32, format: ImageFormat): ByteArray {
val imageFormat = when (format) {
ImageFormat.PNG -> PNG
ImageFormat.TGA -> TGA
ImageFormat.BMP -> BMP
}
return imageFormat.encode(qrCodeBitmap)
} }
open fun generate(param: EpcQrCodeConfig): EpcQrCodeValues { open fun generate(param: EpcQrCodeConfig): EpcQrCodeValues {

View File

@ -3,10 +3,12 @@ package net.codinux.banking.epcqrcode
enum class ImageFormat { enum class ImageFormat {
// BMP, // .bmp is not supported on Android // TODO: implement support for JPEG and SVG
JPEG, PNG,
PNG BMP, // .bmp is not supported on Android
TGA
} }

View File

@ -2,6 +2,7 @@ package net.codinux.banking.epcqrcode
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertTrue
class EpcQrCodeGeneratorTest { class EpcQrCodeGeneratorTest {
@ -14,7 +15,7 @@ class EpcQrCodeGeneratorTest {
@Test @Test
fun basicDataOnly() { fun basicDataOnly() {
val config = EpcQrCodeConfig(ReceiverName, ReceiverIban) val config = basicDataConfig()
val result = underTest.generateAsString(config) val result = underTest.generateAsString(config)
@ -32,4 +33,28 @@ class EpcQrCodeGeneratorTest {
""".trimIndent()) """.trimIndent())
} }
@Test
fun png() {
val result = underTest.generateEpcQrCode(basicDataConfig(ImageFormat.PNG))
// don't know why, in JavaScript it's 1000643, on all other platforms 10592 bytes
assertTrue(listOf(10592, 1000643).contains(result.bytes.size))
}
@Test
fun bmp() {
val result = underTest.generateEpcQrCode(basicDataConfig(ImageFormat.BMP))
assertEquals(1000054, result.bytes.size)
}
@Test
fun tga() {
val result = underTest.generateEpcQrCode(basicDataConfig(ImageFormat.TGA))
assertEquals(1000018, result.bytes.size)
}
private fun basicDataConfig(imageFormat: ImageFormat = ImageFormat.PNG) = EpcQrCodeConfig(ReceiverName, ReceiverIban, imageFormat = imageFormat)
} }