Implemented generating EPC QR code for JavaFX

This commit is contained in:
dankito 2020-11-16 00:15:16 +01:00
parent c8d1a1a98c
commit 0f8f09e669
4 changed files with 130 additions and 9 deletions

View File

@ -32,7 +32,7 @@ tasks.withType<KotlinCompile>() {
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation(project(":EpcQrCode"))
api(project(":EpcQrCode"))
implementation("net.dankito.utils:java-fx-utils:1.0.8")
}

View File

@ -1,32 +1,137 @@
package net.codinux.banking.epcqrcode.windows.main
import javafx.beans.property.SimpleObjectProperty
import javafx.beans.property.SimpleStringProperty
import javafx.geometry.Pos
import javafx.scene.control.TextFormatter
import javafx.scene.image.Image
import tornadofx.*
import java.io.ByteArrayInputStream
import net.codinux.banking.epcqrcode.*
import org.slf4j.LoggerFactory
import tornadofx.*
import java.io.ByteArrayInputStream
import java.text.DecimalFormat
import java.text.ParsePosition
class MainWindow : View(FX.messages["application.title"]) {
companion object {
private const val FieldHeight = 31.0
private val DecimalFormat = DecimalFormat( "#.0" )
private val logger = LoggerFactory.getLogger(MainWindow::class.java)
}
private val qrCodeContent = EpcQrCodeCreator().generateAsString(CreatorParam("Mahatma Gandhi", "IN00123456789876543210", null, 1234.56, "Struggle for independence")) // TODO: remove again
private val receiver = SimpleStringProperty("")
private val iban = SimpleStringProperty("")
private val bic = SimpleStringProperty("")
private val amount = SimpleStringProperty("")
private val reference = SimpleStringProperty("")
private val qrCodeGenerator = QrCodeGenerator()
private val qrCode = SimpleObjectProperty<Image>(generateQrCode(qrCodeContent))
private val generatedQrCode = SimpleObjectProperty<Image>()
override val root = vbox {
imageview(qrCode)
prefWidth = 350.0
form {
fieldset {
field(messages["Receiver"]) {
textfield(receiver) {
prefHeight = FieldHeight
}
}
field(messages["IBAN"]) {
textfield(iban) {
prefHeight = FieldHeight
}
}
field(messages["BIC"]) {
textfield(bic) {
prefHeight = FieldHeight
}
}
field(messages["Amount"]) {
textfield(amount) {
prefHeight = FieldHeight
setTextFormatter(TextFormatter<String> { change ->
isDecimalNumberEntered(change)
})
}
}
field(messages["Reference"]) {
textfield(reference) {
prefHeight = FieldHeight
}
}
}
hbox(alignment = Pos.CENTER_RIGHT) {
button(messages["Generate"]) {
prefWidth = 200.0
minHeight = 36.0
action { generateQrCode() }
}
}
}
hbox(alignment = Pos.CENTER) {
imageview(generatedQrCode) {
hboxConstraints {
marginTop = 4.0
}
}
}
}
private fun isDecimalNumberEntered(change: TextFormatter.Change): TextFormatter.Change? {
val newText = change.controlNewText
if (newText.isEmpty()) {
return change
}
val parsePosition = ParsePosition(0)
val parsedNumber = DecimalFormat.parse(newText, parsePosition)
return if (parsedNumber == null || parsePosition.index < newText.length) {
null
}
else {
change
}
}
private fun generateQrCode() {
val param = CreatorParam(receiver.value, iban.value, bic.value, map(amount.value), reference.value)
val qrCodeContent = EpcQrCodeCreator().generateAsString(param)
generatedQrCode.value = generateQrCode(qrCodeContent)
currentStage?.sizeToScene()
}
private fun map(amount: String): Double? {
return DecimalFormat.parse(amount, ParsePosition(0))?.toDouble()
}
private fun generateQrCode(informationToEncode: String): Image? {
try {
val qrCodeBytes = qrCodeGenerator.generateQrCode(informationToEncode)

View File

@ -1,4 +1,4 @@
application.title=EPC QR Code
application.title=EPC QR Code Generator
ok=OK
@ -6,4 +6,12 @@ cancel=Cancel
main.window.menu.file=File
main.window.menu.file.quit=Quit
main.window.menu.file.quit=Quit
Receiver=Receiver
IBAN=IBAN
BIC=BIC
Amount=Amount
Reference=Reference
Generate=Generate

View File

@ -6,4 +6,12 @@ cancel=Abbrechen
main.window.menu.file=Datei
main.window.menu.file.quit=Beenden
main.window.menu.file.quit=Beenden
Receiver=Empfänger
IBAN=IBAN
BIC=BIC
Amount=Betrag
Reference=Verwendungszweck
Generate=Erstellen