Implemented converting QR code to resulting image format
This commit is contained in:
parent
14edcc6aae
commit
b0f118cfad
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
) {
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
||||
}
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue