Skip to content

Commit

Permalink
使用JDK17重新编译打包zxing模组,增强反色二维码读取能力
Browse files Browse the repository at this point in the history
  • Loading branch information
RyuZUSUNC committed Aug 12, 2022
1 parent 6b2148d commit e4f477d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 62 deletions.
File renamed without changes.
Binary file added lib/zxing-core-javase-onjdk17-3.5.0.jar
Binary file not shown.
12 changes: 4 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
<artifactId>ApacheCommons</artifactId>
<version>1.1</version>
<scope>system</scope>
<systemPath>${pom.basedir}/lib/ApacheCommons-1.1.jar</systemPath>
<systemPath>${pom.basedir}/lib/ApacheCommons-onjdk17-1.1.jar</systemPath>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
Expand All @@ -159,14 +159,10 @@

<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.5.0</version>
</dependency>

<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<artifactId>core-javase</artifactId>
<version>3.5.0</version>
<scope>system</scope>
<systemPath>${pom.basedir}/lib/zxing-core-javase-onjdk17-3.5.0.jar</systemPath>
</dependency>

<!-- <dependency>-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ public static BufferedImage encode(QRcodeParameters parameters) throws WriterExc
public static String decode(String filepath, String characterSet) throws IOException, NotFoundException {
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filepath));
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
// source = new InvertedLuminanceSource(source);
return getResultString(characterSet, source);
}

public static String decodeReverseColor(String filepath, String characterSet) throws IOException, NotFoundException {
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filepath));
LuminanceSource source = new InvertedLuminanceSource(new BufferedImageLuminanceSource(bufferedImage));
return getResultString(characterSet, source);
}

private static String getResultString(String characterSet, LuminanceSource source) throws NotFoundException {
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap bitmap = new BinaryBitmap(binarizer);
HashMap<DecodeHintType, Object> decodeHints = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,60 +85,9 @@ protected void initialize() {
public void ONClickConfirm() {
super.ONClickConfirm();
if (JTB_modeSelect.isSelected()) {
if (JTA_src.getText().length() > 0) {
JSP_running.setVisible(true);
qRcodeParameters.setInputContent(JTA_src.getText());
qRcodeParameters.setBackgroundColor(colorStringConvert(JCP_BKColor.getValue().toString()));
qRcodeParameters.setQrCodeColor(colorStringConvert(JCP_QRColor.getValue().toString()));
qRcodeParameters.setCharacterSet(JCB_charset.getValue().toString());
qRcodeParameters.setBarcodeFormat(getBarcodeFormat(JCB_barcodeFormat.getValue()));
qRcodeParameters.setImgWidth(!JTF_outImgWidth.getText().equals("") ? Integer.parseInt(JTF_outImgWidth.getText()) : 400);
qRcodeParameters.setImgHeight(!JTF_outImgHeight.getText().equals("") ? Integer.parseInt(JTF_outImgHeight.getText()) : 400);
qRcodeParameters.setMargin(JCB_imgMargin.getValue());
new Thread(() -> {
try {
outBufferedImage = Image_QRcode.encode(qRcodeParameters);
Platform.runLater(() -> {
IMG_outImg.setFitHeight(JBT_outImg.getHeight() - margins);
IMG_outImg.setImage(ViewUtils.convertToFxImage(outBufferedImage));
JBT_outImg.setGraphic(IMG_outImg);
JSP_running.setVisible(false);
});
} catch (WriterException | IOException e) {
Platform.runLater(() -> {
ViewUtils.alertPane((Stage) JLB_title.getScene().getWindow(), Init.getLanguage("Warning"), e.getMessage());
JSP_running.setVisible(false);
});
throw new RuntimeException(e);
}
}).start();
} else {
ViewUtils.alertPane((Stage) JLB_title.getScene().getWindow(), Init.getLanguage("Warning"), Init.getLanguage("ErrorMessage_notNull"));
JSP_running.setVisible(false);
}
encode();
} else {
if (ImgFile != null) {
JSP_running.setVisible(true);
new Thread(() -> {
try {
JTA_dst.setText(Image_QRcode.decode(ImgFile.getPath(), JCB_charset.getValue().toString()));
Platform.runLater(() -> JSP_running.setVisible(false));
} catch (IOException e) {
Platform.runLater(() -> {
ViewUtils.alertPane((Stage) JLB_title.getScene().getWindow(), Init.getLanguage("Warning"), e.getMessage());
JSP_running.setVisible(false);
});
} catch (NotFoundException e) {
Platform.runLater(() -> {
ViewUtils.alertPane((Stage) JLB_title.getScene().getWindow(), Init.getLanguage("Warning"), Init.getLanguage("ErrorMessage_noQRcode"));
JSP_running.setVisible(false);
});
}
}).start();
} else {
ViewUtils.alertPane((Stage) JLB_title.getScene().getWindow(), Init.getLanguage("Warning"), Init.getLanguage("ErrorMessage_notNull"));
JSP_running.setVisible(false);
}
decode();
}
}

Expand Down Expand Up @@ -294,4 +243,73 @@ private int colorStringConvert(String colorString) {
}
return new BigInteger(sb.toString(), 16).intValue();
}

private void decode() {
if (ImgFile != null) {
JSP_running.setVisible(true);
new Thread(() -> {
try {
JTA_dst.setText(Image_QRcode.decode(ImgFile.getPath(), JCB_charset.getValue().toString()));
Platform.runLater(() -> JSP_running.setVisible(false));
} catch (IOException e) {
Platform.runLater(() -> {
ViewUtils.alertPane((Stage) JLB_title.getScene().getWindow(), Init.getLanguage("Warning"), e.getMessage());
JSP_running.setVisible(false);
});
} catch (NotFoundException e) {
try {
JTA_dst.setText(Image_QRcode.decodeReverseColor(ImgFile.getPath(), JCB_charset.getValue().toString()));
JSP_running.setVisible(false);
} catch (IOException ex) {
Platform.runLater(() -> {
ViewUtils.alertPane((Stage) JLB_title.getScene().getWindow(), Init.getLanguage("Warning"), ex.getMessage());
JSP_running.setVisible(false);
});
} catch (NotFoundException ex) {
Platform.runLater(() -> {
ViewUtils.alertPane((Stage) JLB_title.getScene().getWindow(), Init.getLanguage("Warning"), Init.getLanguage("ErrorMessage_noQRcode"));
JSP_running.setVisible(false);
});
}
}
}).start();
} else {
ViewUtils.alertPane((Stage) JLB_title.getScene().getWindow(), Init.getLanguage("Warning"), Init.getLanguage("ErrorMessage_notNull"));
JSP_running.setVisible(false);
}
}

private void encode() {
if (JTA_src.getText().length() > 0) {
JSP_running.setVisible(true);
qRcodeParameters.setInputContent(JTA_src.getText());
qRcodeParameters.setBackgroundColor(colorStringConvert(JCP_BKColor.getValue().toString()));
qRcodeParameters.setQrCodeColor(colorStringConvert(JCP_QRColor.getValue().toString()));
qRcodeParameters.setCharacterSet(JCB_charset.getValue().toString());
qRcodeParameters.setBarcodeFormat(getBarcodeFormat(JCB_barcodeFormat.getValue()));
qRcodeParameters.setImgWidth(!JTF_outImgWidth.getText().equals("") ? Integer.parseInt(JTF_outImgWidth.getText()) : 400);
qRcodeParameters.setImgHeight(!JTF_outImgHeight.getText().equals("") ? Integer.parseInt(JTF_outImgHeight.getText()) : 400);
qRcodeParameters.setMargin(JCB_imgMargin.getValue());
new Thread(() -> {
try {
outBufferedImage = Image_QRcode.encode(qRcodeParameters);
Platform.runLater(() -> {
IMG_outImg.setFitHeight(JBT_outImg.getHeight() - margins);
IMG_outImg.setImage(ViewUtils.convertToFxImage(outBufferedImage));
JBT_outImg.setGraphic(IMG_outImg);
JSP_running.setVisible(false);
});
} catch (WriterException | IOException e) {
Platform.runLater(() -> {
ViewUtils.alertPane((Stage) JLB_title.getScene().getWindow(), Init.getLanguage("Warning"), e.getMessage());
JSP_running.setVisible(false);
});
throw new RuntimeException(e);
}
}).start();
} else {
ViewUtils.alertPane((Stage) JLB_title.getScene().getWindow(), Init.getLanguage("Warning"), Init.getLanguage("ErrorMessage_notNull"));
JSP_running.setVisible(false);
}
}
}
1 change: 0 additions & 1 deletion src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
requires java.naming;
requires java.sql;
requires com.google.zxing;
requires com.google.zxing.javase;
}

0 comments on commit e4f477d

Please sign in to comment.