diff --git a/EpcQrCode/build.gradle.kts b/EpcQrCode/build.gradle.kts index 7f6548b..b9f8133 100644 --- a/EpcQrCode/build.gradle.kts +++ b/EpcQrCode/build.gradle.kts @@ -53,7 +53,7 @@ kotlin { sourceSets { val commonMain by getting { dependencies { - + api("com.soywiz.korlibs.korim:korim:2.6.2") } } val commonTest by getting { diff --git a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt new file mode 100644 index 0000000..a7725e6 --- /dev/null +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt @@ -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() + +} \ No newline at end of file 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 99c75b0..50282ee 100644 --- a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeGenerator.kt +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeGenerator.kt @@ -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, diff --git a/EpcQrCode/src/jvmMain/kotlin/net/codinux/banking/epcqrcode/Extensions.kt b/EpcQrCode/src/jvmMain/kotlin/net/codinux/banking/epcqrcode/Extensions.kt new file mode 100644 index 0000000..1deb9e8 --- /dev/null +++ b/EpcQrCode/src/jvmMain/kotlin/net/codinux/banking/epcqrcode/Extensions.kt @@ -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() \ No newline at end of file diff --git a/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/Extensions.kt b/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/Extensions.kt index a069078..0efbb3a 100644 --- a/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/Extensions.kt +++ b/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/Extensions.kt @@ -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) { diff --git a/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/MainActivity.kt b/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/MainActivity.kt index a2734b1..ebfb775 100644 --- a/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/MainActivity.kt +++ b/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/MainActivity.kt @@ -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) } } } diff --git a/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/MainWindow.kt b/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/MainWindow.kt index 3f42745..9d61142 100644 --- a/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/MainWindow.kt +++ b/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/MainWindow.kt @@ -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() - 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) }