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

标签 java gluon gluon-mobile

我想生成必须将 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/

相关文章:

ios - 如何使用 Gluon 应用程序打开/发送 URL 到 iOS 上的另一个应用程序?

java - 无法在 Raspberry Pi Model 3 和 Java 11 上运行简单的 HelloWorld JavaFx 应用程序

java - 本地保存列表时 GluonClient 数据丢失

java - 使用 gluon 移动插件构建 JavaFX And​​roid 应用程序失败

java - 如何对 map 列表进行二次排序

java - 如何通过 BigIntegers 索引 BigIntegers 数组

java - 如何使用 JavaFX 将对象从一个 Controller 传递到另一个 Controller

java - 如何在 JPanel 中制作简单的图形动画

gluon - 如何在 gluon-ts 中使用 DeepAREstimator 和一个以上的额外回归器?

android - 使用 gluon 移动插件在开发 JavaFX 移动应用程序时进行调试