生成带有卡号,宽度和高度的QR码

时间:2019-06-10 12:15:57

标签: java c# qr-code zxing

我想转换现有的C#QRCodeEncoder代码Java。在C#中生成QR码时,该方法采用cardNumberwidthlength参数,并将QR码作为字符串返回。签名需要保持不变。我已经检查了ZXing库,但是找不到合适的方法。转换为Java时应该使用哪个库?

C#代码

private static string generateQRCode(string cardNumber, int width, int height) {
        string result = "";

        QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
        qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;

        int scale = 8;
        try
        {
            if (width / 22 > 8)
                scale = width / 22;

        }
        catch { }
        qrCodeEncoder.QRCodeScale = scale;
        int version = 3;
        qrCodeEncoder.QRCodeVersion = version;

        string errorCorrect = "M";
        if (errorCorrect == "L")
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
        else if (errorCorrect == "M")
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
        else if (errorCorrect == "Q")
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;
        else if (errorCorrect == "H")
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;

        try
        {
            // Convert the image to byte[]
            MemoryStream stream = new MemoryStream();
            qrCodeEncoder.Encode(cardNumber).Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] imageBytes = stream.ToArray();

            // Convert byte[] to Base64 String
            result = Convert.ToBase64String(imageBytes);
        }
        catch (Exception ex) { }
        return result;
}

0 个答案:

没有答案