如何使用胶水手机生成二维码或条形码,以支持多平台? [英] How to generate QR Code or Bar Code by using Gluon mobile in order to support multi-platorm?

查看:21
本文介绍了如何使用胶水手机生成二维码或条形码,以支持多平台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我想从设备的UUID动态生成二维码。我想知道怎么做才能在胶水中支持多平台?如果我使用由GLUON团队开发的Normall Java库或专用库进行简化,也请推荐我。

推荐答案

您可以使用Zxinglibrary在您的设备上生成QR。此库与Android上的Charm Down BarcodeScan服务使用的库相同。

首先,将此依赖项添加到您的构建中:

compile 'com.google.zxing:core:3.3.3'

现在您可以组合设备服务以使用QR生成器检索UUID。

获得ZXING格式的QR后,您将需要生成图像或文件。

鉴于您不能在Android/iOS上使用Swing,您必须避免MatrixToImageWriter,并根据生成的像素手动进行。

类似以下内容:

public Image generateQR(int width, int height) {
    String uuid = Services.get(DeviceService.class)
            .map(DeviceService::getUuid)
            .orElse("123456789"); // <--- for testing on desktop

    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    try {
        BitMatrix bitMatrix = qrCodeWriter.encode(uuid, BarcodeFormat.QR_CODE, width, height);

        WritablePixelFormat<IntBuffer> wf = PixelFormat.getIntArgbInstance();
        WritableImage writableImage = new WritableImage(width, height);
        PixelWriter pixelWriter = writableImage.getPixelWriter();

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                pixelWriter.setColor(x, y, bitMatrix.get(x, y) ? 
                     Color.BLACK : Color.WHITE);
            }
        }
        return writableImage;

    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}

现在可以从视图中调用此方法,添加一个ImageView来呈现生成的图像:

ImageView imageView = new ImageView();
imageView.setFitWidth(256);
imageView.setFitHeight(256);

imageView.setImage(service.generateQR(256, 256));

编辑

如果您想生成二维码或条形码,可以将generateQR中的上述代码替换为:

MultiFormatWriter codeWriter = new MultiFormatWriter();
BitMatrix bitMatrix = codeWriter.encode(uuid, format, width, height);
... 

并将格式为的参数设置为:

  • 二维码:BarcodeFormat.QR_CODE,使用256x256这样的正方形
  • 条码:BarcodeFormat.CODE_128,并使用256 x 64这样的矩形大小

这篇关于如何使用胶水手机生成二维码或条形码,以支持多平台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆