Implemented saving generated QR code
This commit is contained in:
parent
0f8f09e669
commit
6c21222051
|
@ -1,16 +1,21 @@
|
||||||
package net.codinux.banking.epcqrcode.windows.main
|
package net.codinux.banking.epcqrcode.windows.main
|
||||||
|
|
||||||
|
import javafx.beans.property.SimpleBooleanProperty
|
||||||
import javafx.beans.property.SimpleObjectProperty
|
import javafx.beans.property.SimpleObjectProperty
|
||||||
import javafx.beans.property.SimpleStringProperty
|
import javafx.beans.property.SimpleStringProperty
|
||||||
import javafx.geometry.Pos
|
import javafx.geometry.Pos
|
||||||
|
import javafx.scene.control.Alert
|
||||||
import javafx.scene.control.TextFormatter
|
import javafx.scene.control.TextFormatter
|
||||||
import javafx.scene.image.Image
|
import javafx.scene.image.Image
|
||||||
import net.codinux.banking.epcqrcode.*
|
import javafx.scene.layout.Priority
|
||||||
|
import javafx.stage.FileChooser
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import tornadofx.*
|
import tornadofx.*
|
||||||
import java.io.ByteArrayInputStream
|
import java.io.ByteArrayInputStream
|
||||||
|
import java.io.File
|
||||||
import java.text.DecimalFormat
|
import java.text.DecimalFormat
|
||||||
import java.text.ParsePosition
|
import java.text.ParsePosition
|
||||||
|
import net.codinux.banking.epcqrcode.*
|
||||||
|
|
||||||
|
|
||||||
class MainWindow : View(FX.messages["application.title"]) {
|
class MainWindow : View(FX.messages["application.title"]) {
|
||||||
|
@ -35,9 +40,23 @@ class MainWindow : View(FX.messages["application.title"]) {
|
||||||
private val reference = SimpleStringProperty("")
|
private val reference = SimpleStringProperty("")
|
||||||
|
|
||||||
|
|
||||||
|
private val selectedDestinationFile = SimpleStringProperty("")
|
||||||
|
|
||||||
|
private val isDestinationFileSelected = SimpleBooleanProperty(false)
|
||||||
|
|
||||||
|
|
||||||
private val qrCodeGenerator = QrCodeGenerator()
|
private val qrCodeGenerator = QrCodeGenerator()
|
||||||
|
|
||||||
private val generatedQrCode = 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)
|
||||||
|
|
||||||
|
|
||||||
|
init {
|
||||||
|
selectedDestinationFile.addListener { _, _, newValue ->
|
||||||
|
isDestinationFileSelected.value = newValue.isNotBlank()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
override val root = vbox {
|
override val root = vbox {
|
||||||
|
@ -80,18 +99,69 @@ class MainWindow : View(FX.messages["application.title"]) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
hbox(alignment = Pos.CENTER_RIGHT) {
|
hbox {
|
||||||
button(messages["Generate"]) {
|
textfield(selectedDestinationFile) {
|
||||||
prefWidth = 200.0
|
prefHeight = FieldHeight
|
||||||
|
|
||||||
|
hboxConstraints {
|
||||||
|
hGrow = Priority.ALWAYS
|
||||||
|
|
||||||
|
marginRight = 8.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
button("...") {
|
||||||
|
prefWidth = 50.0
|
||||||
|
prefHeight = FieldHeight
|
||||||
|
|
||||||
|
action { selectDestinationFileAndSaveTo() }
|
||||||
|
}
|
||||||
|
|
||||||
|
vboxConstraints {
|
||||||
|
marginBottom = 6.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hbox {
|
||||||
|
useMaxWidth = true
|
||||||
|
isFillWidth = true
|
||||||
|
|
||||||
|
button(messages["generate"]) {
|
||||||
|
useMaxWidth = true
|
||||||
minHeight = 36.0
|
minHeight = 36.0
|
||||||
|
|
||||||
action { generateQrCode() }
|
action { generateQrCode() }
|
||||||
|
|
||||||
|
hboxConstraints {
|
||||||
|
hGrow = Priority.ALWAYS
|
||||||
|
|
||||||
|
marginRight = 8.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
button(messages["generate.and.save"]) {
|
||||||
|
useMaxWidth = true
|
||||||
|
minHeight = 36.0
|
||||||
|
|
||||||
|
enableWhen(isDestinationFileSelected)
|
||||||
|
|
||||||
|
action { generateAndSaveQrCode() }
|
||||||
|
|
||||||
|
hboxConstraints {
|
||||||
|
hGrow = Priority.ALWAYS
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
hbox(alignment = Pos.CENTER) {
|
hbox(alignment = Pos.CENTER) {
|
||||||
imageview(generatedQrCode) {
|
imageview(generatedQrCodeImage) {
|
||||||
|
contextmenu {
|
||||||
|
item(messages["save.to"]) {
|
||||||
|
action { selectDestinationFileAndSaveTo() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
hboxConstraints {
|
hboxConstraints {
|
||||||
marginTop = 4.0
|
marginTop = 4.0
|
||||||
}
|
}
|
||||||
|
@ -119,11 +189,50 @@ class MainWindow : View(FX.messages["application.title"]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private fun selectDestinationFileAndSaveTo() {
|
||||||
|
val fileChooser = FileChooser()
|
||||||
|
|
||||||
|
if (isDestinationFileSelected.value) {
|
||||||
|
val file = File(selectedDestinationFile.value)
|
||||||
|
fileChooser.initialDirectory = file.parentFile
|
||||||
|
fileChooser.initialFileName = file.name
|
||||||
|
}
|
||||||
|
|
||||||
|
fileChooser.showSaveDialog(currentStage)?.let { selectedFile ->
|
||||||
|
selectedDestinationFile.value = selectedFile.absolutePath
|
||||||
|
|
||||||
|
saveGeneratedQrCodeTo(selectedFile)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun saveGeneratedQrCodeTo(selectedFile: File) {
|
||||||
|
generatedQrCodeBytes?.let { generatedQrCodeBytes ->
|
||||||
|
try {
|
||||||
|
selectedFile.writeBytes(generatedQrCodeBytes)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
val alert = Alert(Alert.AlertType.ERROR)
|
||||||
|
alert.title = messages["Could not save QR code"]
|
||||||
|
alert.headerText = ""
|
||||||
|
alert.contentText = e.toString()
|
||||||
|
alert.show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private fun generateAndSaveQrCode() {
|
||||||
|
generateQrCode()
|
||||||
|
|
||||||
|
if (isDestinationFileSelected.value) {
|
||||||
|
saveGeneratedQrCodeTo(File(selectedDestinationFile.value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun generateQrCode() {
|
private fun generateQrCode() {
|
||||||
val param = CreatorParam(receiver.value, iban.value, bic.value, map(amount.value), reference.value)
|
val param = CreatorParam(receiver.value, iban.value, bic.value, map(amount.value), reference.value)
|
||||||
val qrCodeContent = EpcQrCodeCreator().generateAsString(param)
|
val qrCodeContent = EpcQrCodeCreator().generateAsString(param)
|
||||||
|
|
||||||
generatedQrCode.value = generateQrCode(qrCodeContent)
|
generatedQrCodeImage.value = generateQrCode(qrCodeContent)
|
||||||
|
|
||||||
currentStage?.sizeToScene()
|
currentStage?.sizeToScene()
|
||||||
}
|
}
|
||||||
|
@ -134,9 +243,9 @@ class MainWindow : View(FX.messages["application.title"]) {
|
||||||
|
|
||||||
private fun generateQrCode(informationToEncode: String): Image? {
|
private fun generateQrCode(informationToEncode: String): Image? {
|
||||||
try {
|
try {
|
||||||
val qrCodeBytes = qrCodeGenerator.generateQrCode(informationToEncode)
|
generatedQrCodeBytes = qrCodeGenerator.generateQrCode(informationToEncode)
|
||||||
|
|
||||||
return Image(ByteArrayInputStream(qrCodeBytes))
|
return Image(ByteArrayInputStream(generatedQrCodeBytes))
|
||||||
} 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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,4 +14,8 @@ BIC=BIC
|
||||||
Amount=Amount
|
Amount=Amount
|
||||||
Reference=Reference
|
Reference=Reference
|
||||||
|
|
||||||
Generate=Generate
|
generate=Generate
|
||||||
|
generate.and.save=Generate and save
|
||||||
|
save.to=Save to ...
|
||||||
|
|
||||||
|
alert.header.could.not.save.qr.code=Could not save QR code
|
|
@ -14,4 +14,8 @@ BIC=BIC
|
||||||
Amount=Betrag
|
Amount=Betrag
|
||||||
Reference=Verwendungszweck
|
Reference=Verwendungszweck
|
||||||
|
|
||||||
Generate=Erstellen
|
generate=Erstellen
|
||||||
|
generate.and.save=Erstellen und speichern
|
||||||
|
save.to=Speichern unter...
|
||||||
|
|
||||||
|
alert.header.could.not.save.qr.code=QR Code konnte nicht gespeichert werden
|
Loading…
Reference in New Issue