gpt4 book ai didi

java - 如何在 Gluon 中生成带有图像叠加的二维码?

转载 作者:行者123 更新时间:2023-11-29 04:11:51 24 4
gpt4 key购买 nike

我想生成必须将 Logo 放在中心的 QR CODE。我检查了 zxing 库,并通过阅读这段代码 ( How to generate QR code with logo inside it? ) 使用 Java 应用程序完成了它。但据我所知,我们不能在 android/ios 中使用 swing。那么如何处理呢?

最佳答案

根据您所指的 link ,已接受的答案指向此 post ,您可以在其中看到生成 QR 的技巧,该 QR 允许隐藏其中心部分而不影响 QR 扫描仪的可读性,可以通过增加错误 level :

30% (H) of error correction were a error correction of level H should result in a QRCode that is still valid even when it’s 30% obscured

作为这个 question 的后续,你可以在编码文本时只包含一个提示:

public Image generateQR(int width, int height) {
File root = Services.get(StorageService.class)
.flatMap(StorageService::getPrivateStorage)
.orElseThrow(() -> new RuntimeException("Storage Service not found"));

String uuid = Services.get(DeviceService.class)
.map(DeviceService::getUuid)
.orElse("123456789");

MultiFormatWriter codeWriter = new MultiFormatWriter();

// Add maximum correction
Map<EncodeHintType, ErrorCorrectionLevel> hints = new HashMap<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

try {
BarcodeFormat format = BarcodeFormat.QR_CODE;
BitMatrix bitMatrix = codeWriter.encode(uuid, format,
width, height, hints); // <--- add hints
...
return writableImage;
}
}

您将获得一个没有叠加层的二维码图像。

下一步是在不使用 Swing 的情况下将此图像与您的 Logo 图像结合起来。但实际上我们不需要这样做:我们可以只使用两个 ImageView 节点!

ImageView imageView = new ImageView();
imageView.setFitWidth(256);
imageView.setFitHeight(256);
imageView.setImage(service.generateQR(256, 256));

ImageView overlay = new ImageView(
new Image(getClass().getResourceAsStream("/icon.png")));
overlay.setFitWidth(80);
overlay.setFitHeight(80);

StackPane combinedQR = new StackPane(imageView, overlay);
...

生成的代码仍然可读。

您还可以使用结果,为图像添加混合效果,例如:

overlay.setBlendMode(BlendMode.ADD);

但这将取决于您的 Logo 图像如何与 QR 融合。

最后,如果您仍然需要单个组合图像,您可以创建堆栈 Pane 的快照:

WritableImage snapshot = combinedQR.snapshot(new SnapshotParameters(), null);

关于java - 如何在 Gluon 中生成带有图像叠加的二维码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54628972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com