Added QR code width and height to EpcQrCodeConfig

This commit is contained in:
dankito 2023-04-10 23:32:20 +02:00
parent 6ed52967a1
commit 14edcc6aae
4 changed files with 20 additions and 27 deletions

View File

@ -5,7 +5,7 @@ import com.soywiz.korim.bitmap.Bitmap32
open class EpcQrCode(
bitmap: Bitmap32,
heightAndWidth: Int = DefaultHeightAndWidth
heightAndWidth: Int
) {
companion object {
@ -15,30 +15,15 @@ open class EpcQrCode(
}
open var bitmap: Bitmap32 = bitmap // declare before init() method, otherwise application will crash
open var bitmap: Bitmap32 = bitmap
protected set
open val bytes: ByteArray
get() = bitmap.extractBytes()
open val height: Int
get() = bitmap.height
open val height: Int = heightAndWidth
open val width: Int
get() = bitmap.width
constructor(bitmap: Bitmap32) : this(bitmap, DefaultHeightAndWidth) // convenience constructor for languages that don't support default parameters
init {
scale(heightAndWidth)
}
open fun scale(heightAndWidth: Int) {
this.bitmap = bitmap.scaled(heightAndWidth, heightAndWidth, true)
}
open val width: Int = heightAndWidth
open fun qrCodeAsString(border: Int = 0, invertColors: Boolean = false): String {

View File

@ -15,6 +15,8 @@ open class EpcQrCodeConfig(
open val noteToUser: String? = null,
open val qrCodeHeightAndWidth: Int = EpcQrCode.DefaultHeightAndWidth,
open val encoding: EpcQrCodeCharacterSet = EpcQrCodeCharacterSet.UTF_8
) {

View File

@ -6,15 +6,15 @@ import com.soywiz.korim.qr.QRErrorCorrectLevel
open class EpcQrCodeGenerator {
protected open val qrCode = QR(correctLevel = QRErrorCorrectLevel.M)
open fun generateEpcQrCode(config: EpcQrCodeConfig): EpcQrCode {
return generateEpcQrCode(config, EpcQrCode.DefaultHeightAndWidth)
}
val heightAndWidth = config.qrCodeHeightAndWidth
val epcQrCodeContent = generateAsString(config)
open fun generateEpcQrCode(config: EpcQrCodeConfig, heightAndWidth: Int = EpcQrCode.DefaultHeightAndWidth): EpcQrCode {
val qrCode = QR(correctLevel = QRErrorCorrectLevel.M)
val qrCodeBitmap = qrCode.msg(generateAsString(config))
val qrCodeBitmap = qrCode.msg(epcQrCodeContent)
.scaled(heightAndWidth, heightAndWidth, true)
return EpcQrCode(qrCodeBitmap, heightAndWidth)
}

View File

@ -1,5 +1,6 @@
package net.codinux.banking.epcqrcode.rest.service
import net.codinux.banking.epcqrcode.EpcQrCode
import net.codinux.banking.epcqrcode.EpcQrCodeConfig
import net.codinux.banking.epcqrcode.EpcQrCodeGenerator
import net.codinux.banking.epcqrcode.rest.dto.GenerateEpcQrCodeRequest
@ -14,11 +15,15 @@ class EpcQrCodeService {
private val base64Encoder = Base64.getEncoder()
fun generateEpcQrCodeBase64Encoded(request: GenerateEpcQrCodeRequest): ByteArray {
val epcQrCode = epcQrCodeGenerator.generateEpcQrCode(mapToEpcQrCodeConfig(request), request.imageHeightAndWidth)
val epcQrCode = generateEpcQrCode(request)
return base64Encoder.encode(epcQrCode.bytes)
}
private fun generateEpcQrCode(request: GenerateEpcQrCodeRequest): EpcQrCode {
return epcQrCodeGenerator.generateEpcQrCode(mapToEpcQrCodeConfig(request))
}
private fun mapToEpcQrCodeConfig(dto: GenerateEpcQrCodeRequest): EpcQrCodeConfig {
return EpcQrCodeConfig(
dto.receiverName,
@ -26,7 +31,8 @@ class EpcQrCodeService {
dto.bic,
dto.amount,
dto.reference,
dto.noteToUser
dto.noteToUser,
dto.imageHeightAndWidth
)
}
}