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( open class EpcQrCode(
bitmap: Bitmap32, bitmap: Bitmap32,
heightAndWidth: Int = DefaultHeightAndWidth heightAndWidth: Int
) { ) {
companion object { 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 protected set
open val bytes: ByteArray open val bytes: ByteArray
get() = bitmap.extractBytes() get() = bitmap.extractBytes()
open val height: Int open val height: Int = heightAndWidth
get() = bitmap.height
open val width: Int open val width: Int = heightAndWidth
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 fun qrCodeAsString(border: Int = 0, invertColors: Boolean = false): String { 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 noteToUser: String? = null,
open val qrCodeHeightAndWidth: Int = EpcQrCode.DefaultHeightAndWidth,
open val encoding: EpcQrCodeCharacterSet = EpcQrCodeCharacterSet.UTF_8 open val encoding: EpcQrCodeCharacterSet = EpcQrCodeCharacterSet.UTF_8
) { ) {

View File

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

View File

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