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 69830a7..d8963ec 100644 --- a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt @@ -4,7 +4,8 @@ import com.soywiz.korim.bitmap.Bitmap32 open class EpcQrCode( - bitmap: Bitmap32, + open val bitmap: Bitmap32, + open val bytes: ByteArray, 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 width: Int = heightAndWidth diff --git a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeConfig.kt b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeConfig.kt index bebbecd..bb2609e 100644 --- a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeConfig.kt +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeConfig.kt @@ -17,7 +17,9 @@ open class EpcQrCodeConfig( 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 ) { diff --git a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeGenerator.kt b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeGenerator.kt index f8e9424..ed6cd0c 100644 --- a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeGenerator.kt +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeGenerator.kt @@ -1,5 +1,9 @@ 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.QRErrorCorrectLevel @@ -16,7 +20,19 @@ open class EpcQrCodeGenerator { val qrCodeBitmap = qrCode.msg(epcQrCodeContent) .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 { diff --git a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/ImageFormat.kt b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/ImageFormat.kt index c59672e..9f0578d 100644 --- a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/ImageFormat.kt +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/ImageFormat.kt @@ -3,10 +3,12 @@ package net.codinux.banking.epcqrcode 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 } \ No newline at end of file diff --git a/EpcQrCode/src/commonTest/kotlin/net.codinux.banking.epcqrcode/EpcQrCodeGeneratorTest.kt b/EpcQrCode/src/commonTest/kotlin/net.codinux.banking.epcqrcode/EpcQrCodeGeneratorTest.kt index fd687f2..e2ca8a1 100644 --- a/EpcQrCode/src/commonTest/kotlin/net.codinux.banking.epcqrcode/EpcQrCodeGeneratorTest.kt +++ b/EpcQrCode/src/commonTest/kotlin/net.codinux.banking.epcqrcode/EpcQrCodeGeneratorTest.kt @@ -2,6 +2,7 @@ package net.codinux.banking.epcqrcode import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertTrue class EpcQrCodeGeneratorTest { @@ -14,7 +15,7 @@ class EpcQrCodeGeneratorTest { @Test fun basicDataOnly() { - val config = EpcQrCodeConfig(ReceiverName, ReceiverIban) + val config = basicDataConfig() val result = underTest.generateAsString(config) @@ -32,4 +33,28 @@ class EpcQrCodeGeneratorTest { """.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) } \ No newline at end of file