【问题标题】:Is it possible to set version while generating qr code using zxing lib in android?在android中使用zxing lib生成二维码时是否可以设置版本?
【发布时间】:2020-10-31 23:29:14
【问题描述】:

使用 zxing 库为 android 生成二维码时,可以设置版本号,如版本 4 或任何其他版本。 任何指导或链接都将是可观的。 谢谢。

【问题讨论】:

    标签: android qr-code zxing


    【解决方案1】:

    是的,检查 EncodedHintType 映射:

    private Bitmap stringToQRCode(String text, int width, int height) {
        BitMatrix bitMatrix;
        try {
            HashMap<EncodeHintType, Object> map = new HashMap<>();
            map.put(EncodeHintType.CHARACTER_SET, "utf-8");
            map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); 
            map.put(EncodeHintType.QR_VERSION, 9);  // (1-40)
            map.put(EncodeHintType.MARGIN, 2); // pixels
            bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, map);
    
            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;
        } catch (Exception i) {
            i.printStackTrace();
            return null;
        }
    }
    

    【讨论】:

      【解决方案2】:

      没有。这没有任何实际意义。版本不能低于对数据进行编码所需的版本,将其设置得更高只会使二维码更密集,更难阅读。

      【讨论】:

      • 如何为版本 4(33x33 模块)生成它?我使用 com.keepautomation.barcode 库在普通 java 程序中进行了尝试,并成功生成了版本 4(33x33 模块)二维码,但是否可以在 Android 中实现相同的功能?
      • 你能指导我在android中实现吗?
      • 我不确定你的意思。版本设置为可容纳数据的最低版本。如果是第 4 版,则将使用第 4 版。你根本不需要做任何事情。
      • 好的,我明白你的意思了。根据你的版本将根据数据大小设置任何占用它。谢谢
      猜你喜欢
      • 1970-01-01
      • 2014-04-17
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多