首页 > 解决方案 > android中的二维码生成工作但不是颤振

问题描述

在 android 中,我使用这些 epr 代码行来创建一个 qrcode,一切正常......它使用正确的数据生成我的应用程序可读的 qrcode。我试图在 Flutter 上复制相同的代码,但尽管生成了 qrcode,但它不起作用。未检测到。可能有一些错误的数据?

                qrCodeWriter = new QRCodeWriter();
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("type", 1);
                jsonObject.put("content", email); // "esertest@clikapptest.it"
                String display_name = ContentManager.getInstance(getContext()).getThisAttivita();
                if (display_name != null) {
                    jsonObject.put("display_name", display_name);
                } else
                    jsonObject.put("display_name", ContentManager.getInstance(getContext()).getMerchantData().getIdentifier());
                if (scope.equals(Enums.SCOPES.COLLABORATORE.name()))
                    jsonObject.put("collaborator", DBHelper.getInstance(getActivity()).getSettingsField(DBHelper.SETTINGS_TABLE_FIELD_EMAIL));
                jsonObject.put("mac", it.clikapp.toduba.network.Utils.getMacAddress(getContext()));
//                        BitMatrix bitMatrix = qrCodeWriter.encode(new String(Base64.encode(jsonObject.toString().getBytes(), Base64.DEFAULT)), BarcodeFormat.QR_CODE, iv_qr_code.getWidth(), iv_qr_code.getHeight(), hintsMap);
                BitMatrix bitMatrix = qrCodeWriter.encode(Utils.encryptQRCode(ContentManager.getInstance(getContext()).getOauth().getQrCodeKey(), jsonObject.toString()), BarcodeFormat.QR_CODE, width, height, hintsMap);
                Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
                for (int x = 0; x < width; x++) {
                    for (int y = 0; y < height; y++) {
                        //bitmap.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);//guest_pass_background_color
//                            bitmap.setPixel(x, y, bitMatrix.get(x, y) ? Color.WHITE : ContextCompat.getColor(getActivity(), R.color.colorPrimary));
                        if (getActivity() != null && !getActivity().isFinishing())
                            bitmap.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : ContextCompat.getColor(getActivity(), R.color.colorPrimary));
                    }
                }

颤振版本:

    Map<String, dynamic> myData = {
  'type': 1,
  'content': connection.email, //TODO check
  'display_name': "${(await contentManager.getUserInfo()).identifier}",
  //'collaborator': connection.name, //TODO check
  'mac': await Utilities.getDeviceMac(),
};

String encodedJson = jsonEncode(myData);

标签: androidflutterqr-code

解决方案


您可以使用 Flutter 插件在您的应用中添加 QR 扫描功能

参考这个链接

 Future _scanBytes() async {
    try {
      String barcode = await scanner.scan();
      setState(() => this.barcode = barcode);
    } on PlatformException catch (e) {
      if (e.code == scanner.CameraAccessDenied) {
        setState(() {
          this.barcode = 'The user did not grant the camera permission!';
        });
      } else {
        setState(() => this.barcode = 'Unknown error: $e');
      }
    } on FormatException{
      setState(() => this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)');
    } catch (e) {
      setState(() => this.barcode = 'Unknown error: $e');
    }
    print("barcode "+barcode);
  }

推荐阅读