Implemented generating QR code

This commit is contained in:
dankito 2022-03-13 23:23:24 +01:00
parent 01b5d33dfb
commit 4061206e34
7 changed files with 48 additions and 14 deletions

View File

@ -53,7 +53,7 @@ kotlin {
sourceSets {
val commonMain by getting {
dependencies {
api("com.soywiz.korlibs.korim:korim:2.6.2")
}
}
val commonTest by getting {

View File

@ -0,0 +1,13 @@
package net.codinux.banking.epcqrcode
import com.soywiz.korim.bitmap.Bitmap32
open class EpcQrCode(
open val bitmap: Bitmap32
) {
open val bytes: ByteArray
get() = bitmap.extractBytes()
}

View File

@ -1,9 +1,20 @@
package net.codinux.banking.epcqrcode
import com.soywiz.korim.qr.QR
import com.soywiz.korim.qr.QRErrorCorrectLevel
open class EpcQrCodeGenerator {
open fun generateEpcQrCode(param: EpcQrCodeConfig): EpcQrCode {
val qrCode = QR(correctLevel = QRErrorCorrectLevel.M)
val qrCodeBitmap = qrCode.msg(generateAsString(param))
return EpcQrCode(qrCodeBitmap)
}
open fun generate(param: EpcQrCodeConfig): EpcQrCodeValues {
return EpcQrCodeValues(
EpcQrCodeValues.ServiceTagDefaultValue,

View File

@ -0,0 +1,8 @@
package net.codinux.banking.epcqrcode
import com.soywiz.korim.awt.toAwt
import java.awt.image.BufferedImage
val EpcQrCode.image: BufferedImage
get() = this.bitmap.toAwt()

View File

@ -1,10 +1,16 @@
package net.codinux.banking.epcqrcode
import android.content.Context
import android.graphics.Bitmap
import android.os.IBinder
import android.view.View
import android.view.inputmethod.InputMethodManager
import com.google.android.material.textfield.TextInputEditText
import com.soywiz.korim.format.toAndroidBitmap
val EpcQrCode.image: Bitmap
get() = this.bitmap.toAndroidBitmap()
fun Context.hideKeyboard(anyViewInHierarchy: View, flags: Int = 0) {

View File

@ -33,13 +33,10 @@ class MainActivity : AppCompatActivity() {
val density = resources.displayMetrics.density
val config = EncodeToQrCodeConfig((EncodeToQrCodeConfig.DefaultWidth * density).toInt(), (EncodeToQrCodeConfig.DefaultHeight * density).toInt())
val qrCodeContent = EpcQrCodeGenerator().generateAsString(param)
// TODO: this takes a very long time. Improve it
val generatedQrCode = QrCodeGenerator().generateQrCode(qrCodeContent, config)
val epcQrCode = EpcQrCodeGenerator().generateEpcQrCode(param)
runOnUiThread {
done(generatedQrCode)
done(epcQrCode.image)
}
}
}

View File

@ -50,11 +50,11 @@ class MainWindow : View(FX.messages["application.title"]) {
private val isDestinationFileSelected = SimpleBooleanProperty(false)
private val qrCodeGenerator = QrCodeGenerator()
private val qrCodeGenerator = EpcQrCodeGenerator()
private val generatedQrCodeImage = SimpleObjectProperty<Image>()
private var generatedQrCodeBytes: ByteArray? = null // couldn't find no working way to get image bytes from image -> also saving pure bytes in memory (what a waste)
private var generatedEpcQrCode: EpcQrCode? = null
private val serializer = JacksonJsonSerializer()
@ -267,21 +267,20 @@ class MainWindow : View(FX.messages["application.title"]) {
}
private fun generateQrCode() {
val param = EpcQrCodeConfig(receiver.value, iban.value, bic.value, amount.value, reference.value)
val qrCodeContent = EpcQrCodeGenerator().generateAsString(param)
val config = EpcQrCodeConfig(receiver.value, iban.value, bic.value, amount.value, reference.value)
generatedQrCodeImage.value = generateQrCode(qrCodeContent)
generatedQrCodeImage.value = generateQrCode(config)
currentStage?.sizeToScene()
saveSettings() // TODO: actually a side effect
}
private fun generateQrCode(informationToEncode: String): Image? {
private fun generateQrCode(config: EpcQrCodeConfig): Image? {
try {
generatedQrCodeBytes = qrCodeGenerator.generateQrCode(informationToEncode)
generatedEpcQrCode = qrCodeGenerator.generateEpcQrCode(config)
return Image(ByteArrayInputStream(generatedQrCodeBytes))
return generatedEpcQrCode?.image
} catch (e: Exception) {
logger.error("Could not generate QR code for $informationToEncode", e)
}