|
1 | 1 | package com.xwintop.xJavaFxTool.controller.littleTools; |
2 | 2 |
|
3 | | -import java.awt.Dimension; |
4 | | -import java.awt.Rectangle; |
5 | | -import java.awt.Robot; |
6 | | -import java.awt.Toolkit; |
7 | | -import java.awt.image.BufferedImage; |
8 | | -import java.io.File; |
9 | | -import java.net.URL; |
10 | | -import java.text.SimpleDateFormat; |
11 | | -import java.util.Date; |
12 | | -import java.util.ResourceBundle; |
13 | | - |
14 | | -import javax.imageio.ImageIO; |
15 | | - |
16 | | -import org.apache.commons.lang.StringUtils; |
17 | | - |
18 | 3 | import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; |
19 | | -import com.melloware.jintellitype.HotkeyListener; |
20 | | -import com.melloware.jintellitype.JIntellitype; |
| 4 | +import com.tulskiy.keymaster.common.Provider; |
21 | 5 | import com.xwintop.xJavaFxTool.Main; |
22 | 6 | import com.xwintop.xJavaFxTool.utils.QRCodeUtil; |
23 | 7 | import com.xwintop.xJavaFxTool.utils.ScreenShoter; |
24 | 8 | import com.xwintop.xJavaFxTool.view.littleTools.QRCodeBuilderView; |
25 | 9 | import com.xwintop.xcore.util.javafx.FileChooserUtil; |
26 | 10 | import com.xwintop.xcore.util.javafx.TooltipUtil; |
27 | | - |
28 | 11 | import javafx.application.Platform; |
29 | 12 | import javafx.beans.value.ChangeListener; |
30 | 13 | import javafx.beans.value.ObservableValue; |
|
34 | 17 | import javafx.scene.image.Image; |
35 | 18 | import javafx.scene.paint.Color; |
36 | 19 | import javafx.stage.FileChooser; |
| 20 | +import lombok.extern.slf4j.Slf4j; |
| 21 | +import org.apache.commons.lang.StringUtils; |
| 22 | + |
| 23 | +import javax.imageio.ImageIO; |
| 24 | +import javax.swing.*; |
| 25 | +import java.awt.*; |
| 26 | +import java.awt.event.InputEvent; |
| 27 | +import java.awt.image.BufferedImage; |
| 28 | +import java.io.File; |
| 29 | +import java.net.URL; |
| 30 | +import java.text.SimpleDateFormat; |
| 31 | +import java.util.Date; |
| 32 | +import java.util.ResourceBundle; |
37 | 33 |
|
38 | 34 | /** |
39 | 35 | * @ClassName: QRCodeBuilderController |
|
42 | 38 | * @date: 2019/4/25 0025 23:26 |
43 | 39 | */ |
44 | 40 |
|
45 | | -public class QRCodeBuilderController extends QRCodeBuilderView{ |
| 41 | +@Slf4j |
| 42 | +public class QRCodeBuilderController extends QRCodeBuilderView { |
| 43 | + private Provider provider; |
| 44 | + |
| 45 | + @Override |
| 46 | + public void initialize(URL location, ResourceBundle resources) { |
| 47 | + initView(); |
| 48 | + initEvent(); |
| 49 | + } |
| 50 | + |
| 51 | + private void initView() { |
| 52 | + codeFormatChoiceBox.getItems().addAll("utf-8", "gb2312", "ISO-8859-1", "US-ASCII", "utf-16"); |
| 53 | + codeFormatChoiceBox.setValue("utf-8"); |
| 54 | + onColorColorPicker.setValue(Color.BLACK); |
| 55 | + offColorColorPicker.setValue(Color.WHITE); |
| 56 | + errorCorrectionLevelChoiceBox.getItems().addAll(ErrorCorrectionLevel.L, ErrorCorrectionLevel.M, |
| 57 | + ErrorCorrectionLevel.Q, ErrorCorrectionLevel.H); |
| 58 | + errorCorrectionLevelChoiceBox.setValue(ErrorCorrectionLevel.H); |
| 59 | + marginChoiceBox.getItems().addAll(1, 2, 3, 4); |
| 60 | + marginChoiceBox.setValue(1); |
| 61 | + formatImageChoiceBox.getItems().addAll("png", "jpg", "gif", "jpeg", "bmp"); |
| 62 | + formatImageChoiceBox.setValue("png"); |
| 63 | + } |
| 64 | + |
| 65 | + private void initEvent() { |
| 66 | + try { |
| 67 | + provider = Provider.getCurrentProvider(false); |
| 68 | + provider.register(KeyStroke.getKeyStroke('S', InputEvent.ALT_DOWN_MASK), hotKey -> { |
| 69 | + snapshotAction(null); |
| 70 | + }); |
| 71 | + TooltipUtil.showToast("按alt+s可快速截图识别!!!"); |
| 72 | + } catch (Exception e) { |
| 73 | + TooltipUtil.showToast("热键注册失败。"); |
| 74 | + } |
| 75 | + contentTextField.textProperty().addListener(new ChangeListener<String>() { |
| 76 | + @Override |
| 77 | + public void changed(ObservableValue<? extends String> arg0, String oldValue, String newValue) { |
| 78 | + Platform.runLater(() -> { |
| 79 | + builderAction(null); |
| 80 | + }); |
| 81 | + } |
| 82 | + }); |
| 83 | + } |
46 | 84 |
|
47 | | - @Override |
48 | | - public void initialize(URL location, ResourceBundle resources) { |
49 | | - initView(); |
50 | | - initEvent(); |
51 | | - } |
| 85 | + @FXML |
| 86 | + private void builderAction(ActionEvent event) { |
| 87 | + if (StringUtils.isEmpty(contentTextField.getText())) { |
| 88 | + return; |
| 89 | + } |
| 90 | + try { |
| 91 | + Image image = QRCodeUtil.toImage(contentTextField.getText(), (int) codeImageView.getFitWidth(), |
| 92 | + (int) codeImageView.getFitHeight(), codeFormatChoiceBox.getValue(), |
| 93 | + errorCorrectionLevelChoiceBox.getValue(), marginChoiceBox.getValue(), onColorColorPicker.getValue(), |
| 94 | + offColorColorPicker.getValue(), formatImageChoiceBox.getValue()); |
| 95 | + codeImageView.setImage(image); |
| 96 | + if (logoCheckBox.isSelected() && codeImageView1.getImage() != null) { |
| 97 | + Image image1 = QRCodeUtil.encodeImgLogo(image, codeImageView1.getImage(), (int) logoSlider.getValue()); |
| 98 | + codeImageView.setImage(image1); |
| 99 | + } |
| 100 | + } catch (Exception e) { |
| 101 | + e.printStackTrace(); |
| 102 | + } |
| 103 | + } |
52 | 104 |
|
53 | | - private void initView() { |
54 | | - codeFormatChoiceBox.getItems().addAll("utf-8", "gb2312", "ISO-8859-1", "US-ASCII", "utf-16"); |
55 | | - codeFormatChoiceBox.setValue("utf-8"); |
56 | | - onColorColorPicker.setValue(Color.BLACK); |
57 | | - offColorColorPicker.setValue(Color.WHITE); |
58 | | - errorCorrectionLevelChoiceBox.getItems().addAll(ErrorCorrectionLevel.L, ErrorCorrectionLevel.M, |
59 | | - ErrorCorrectionLevel.Q, ErrorCorrectionLevel.H); |
60 | | - errorCorrectionLevelChoiceBox.setValue(ErrorCorrectionLevel.H); |
61 | | - marginChoiceBox.getItems().addAll(1, 2, 3, 4); |
62 | | - marginChoiceBox.setValue(1); |
63 | | - formatImageChoiceBox.getItems().addAll("png", "jpg", "gif", "jpeg", "bmp"); |
64 | | - formatImageChoiceBox.setValue("png"); |
65 | | - } |
| 105 | + @FXML |
| 106 | + private void snapshotAction(ActionEvent event) { |
| 107 | + // 默认情况下,Fx运行时会在最后一个stage的close(或hide)后自动关闭,即自动调用Application.stop() |
| 108 | + // 除非通过Platform.setImplicitExit(false)取消这个默认行为。这样,即使所有Fx窗口关闭(或隐藏),Fx运行时还在正常运行 |
| 109 | + Platform.setImplicitExit(false); |
| 110 | + // Main.getStage().setIconified(true); |
| 111 | + if (Main.getStage().isShowing()) { |
| 112 | + Platform.runLater(() -> { |
| 113 | + Main.getStage().hide(); |
| 114 | + }); |
| 115 | + } |
| 116 | + // new SnapshotRectUtil(this); |
| 117 | + new ScreenShoter(this); |
| 118 | + } |
66 | 119 |
|
67 | | - private void initEvent() { |
68 | | - // 第一步:注册热键,第一个参数表示该热键的标识,第二个参数表示组合键,如果没有则为0,第三个参数为定义的主要热键 |
69 | | - try { |
70 | | - JIntellitype.getInstance().registerHotKey(0, JIntellitype.MOD_ALT, (int) 'S'); |
71 | | - // 第二步:添加热键监听器 |
72 | | - JIntellitype.getInstance().addHotKeyListener(new HotkeyListener() { |
73 | | - @Override |
74 | | - public void onHotKey(int markCode) { |
75 | | - switch (markCode) { |
76 | | - case 0: |
77 | | - snapshotAction(null); |
78 | | - break; |
79 | | - } |
80 | | - } |
81 | | - }); |
82 | | - } catch (Exception e) { |
83 | | - TooltipUtil.showToast("热键注册失败。"); |
84 | | - } |
85 | | - contentTextField.textProperty().addListener(new ChangeListener<String>() { |
86 | | - @Override |
87 | | - public void changed(ObservableValue<? extends String> arg0, String oldValue, String newValue) { |
88 | | - Platform.runLater(() -> { |
89 | | - builderAction(null); |
90 | | - }); |
91 | | - } |
92 | | - }); |
93 | | - } |
| 120 | + @FXML |
| 121 | + private void snapshotDesktopAction(ActionEvent event) throws Exception { |
| 122 | + Platform.setImplicitExit(false); |
| 123 | + try { |
| 124 | + Main.getStage().hide(); |
| 125 | + Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit().getScreenSize(); |
| 126 | + Robot robot = new Robot(); |
| 127 | + BufferedImage screenImg = robot.createScreenCapture( |
| 128 | + new Rectangle(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height)); |
| 129 | + Main.getStage().show(); |
| 130 | + String code = QRCodeUtil.toDecode(screenImg); |
| 131 | + if (StringUtils.isNotEmpty(code)) { |
| 132 | + contentTextField.setText(code); |
| 133 | + } else { |
| 134 | + Platform.runLater(() -> { |
| 135 | + TooltipUtil.showToast("未识别到二维码。"); |
| 136 | + }); |
| 137 | + } |
| 138 | + } catch (Exception e) { |
| 139 | + TooltipUtil.showToast("发生异常:" + e.getMessage()); |
| 140 | + } finally { |
| 141 | + Platform.setImplicitExit(true); |
| 142 | + } |
| 143 | + } |
94 | 144 |
|
95 | | - @FXML |
96 | | - private void builderAction(ActionEvent event) { |
97 | | - if (StringUtils.isEmpty(contentTextField.getText())) { |
98 | | - return; |
99 | | - } |
100 | | - try { |
101 | | - Image image = QRCodeUtil.toImage(contentTextField.getText(), (int) codeImageView.getFitWidth(), |
102 | | - (int) codeImageView.getFitHeight(), codeFormatChoiceBox.getValue(), |
103 | | - errorCorrectionLevelChoiceBox.getValue(), marginChoiceBox.getValue(), onColorColorPicker.getValue(), |
104 | | - offColorColorPicker.getValue(), formatImageChoiceBox.getValue()); |
105 | | - codeImageView.setImage(image); |
106 | | - if(logoCheckBox.isSelected() && codeImageView1.getImage()!=null){ |
107 | | - Image image1 = QRCodeUtil.encodeImgLogo(image, codeImageView1.getImage(),(int) logoSlider.getValue()); |
108 | | - codeImageView.setImage(image1); |
109 | | - } |
110 | | - } catch (Exception e) { |
111 | | - e.printStackTrace(); |
112 | | - } |
113 | | - } |
| 145 | + @FXML |
| 146 | + private void saveAction(ActionEvent event) throws Exception { |
| 147 | + String fileName = "x" + new SimpleDateFormat("yyyyMMddHHmm").format(new Date()) + "." + formatImageChoiceBox.getValue(); |
| 148 | + File file = FileChooserUtil.chooseSaveFile(fileName, new FileChooser.ExtensionFilter("All Images", "*.*"), |
| 149 | + new FileChooser.ExtensionFilter("JPG", "*.jpg"), new FileChooser.ExtensionFilter("PNG", "*.png"), |
| 150 | + new FileChooser.ExtensionFilter("gif", "*.gif"), new FileChooser.ExtensionFilter("jpeg", "*.jpeg"), |
| 151 | + new FileChooser.ExtensionFilter("bmp", "*.bmp")); |
| 152 | + if (file != null) { |
| 153 | + String[] fileType = file.getPath().split("\\."); |
| 154 | + ImageIO.write(SwingFXUtils.fromFXImage(codeImageView.getImage(), null), fileType[fileType.length - 1], |
| 155 | + file); |
| 156 | + TooltipUtil.showToast("保存图片成功,图片在:" + file.getPath()); |
| 157 | + } |
| 158 | + } |
114 | 159 |
|
115 | | - @FXML |
116 | | - private void snapshotAction(ActionEvent event) { |
117 | | - // 默认情况下,Fx运行时会在最后一个stage的close(或hide)后自动关闭,即自动调用Application.stop() |
118 | | - // 除非通过Platform.setImplicitExit(false)取消这个默认行为。这样,即使所有Fx窗口关闭(或隐藏),Fx运行时还在正常运行 |
119 | | - Platform.setImplicitExit(false); |
120 | | - // Main.getStage().setIconified(true); |
121 | | - if (Main.getStage().isShowing()) { |
122 | | - Platform.runLater(() -> { |
123 | | - Main.getStage().hide(); |
124 | | - }); |
125 | | - } |
126 | | - // new SnapshotRectUtil(this); |
127 | | - new ScreenShoter(this); |
128 | | - } |
129 | | - |
130 | | - @FXML |
131 | | - private void snapshotDesktopAction(ActionEvent event) throws Exception { |
132 | | - Platform.setImplicitExit(false); |
133 | | - try { |
134 | | - Main.getStage().hide(); |
135 | | - Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit().getScreenSize(); |
136 | | - Robot robot = new Robot(); |
137 | | - BufferedImage screenImg = robot.createScreenCapture( |
138 | | - new Rectangle(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height)); |
139 | | - Main.getStage().show(); |
140 | | - String code = QRCodeUtil.toDecode(screenImg); |
141 | | - if (StringUtils.isNotEmpty(code)) { |
142 | | - contentTextField.setText(code); |
143 | | - }else{ |
144 | | - Platform.runLater(() -> { |
145 | | - TooltipUtil.showToast("未识别到二维码。"); |
146 | | - }); |
147 | | - } |
148 | | - } catch (Exception e) { |
149 | | - TooltipUtil.showToast("发生异常:"+e.getMessage()); |
150 | | - }finally { |
151 | | - Platform.setImplicitExit(true); |
152 | | - } |
153 | | - } |
| 160 | + @FXML |
| 161 | + private void logoAction(ActionEvent event) throws Exception { |
| 162 | + File file = FileChooserUtil.chooseFile(new FileChooser.ExtensionFilter("All Images", "*.*"), |
| 163 | + new FileChooser.ExtensionFilter("JPG", "*.jpg"), new FileChooser.ExtensionFilter("PNG", "*.png"), |
| 164 | + new FileChooser.ExtensionFilter("gif", "*.gif"), new FileChooser.ExtensionFilter("jpeg", "*.jpeg"), |
| 165 | + new FileChooser.ExtensionFilter("bmp", "*.bmp")); |
| 166 | + if (file != null) { |
| 167 | + Image image = SwingFXUtils.toFXImage(ImageIO.read(file), null); |
| 168 | + codeImageView1.setImage(image); |
| 169 | + } |
| 170 | + } |
154 | 171 |
|
155 | | - @FXML |
156 | | - private void saveAction(ActionEvent event) throws Exception { |
157 | | - String fileName = "x"+new SimpleDateFormat("yyyyMMddHHmm").format(new Date())+"."+formatImageChoiceBox.getValue(); |
158 | | - File file = FileChooserUtil.chooseSaveFile(fileName, new FileChooser.ExtensionFilter("All Images", "*.*"), |
159 | | - new FileChooser.ExtensionFilter("JPG", "*.jpg"), new FileChooser.ExtensionFilter("PNG", "*.png"), |
160 | | - new FileChooser.ExtensionFilter("gif", "*.gif"), new FileChooser.ExtensionFilter("jpeg", "*.jpeg"), |
161 | | - new FileChooser.ExtensionFilter("bmp", "*.bmp")); |
162 | | - if (file != null) { |
163 | | - String[] fileType = file.getPath().split("\\."); |
164 | | - ImageIO.write(SwingFXUtils.fromFXImage(codeImageView.getImage(), null), fileType[fileType.length - 1], |
165 | | - file); |
166 | | - TooltipUtil.showToast("保存图片成功,图片在:"+file.getPath()); |
167 | | - } |
168 | | - } |
169 | | - @FXML |
170 | | - private void logoAction(ActionEvent event) throws Exception { |
171 | | - File file = FileChooserUtil.chooseFile(new FileChooser.ExtensionFilter("All Images", "*.*"), |
172 | | - new FileChooser.ExtensionFilter("JPG", "*.jpg"), new FileChooser.ExtensionFilter("PNG", "*.png"), |
173 | | - new FileChooser.ExtensionFilter("gif", "*.gif"), new FileChooser.ExtensionFilter("jpeg", "*.jpeg"), |
174 | | - new FileChooser.ExtensionFilter("bmp", "*.bmp")); |
175 | | - if (file != null) { |
176 | | - Image image = SwingFXUtils.toFXImage(ImageIO.read(file),null); |
177 | | - codeImageView1.setImage(image); |
178 | | - } |
179 | | - } |
| 172 | + public void snapshotActionCallBack(Image image) { |
| 173 | + Platform.runLater(() -> { |
| 174 | + // Main.getStage().setIconified(false); |
| 175 | + Main.getStage().show(); |
| 176 | + }); |
| 177 | + codeImageView1.setImage(image); |
| 178 | + String code = QRCodeUtil.toDecode(image); |
| 179 | + if (StringUtils.isNotEmpty(code)) { |
| 180 | + contentTextField.setText(code); |
| 181 | + } |
| 182 | + Platform.setImplicitExit(true); |
| 183 | + } |
180 | 184 |
|
181 | | - public void snapshotActionCallBack(Image image) { |
182 | | - Platform.runLater(() -> { |
183 | | - // Main.getStage().setIconified(false); |
184 | | - Main.getStage().show(); |
185 | | - }); |
186 | | - codeImageView1.setImage(image); |
187 | | - String code = QRCodeUtil.toDecode(image); |
188 | | - if (StringUtils.isNotEmpty(code)) { |
189 | | - contentTextField.setText(code); |
190 | | - } |
191 | | - Platform.setImplicitExit(true); |
192 | | - } |
| 185 | + /** |
| 186 | + * 父控件被移除前调用 |
| 187 | + */ |
| 188 | + public void onCloseRequest(javafx.event.Event event) { |
| 189 | + try { |
| 190 | + if (provider != null) { |
| 191 | + provider.reset(); |
| 192 | + provider.stop(); |
| 193 | + } |
| 194 | + } catch (Exception e) { |
| 195 | + log.error("停止快捷键监听失败:", e); |
| 196 | + } |
| 197 | + } |
193 | 198 | } |
0 commit comments