2023年2月23日16:47:25 二维码生成

master
zhangmeng 2023-02-23 16:47:48 +08:00
parent 55200ea20f
commit 18c2040e33
11 changed files with 402 additions and 0 deletions

View File

@ -109,6 +109,11 @@ public class HomeController implements Serializable {
load_small_tools(3);
}
@FXML
public void qr_code_menu_item(){
load_small_tools(4);
}
@FXML
public void base_62_menu_item(){
load_codec_tools(0);

View File

@ -88,6 +88,11 @@ public class PlayerController {
load_small_tools(3);
}
@FXML
private void qr_code_menu_item(){
load_small_tools(4);
}
@FXML
public void video_menu_item() {
boolean flag = false;

View File

@ -0,0 +1,310 @@
package com.zhangmeng.tools.controller;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.sun.javafx.iio.ImageStorage;
import com.zhangmeng.tools.utils.AlertUtils;
import com.zhangmeng.tools.utils.ImagePath;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.w3c.dom.ls.LSInput;
import java.awt.*;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author :
* @version : 1.0
* @date : 2023-02-23 11:39
*/
@Slf4j
public class QrCodeController {
@FXML
private TextField width;
@FXML
private TextField height;
@FXML
private TextField margin;
@FXML
private ColorPicker qr_code_fore_image;
@FXML
private ColorPicker qr_code_back_image;
@FXML
private TextField content;
@FXML
private AnchorPane image_root;
@FXML
private Button file_choose_button;
@FXML
private TextField base_path;
@FXML
private ComboBox<ErrorCorrectionLevel> comboBox;
@FXML
private Button logo_button;
@FXML
private TextField logo_path;
@FXML
private TextArea res_content;
private ObservableList<ErrorCorrectionLevel> list = FXCollections.observableArrayList();
private SimpleObjectProperty<File> logo = new SimpleObjectProperty<>();
private SimpleObjectProperty<File> targetFile_cache = new SimpleObjectProperty<>();
@FXML
public void get_text(){
if (targetFile_cache.getValue() == null){
AlertUtils.alert_warning("没有可识别的二维码!");
return;
}
File value = targetFile_cache.getValue();
String decode = QrCodeUtil.decode(value);
res_content.setText(decode);
}
@FXML
public void file_choose_dir() {
Stage stage = new Stage();
DirectoryChooser dc = new DirectoryChooser();
dc.setTitle("文件夹选择器");
File file = dc.showDialog(stage);
if (file != null) {
log.info(file.getAbsolutePath());
base_path.setText(file.getPath());
}
}
@FXML
public void file_choose_logo() {
Stage stage = new Stage();
FileChooser fc = new FileChooser();
fc.setTitle("单选文件");
fc.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("图片类型", "*.jpg", "*.png")
);
File file = fc.showOpenDialog(stage);
if (file == null) {
return;
}
logo_path.setText(file.getAbsolutePath());
logo.setValue(file);
}
@FXML
public void qr_code_preview() {
qr_code_gen();
}
private void qr_code_gen() {
if (width.getText().length() == 0) {
AlertUtils.alert_warning("请输入图片宽度!");
return;
}
if (height.getText().length() == 0) {
AlertUtils.alert_warning("请输入图片高度!");
return;
}
if (base_path.getText().length() == 0) {
AlertUtils.alert_warning("请选择文件夹!");
return;
}
if (content.getText().length() == 0) {
AlertUtils.alert_warning("请输入内容!");
return;
}
boolean flag_margin = false;
if (margin.getText().length() != 0) {
try {
Integer.parseInt(margin.getText());
} catch (NumberFormatException e) {
AlertUtils.alert_warning("二维码和背景之间的边距为数字!");
return;
}
flag_margin = true;
}
javafx.scene.paint.Color fore_color = qr_code_fore_image.getValue();
javafx.scene.paint.Color back_color = qr_code_back_image.getValue();
QrConfig config = new QrConfig(Integer.parseInt(width.getText()), Integer.parseInt(height.getText()));
// 设置边距,既二维码和背景之间的边距
if (flag_margin) {
config.setMargin(Integer.parseInt(margin.getText()));
}
// 设置前景色,既二维码颜色(青色)
double red = fore_color.getRed();
double green = fore_color.getGreen();
double blue = fore_color.getBlue();
Color color1 = new Color((float) red, (float) green, (float) blue);
// 设置背景色(灰色)
double red1 = back_color.getRed();
double green1 = back_color.getGreen();
double blue1 = back_color.getBlue();
Color color2 = new Color((float) red1, (float) green1, (float) blue1);
config.setForeColor(color1);
log.info("fore_color:---red:{},green:{},blue:{}", color1.getRed(), color1.getGreen(), color1.getBlue());
config.setBackColor(color2);
log.info("fore_color:---red:{},green:{},blue:{}", color2.getRed(), color2.getGreen(), color2.getBlue());
ErrorCorrectionLevel errorCorrectionLevel = comboBox.getSelectionModel().getSelectedItem();
if (logo.getValue() != null){
config.setImg(logo.getValue());
}
config.setErrorCorrection(errorCorrectionLevel);
File targetFile = FileUtil.file(base_path.getText() + File.separator + file_name());
QrCodeUtil.generate(content.getText(), config, targetFile);
ImageView imageView = new ImageView(new Image(targetFile.getPath()));
image_root.getChildren().clear();
image_root.getChildren().add(imageView);
targetFile_cache.setValue(targetFile);
}
public static String file_name() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
return simpleDateFormat.format(new Date()) + ".jpg";
}
@FXML
public void initialize() {
ErrorCorrectionLevel[] values = ErrorCorrectionLevel.values();
list.addAll(values);
comboBox.setItems(list);
comboBox.getSelectionModel().select(0);
comboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
log.info("纠错级别!{}",newValue.getBits());
}
});
Color black = Color.white;
qr_code_back_image.setValue(javafx.scene.paint.Color.rgb(black.getRed(), black.getGreen(), black.getBlue()));
Color fore = Color.black;
qr_code_fore_image.setValue(javafx.scene.paint.Color.rgb(fore.getRed(), fore.getGreen(), fore.getBlue()));
file_choose_button.setText(null);
ImageView iv = new ImageView(new Image(ImagePath.path(ImagePath.ImagePathType.IMAGE_FILE)));
iv.setPreserveRatio(true);
iv.setFitWidth(18);
file_choose_button.setGraphic(iv);
logo_button.setText(null);
ImageView iv2 = new ImageView(new Image(ImagePath.path(ImagePath.ImagePathType.IMAGE_FILE)));
iv2.setPreserveRatio(true);
iv2.setFitWidth(18);
logo_button.setGraphic(iv2);
if (width.getText().length() == 0) {
width.setText("300");
}
if (height.getText().length() == 0) {
height.setText("300");
}
width.textProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
log.info("width exchange : {}", newValue);
}
});
height.textProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
log.info("height exchange : {}", newValue);
}
});
margin.textProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
log.info("margin exchange : {}", newValue);
}
});
}
public static void generate(String content, int width, int height, File targetFile) {
QrCodeUtil.generate(content, width, height, targetFile);
}
public static void generate(String content, int width, int height, int margin, Color fore_color, Color back_color, File targetFile) {
QrConfig config = new QrConfig(width, height);
// 设置边距,既二维码和背景之间的边距
config.setMargin(margin);
// 设置前景色,既二维码颜色(青色)
config.setForeColor(fore_color);
// 设置背景色(灰色)
config.setBackColor(back_color);
QrCodeUtil.generate(content, config, targetFile);
}
public static void generate(String content, int width, int height, int margin, Color fore_color, Color back_color, File logo, File targetFile) {
QrConfig config = new QrConfig(width, height);
// 设置边距,既二维码和背景之间的边距
config.setMargin(margin);
// 设置前景色,既二维码颜色(青色)
config.setForeColor(fore_color);
// 设置背景色(灰色)
config.setBackColor(back_color);
config.setImg(logo);
QrCodeUtil.generate(content, config, targetFile);
}
public static void generate(String content, int width, int height, int margin, Color fore_color, Color back_color, File logo, ErrorCorrectionLevel errorCorrectionLevel, File targetFile) {
QrConfig config = new QrConfig(width, height);
// 设置边距,既二维码和背景之间的边距
config.setMargin(margin);
// 设置前景色,既二维码颜色(青色)
config.setForeColor(fore_color);
// 设置背景色(灰色)
config.setBackColor(back_color);
config.setImg(logo);
config.setErrorCorrection(errorCorrectionLevel);
QrCodeUtil.generate(content, config, targetFile);
}
public static String decode(File file) {
return QrCodeUtil.decode(file);
}
}

View File

@ -44,6 +44,7 @@ public class SmallToolsController {
private AnchorPane unicode;
private AnchorPane jwt_web;
private AnchorPane color_choose;
private AnchorPane qr_code;
@FXML
private ListView<ResourcesUtils.SmallTools> listView;
@ -118,6 +119,16 @@ public class SmallToolsController {
color_choose(flag);
}
@FXML
public void qr_code_menu_item(){
boolean flag = false;
if (qr_code != null){
flag = true;
}
qr_code(flag);
}
@FXML
public void base_62_menu_item(){
load_codec_tools(0);
@ -216,6 +227,12 @@ public class SmallToolsController {
}
color_choose(flag);
}
if (newValue.getIndex() == 4) {
if (qr_code != null) {
flag = true;
}
qr_code(flag);
}
}
});
}
@ -226,6 +243,7 @@ public class SmallToolsController {
case Unicode -> new Image(ImagePath.path(ImagePath.ImagePathType.Unicode));
case JWT_WEB -> new Image(ImagePath.path(ImagePath.ImagePathType.JWT_WEB));
case COLOR_CHOOSE -> new Image(ImagePath.path(ImagePath.ImagePathType.COLOR_CHOOSE));
case Qr_CODE -> new Image(ImagePath.path(ImagePath.ImagePathType.Qr_CODE));
};
}
@ -357,6 +375,23 @@ public class SmallToolsController {
common_method();
}
public void qr_code(boolean flag){
//默认选择第一个
listView.getSelectionModel().select(4);
if (!flag) {
try {
root = FXMLLoader.load(ResourcesUtils.getResource("qr-code"));
} catch (IOException e) {
e.printStackTrace();
}
color_choose = root;
} else {
root = color_choose;
}
common_method();
}
private void common_method() {
splitPane.getItems().remove(1);

View File

@ -60,6 +60,7 @@ public class ImagePath {
Unicode("Unicode"),
COLOR_CHOOSE("颜色选择"),
Tools_ICON("tools-hardware"),
Qr_CODE("二维码"),
ICON_NULL_COVER("");
@ -244,6 +245,7 @@ public class ImagePath {
public static String JWT_WEB = "svg/jwt-web.png";
public static String COLOR_CHOOSE = "svg/color-choose.png";
public static String Tools_ICON = "svg/tools-hardware.png";
public static String Qr_CODE = "svg/qr-code.png";
public static String path(ImagePathType type) {
@ -401,6 +403,9 @@ public class ImagePath {
case Tools_ICON:
path = ImagePath.Tools_ICON;
break;
case Qr_CODE:
path = ImagePath.Qr_CODE;
break;
}
return "static/" + path;
}

View File

@ -84,6 +84,7 @@ public class ResourcesUtils {
Unicode("Unicode和字符串转换",1),
JWT_WEB("json-web-token",2),
COLOR_CHOOSE("颜色选择",3),
Qr_CODE("生成二维码",4),
;
SmallTools(String title, int index) {

View File

@ -34,6 +34,7 @@
<MenuItem mnemonicParsing="false" text="Unicode和字符串转换" onAction="#unicode_menu_item"/>
<MenuItem mnemonicParsing="false" text="jwt工具" onAction="#jwt_menu_item"/>
<MenuItem mnemonicParsing="false" text="颜色选择工具" onAction="#color_choose_menu_item"/>
<MenuItem mnemonicParsing="false" text="二维码生成" onAction="#qr_code_menu_item"/>
</items>
</Menu>

View File

@ -32,6 +32,7 @@
<MenuItem mnemonicParsing="false" text="Unicode和字符串转换" onAction="#unicode_menu_item"/>
<MenuItem mnemonicParsing="false" text="jwt工具" onAction="#jwt_menu_item"/>
<MenuItem mnemonicParsing="false" text="颜色选择工具" onAction="#color_choose_menu_item"/>
<MenuItem mnemonicParsing="false" text="二维码生成" onAction="#qr_code_menu_item"/>
</items>
</Menu>
</menus>

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ColorPicker?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="649.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zhangmeng.tools.controller.QrCodeController">
<children>
<Label layoutX="137.0" layoutY="127.0" text="二维码宽度" />
<TextField fx:id="width" layoutX="233.0" layoutY="123.0" />
<Label layoutX="458.0" layoutY="127.0" text="二维码高度" />
<TextField fx:id="height" layoutX="558.0" layoutY="123.0" />
<Label layoutX="778.0" layoutY="127.0" text="二维码前置颜色" />
<ColorPicker fx:id="qr_code_fore_image" layoutX="903.0" layoutY="123.0" prefHeight="25.0" prefWidth="161.0" />
<Label layoutX="778.0" layoutY="168.0" text="二维码背景颜色" />
<ColorPicker fx:id="qr_code_back_image" layoutX="903.0" layoutY="164.0" prefHeight="25.0" prefWidth="161.0" />
<Label layoutX="137.0" layoutY="168.0" text="二维码和背景之间的边距" />
<TextField fx:id="margin" layoutX="297.0" layoutY="164.0" prefHeight="25.0" prefWidth="161.0" />
<Label layoutX="494.0" layoutY="168.0" text="纠错级别" />
<ComboBox fx:id="comboBox" layoutX="558.0" layoutY="164.0" prefHeight="25.0" prefWidth="161.0" />
<AnchorPane fx:id="image_root" layoutX="136.0" layoutY="267.0" prefHeight="338.0" prefWidth="512.0" style="-fx-border-color: #666666" AnchorPane.bottomAnchor="44.0" AnchorPane.leftAnchor="136.0" AnchorPane.topAnchor="267.0" />
<Button layoutX="664.0" layoutY="267.0" mnemonicParsing="false" onAction="#qr_code_preview" text="预览" AnchorPane.bottomAnchor="357.0" AnchorPane.leftAnchor="664.0" />
<TextArea fx:id="res_content" layoutX="778.0" layoutY="267.0" prefHeight="338.0" prefWidth="229.0" AnchorPane.bottomAnchor="44.0" AnchorPane.leftAnchor="778.0" AnchorPane.topAnchor="267.0" />
<Button onAction="#get_text" layoutX="1019.0" layoutY="267.0" mnemonicParsing="false" text="获取文本" AnchorPane.bottomAnchor="357.0" AnchorPane.leftAnchor="1019.0" />
<Label layoutX="137.0" layoutY="82.0" text="二维码内容" />
<TextField fx:id="content" layoutX="233.0" layoutY="78.0" prefHeight="25.0" prefWidth="830.0" />
<Label layoutX="136.0" layoutY="34.0" text="选择生成二维码的文件夹所在路径" />
<Button fx:id="file_choose_button" layoutX="337.0" layoutY="30.0" mnemonicParsing="false" onAction="#file_choose_dir" text="Button" />
<TextField fx:id="base_path" layoutX="408.0" layoutY="30.0" prefHeight="25.0" prefWidth="654.0" />
<Label layoutX="137.0" layoutY="216.0" text="logo图片" />
<TextField fx:id="logo_path" layoutX="197.0" layoutY="212.0" prefHeight="25.0" prefWidth="523.0" />
<Button fx:id="logo_button" layoutX="735.0" layoutY="212.0" mnemonicParsing="false" text="选择logo" onAction="#file_choose_logo" />
</children>
</AnchorPane>

View File

@ -33,6 +33,7 @@
<MenuItem mnemonicParsing="false" text="Unicode和字符串转换" onAction="#unicode_menu_item"/>
<MenuItem mnemonicParsing="false" text="jwt工具" onAction="#jwt_menu_item"/>
<MenuItem mnemonicParsing="false" text="颜色选择工具" onAction="#color_choose_menu_item"/>
<MenuItem mnemonicParsing="false" text="二维码生成" onAction="#qr_code_menu_item"/>
</items>
</Menu>

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB