C# 条码和二维码

 C# 条码和二维码

条码 = UserCodeImageHelper.Instance.MakeBarCodeImage(nBID, 字符串);
二维码 = UserCodeImageHelper.Instance.MakeQRCodeImage(nBID, 字符串);
字符串:支持大小写字母,数字,某些特殊符号

 

=====================================================================================

生成二维码

引入ZXing.Net

 

=====================================================================================

 

后端代码

 

public static class ZxingCodeHelper
    {
        /// <summary>
        /// 生成二维码,返回Base64字符串
        /// </summary>
        /// <param name="text">内容</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns></returns>
        public static string CreateQRCodeBase64(string text = "内容为空", int width = 300, int height = 300)
        {
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            QrCodeEncodingOptions options = new QrCodeEncodingOptions()
            {
                DisableECI = true,//设置内容编码
                CharacterSet = "UTF-8",  //设置二维码的宽度和高度
                Width = width,
                Height = height,
                Margin = 1//设置二维码的边距,单位不是固定像素
            };
            writer.Options = options;
            string qrBase64 = string.Empty;
            using (Bitmap map = writer.Write(text))
            {
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                map.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                qrBase64 = Convert.ToBase64String(stream.GetBuffer());
            };
            return "data:image/png;base64," + qrBase64 + "";
        }
    }

 

=====================================================================================

 

前端Ajax请求

 

//创建第三方扫码支付(V1.1.2)接口
                url: "/ShopUnspay/CreateScanCodeUnspay",
                data: {
                    flowID: "F2019011015060413755",
                    amountStr: "0.01",
                    payType: "2",//支付类型 1:支付宝 2:微信 3:银联
                },
success: function (data) {

                    //---------------------------------------
                    alert("成功返回" + data);
                    //二维码
                    var obj = JSON.parse(data);
                    if (obj.code == "1") {
                        $('#BarCode').attr('src', obj.data["BarCode"]);
                        $('#QRCode').attr('src', obj.data["QRCode"]);
                        return;
                    }
 },
                error: function (data) {
                    alert("请求发送失败");
                    alert(JSON.stringify(data))
                    return;
                }

 

=====================================================================================

 

前端html赋值

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>ShopUsersTest</title>
</head>
<body>
    <button id="btnOK">点击确定</button>
    <div>
    </div>
    <img src="" alt="条形码" id="BarCode" />
    <img src="" alt="二维码" id="QRCode" />
</body>
</html>

 

posted on 2022-10-31 17:57  Jankie1122  阅读(65)  评论(0编辑  收藏  举报