【问题标题】:Data too big for QR Code Generation with XZing in Android with Java数据太大,无法在使用 Java 的 Android 中使用 XZing 生成 QR 码
【发布时间】:2020-07-04 22:09:02
【问题描述】:

我正在尝试使用 Java 在我的 Android 应用程序中使用 PGP 公钥生成 QR 码并将其设置为 ImageView。我使用的库是 XZing,我在 Android Studio 上。 这在使用较小的字符串之前效果很好,但是当我尝试使用像 PGP 公钥这样大的东西时,我得到了这个错误:

    com.google.zxing.WriterException: Data too big

所以我的数据太大了。有没有可能在 XZing 中用这么大的字符串创建一个二维码?

我尝试了两种不同的方法,但都导致相同的异常。

方法一:

    private void generateQR(String value) {
        qrImage.setVisibility(View.VISIBLE);

      QRGEncoder qrgEncoder = new QRGEncoder(value, null, QRGContents.Type.TEXT, 1000);

        try {
            // Getting QR-Code as Bitmap
            Bitmap bm = qrgEncoder.encodeAsBitmap();

                qrImage.setImageBitmap(bm);

            // Setting Bitmap to ImageView
        } catch (WriterException e) {
            Log.v(TAG, e.toString());
        }
    }

方法二:

private Bitmap textToImage(String text, int width, int height) throws WriterException, NullPointerException {
        BitMatrix bitMatrix;
        try {
            bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.DATA_MATRIX.QR_CODE,
                    width, height, null);
        } catch (IllegalArgumentException Illegalargumentexception) {
            return null;
        }

        int bitMatrixWidth = bitMatrix.getWidth();
        int bitMatrixHeight = bitMatrix.getHeight();
        int[] pixels = new int[bitMatrixWidth * bitMatrixHeight];

        int colorWhite = 0xFFFFFFFF;
        int colorBlack = 0xFF000000;

        for (int y = 0; y < bitMatrixHeight; y++) {
            int offset = y * bitMatrixWidth;
            for (int x = 0; x < bitMatrixWidth; x++) {
                pixels[offset + x] = bitMatrix.get(x, y) ? colorBlack : colorWhite;
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444);

        bitmap.setPixels(pixels, 0, width, 0, 0, bitMatrixWidth, bitMatrixHeight);
        return bitmap;
    }

【问题讨论】:

    标签: java android android-studio qr-code


    【解决方案1】:

    二维码有一个最大尺寸。 我将纠错级别更改为L,这样就可以适应更长的 URL。

    这可能对你有用:

    new MultiFormatWriter().encode(qrCodeText, BarcodeFormat.QR_CODE, width, height, mapWith(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L));
    

    【讨论】:

    • mapWith(...) 来自哪个库?
    • 我发现拥有这么多信息的 QR 码是没有意义的,因为 QR 图像将无法读取并且您将无法扫描它。所以这个限制是有道理的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 2018-01-13
    相关资源
    最近更新 更多