Implemented generating QR code
This commit is contained in:
parent
01b5d33dfb
commit
4061206e34
|
@ -53,7 +53,7 @@ kotlin {
|
||||||
sourceSets {
|
sourceSets {
|
||||||
val commonMain by getting {
|
val commonMain by getting {
|
||||||
dependencies {
|
dependencies {
|
||||||
|
api("com.soywiz.korlibs.korim:korim:2.6.2")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val commonTest by getting {
|
val commonTest by getting {
|
||||||
|
|
|
@ -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()
|
||||||
|
|
||||||
|
}
|
|
@ -1,9 +1,20 @@
|
||||||
package net.codinux.banking.epcqrcode
|
package net.codinux.banking.epcqrcode
|
||||||
|
|
||||||
|
import com.soywiz.korim.qr.QR
|
||||||
|
import com.soywiz.korim.qr.QRErrorCorrectLevel
|
||||||
|
|
||||||
|
|
||||||
open class EpcQrCodeGenerator {
|
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 {
|
open fun generate(param: EpcQrCodeConfig): EpcQrCodeValues {
|
||||||
return EpcQrCodeValues(
|
return EpcQrCodeValues(
|
||||||
EpcQrCodeValues.ServiceTagDefaultValue,
|
EpcQrCodeValues.ServiceTagDefaultValue,
|
||||||
|
|
|
@ -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()
|
|
@ -1,10 +1,16 @@
|
||||||
package net.codinux.banking.epcqrcode
|
package net.codinux.banking.epcqrcode
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.graphics.Bitmap
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.inputmethod.InputMethodManager
|
import android.view.inputmethod.InputMethodManager
|
||||||
import com.google.android.material.textfield.TextInputEditText
|
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) {
|
fun Context.hideKeyboard(anyViewInHierarchy: View, flags: Int = 0) {
|
||||||
|
|
|
@ -33,13 +33,10 @@ class MainActivity : AppCompatActivity() {
|
||||||
val density = resources.displayMetrics.density
|
val density = resources.displayMetrics.density
|
||||||
val config = EncodeToQrCodeConfig((EncodeToQrCodeConfig.DefaultWidth * density).toInt(), (EncodeToQrCodeConfig.DefaultHeight * density).toInt())
|
val config = EncodeToQrCodeConfig((EncodeToQrCodeConfig.DefaultWidth * density).toInt(), (EncodeToQrCodeConfig.DefaultHeight * density).toInt())
|
||||||
|
|
||||||
val qrCodeContent = EpcQrCodeGenerator().generateAsString(param)
|
val epcQrCode = EpcQrCodeGenerator().generateEpcQrCode(param)
|
||||||
|
|
||||||
// TODO: this takes a very long time. Improve it
|
|
||||||
val generatedQrCode = QrCodeGenerator().generateQrCode(qrCodeContent, config)
|
|
||||||
|
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
done(generatedQrCode)
|
done(epcQrCode.image)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,11 +50,11 @@ class MainWindow : View(FX.messages["application.title"]) {
|
||||||
private val isDestinationFileSelected = SimpleBooleanProperty(false)
|
private val isDestinationFileSelected = SimpleBooleanProperty(false)
|
||||||
|
|
||||||
|
|
||||||
private val qrCodeGenerator = QrCodeGenerator()
|
private val qrCodeGenerator = EpcQrCodeGenerator()
|
||||||
|
|
||||||
private val generatedQrCodeImage = SimpleObjectProperty<Image>()
|
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()
|
private val serializer = JacksonJsonSerializer()
|
||||||
|
@ -267,21 +267,20 @@ class MainWindow : View(FX.messages["application.title"]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateQrCode() {
|
private fun generateQrCode() {
|
||||||
val param = EpcQrCodeConfig(receiver.value, iban.value, bic.value, amount.value, reference.value)
|
val config = EpcQrCodeConfig(receiver.value, iban.value, bic.value, amount.value, reference.value)
|
||||||
val qrCodeContent = EpcQrCodeGenerator().generateAsString(param)
|
|
||||||
|
|
||||||
generatedQrCodeImage.value = generateQrCode(qrCodeContent)
|
generatedQrCodeImage.value = generateQrCode(config)
|
||||||
|
|
||||||
currentStage?.sizeToScene()
|
currentStage?.sizeToScene()
|
||||||
|
|
||||||
saveSettings() // TODO: actually a side effect
|
saveSettings() // TODO: actually a side effect
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateQrCode(informationToEncode: String): Image? {
|
private fun generateQrCode(config: EpcQrCodeConfig): Image? {
|
||||||
try {
|
try {
|
||||||
generatedQrCodeBytes = qrCodeGenerator.generateQrCode(informationToEncode)
|
generatedEpcQrCode = qrCodeGenerator.generateEpcQrCode(config)
|
||||||
|
|
||||||
return Image(ByteArrayInputStream(generatedQrCodeBytes))
|
return generatedEpcQrCode?.image
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
logger.error("Could not generate QR code for $informationToEncode", e)
|
logger.error("Could not generate QR code for $informationToEncode", e)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue