代码语言:txt复制
import javafx.application.Application
import javafx.beans.property.SimpleObjectProperty
import javafx.embed.swing.SwingFXUtils
import javafx.scene.image.Image
import javafx.scene.image.WritableImage
import tornadofx.*
import javax.imageio.ImageIO
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.MultiFormatWriter
import com.google.zxing.client.j2se.MatrixToImageWriter
import javafx.geometry.Pos
import javafx.scene.paint.Color
import javafx.scene.text.FontWeight
import javafx.stage.FileChooser
import javafx.stage.Stage
import java.awt.image.BufferedImage
import java.io.File
import java.util.*
fun main() = Application.launch(QrCodeGenApp::class.java)
class QrCodeGenApp : App(QrCodeGen::class,Styles::class)
val imgPath = stringProperty()
val info = stringProperty()
val qrImage = SimpleObjectProperty<Image>()
val hasImage = booleanProperty(false)
val srcImg = SimpleObjectProperty<Image>()
class QrCodeGen : View("二维码生成器") {
override val root = borderpane {
prefWidth = 700.0
prefHeight = 500.0
top = vbox(10) {
hbox(10) {
button("选择图片").action { chooseImg() }
textfield(imgPath)
alignment = Pos.CENTER
}
hbox(10) {
button("生成二维码图像") {
enableWhen(imgPath.isNotEmpty)
action { genQRCode() }
}
button("保存二维码图像至本地") {
enableWhen(hasImage)
action { saveImg() }
}
alignment = Pos.CENTER
}
}
center = hbox(10) {
alignment = Pos.CENTER
vbox(5) {
label("原始图片")
imageview(srcImg) {
fitHeight = 300.0
fitWidth = 300.0
isPickOnBounds = true
isPreserveRatio = true
alignment = Pos.CENTER
}
}
vbox(5) {
label("二维码图像")
imageview(qrImage) {
fitHeight = 300.0
fitWidth = 300.0
isPickOnBounds = true
isPreserveRatio = true
alignment = Pos.CENTER
}
}
}
bottom = hbox {
label(info)
}
}
private fun chooseImg() {
val imgType = listOf("*.jpg", "*.png", "*.bmp", "*.gif")
imgPath.value = chooseFile0("选择图片", imgType)
}
private fun chooseFile0(title: String? = null, filetype: List<String>): String {
val efset = arrayOf(FileChooser.ExtensionFilter("$filetype", filetype))
val imgFile = chooseFile(title, efset, FileChooserMode.Single) {
// 初始目录为当前项目目录
initialDirectory = File(File("").canonicalPath)
}
if (imgFile.isNotEmpty()) {
var imgpath1 = imgFile.first().toString().replace("\", "/")
// linux系统下文件绝对路径以“/”开头,windows系统下文件绝对路径包含":"
if (imgpath1.startsWith("/").or(imgpath1.contains(":"))) {
imgpath1 = "file:$imgpath1"
srcImg.value = Image(imgpath1)
info.value = "选择图片文件: $imgpath1"
}
return imgpath1
} else {
hasImage.value = false
return ""
}
}
private fun saveImg() {
if (!hasImage.value) return
val fileChooser = FileChooser()
fileChooser.title = "保存图片"
fileChooser.initialDirectory = File(File("").canonicalPath)
fileChooser.initialFileName = "qrcode.png"
val file = fileChooser.showSaveDialog(Stage()) ?: return
ImageIO.write(SwingFXUtils.fromFXImage(qrImage.value, null), "png", file)
// information(header = "", content = "保存图片到${file.absolutePath}成功", title = "保存图片")
info.value = "保存图片到 ${file.absolutePath} 成功"
}
private fun genQRCode() {
if (imgPath.value.isEmpty()) return
val bufImage = createQR(imgPath.value)
val writeAbleImage = WritableImage(bufImage!!.width, bufImage.height)
qrImage.value = SwingFXUtils.toFXImage(bufImage, writeAbleImage)
hasImage.value = true
}
private fun createQR(imgpath: String): BufferedImage? {
val hints = Hashtable<EncodeHintType, String>()
hints[EncodeHintType.CHARACTER_SET] = "utf-8"
val bitMatrix = MultiFormatWriter().encode(imgpath, BarcodeFormat.QR_CODE, 300, 300, hints)
return MatrixToImageWriter.toBufferedImage(bitMatrix);
}
}
class Styles : Stylesheet() {
init {
button {
padding = box(10.px)
alignment = Pos.CENTER
backgroundColor = c("#DD7549")
fontWeight = FontWeight.EXTRA_BOLD
fontSize=18.px
and (hover) {
backgroundColor = c("#A05434")
textFill = Color.WHITE
}
}
label{
fontSize=18.px
textFill = Color.RED
}
}
}