使用Zxing 一维码

最近看到满大街的二维码扫码有惊喜,对二维码也有过一些了解,想看看到底是什么原理,在网上找了一些资料,自己弄了一个实例,采用的是MVC,贴出来分享一下

一维码生成

 Controller

 
        public ActionResult QRCodView()
        {
            return View();
        }

 

1   <div class="col-md-4">
2         <h2>一维码生成</h2>
3         <div><input type="number" maxlength="24" placeholder="请输入24位数字" id="text1" class="form-control" /><i id="btnGO1" class="button btn-primary h6">生成一维码</i></div>
4         <img id="BarCod" src="~/Image/e78b58d4-c4d4-4561-a1a0-854170419f73.jpg" class="img-thumbnail" />
5     </div>
View
1    $("#btnGO1").click(function () {
2         $.post("/Data/Create", { context: $("#text1").val() }, function (d) {
3             $("#BarCod").attr("src", d);
4         });
5     });
JS代码
   //一维码生成
        public string Create()
        {
            string context = Request.Form["context"];
            string imgPath = CommCor.BarCodeUnit.CreateBarCode(context, Server.MapPath("~/TempFiled/"));
            
            return "/TempFiled/" + imgPath;
        }
//引用命名空间
using ZXing;
using System.Drawing;
using ZXing.QrCode;
using ZXing.Common;
using System.Text.RegularExpressions;
using System.Drawing.Imaging;
using ZXing.QrCode.Internal;
using System.IO;




        /// <summary>
        /// 一维码生成
        /// </summary>
        /// <param name="contents"></param>
        public static string CreateBarCode(string contents, string tempPath)
        {
           
            EncodingOptions options = null;
            BarcodeWriter writer = null;
            options = new EncodingOptions
            {
                Width = 200,
                Height = 200
            };
            writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.ITF;
            writer.Options = options;
            Bitmap bitmap = writer.Write(contents);
            string fileName = Guid.NewGuid().ToString() + ".png";
            bitmap.Save(tempPath + fileName);
            return fileName;


        }
一维码生成核心代码

效果如上图

 

转载于:https://www.cnblogs.com/imeiba/p/5580198.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值