C#--条形码和二维码的简单实现

首先 简单的介绍一下条形码和二维码html

条形码: ajax

  条形码技术是在计算机应用中产生发展起来的一种普遍应用于商业、邮政、图书管理、仓储、工业生产过程控制、交通运输、包装、配送等领域的自动识别技术。它最先出如今20世纪40年代,是“由一组规则排列的条、空及其对应字符组成的,用以表示必定信息的标识”。

 

条形码自动识别系统由条形码标签、条形码生成设备、条形码识读器和计算机组成。
 
二维码:
  二维码又称二维条码,常见的二维码为QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型。

 

用MVC实现条形码和二维码微信

接下来呢,要介绍的是 借助 zxing.dll 实现二维码和条形码dom

首先我是用MVC来实现功能的ui

在视图里写如下代码编码

第1、先写出 条形码和二维码中咱们要出现的内容url

 

第2、写下功能按钮,定义onclick事件spa

分别是条形码和二维码的功能code

 

第三 、显示条形码和二维码orm

 

如下有图解

 

13 <body>
14     <div>
     //输入文字 15 <input id="txt" type="text" />
   //功能按钮 16 <input id="Button1" type="button" value="生成条形码图片" onclick="tiao()" /> @*条形码按钮*@ 17 <input id="Button1" type="button" value="生成二维码图片" onclick="Er()" /> @*二维码按钮*@
   //图片显示
18 <img src="" alt="" id="tx" /> 19 <img src="" alt="" id="erwei" /> 20 </div> 21 </body> 22 </html>

 

以后就是咱们用ajax调用onclick事件到控制器了

 1 23 <script>
 2 24     //二维码方法跳转
 3 25 function Er() {  4 26 $.ajax({  5 27             url: "/Show/Er",  6 28             data: { text: $("#txt").val() },  7 29             dataType: "text",  8 30 success: function (data) {  9 31                 $("#erwei").attr("src", data); 10 32 } 11 33 }) 12 34 } 13 35     //条形码方法跳转
14 36 function tiao() { 15 37 $.ajax({ 16 38             url: "/Show/Tiao", 17 39             data: { text: $("#txt").val() }, 18 40             dataType: "text", 19 41 success: function (data) { 20 42                 $("#tx").attr("src", data); 21 43 } 22 44 }) 23 45 } 24 46 </script>

上面是视图里面的简单代码

下面介绍一下控制器里面具体功能的实现

我是用 zxing.dll 实现的功能

二维码和条形码的生成须要引用

zxing.dll  文件

文件下载位置

https://files.cnblogs.com/files/jian1125/zxing.zip

话很少说直接上代码

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using System.Drawing; 7 using System.Drawing.Imaging; 8 using System.Text; 9 using ZXing; //引用zxing.dll文件以后
10 using ZXing.Common; 11 using ZXing.QrCode; 12 using ZXing.QrCode.Internal; 13 
14 namespace Ex8.Controllers 15 { 16     public class ShowController : Controller 17 { 18         // GET: Show
19         public ActionResult Index() 20 { 21             return View(); 22 } 23         public  string Er(string text) 24 { 25             int width = 60; int height = 60; //定义变量 二维码的宽和高
26             Random rd = new Random(10);  //随机数
27             string time = DateTime.Now.ToString("yyyyMMdd")+"erwei"; 28             string path = Server.MapPath("~/Images" + "//" + time + ".Png"); //二维码图
29             string path1 = $"http://localhost:53183/Images/{time}" + ".Png"; 30             BarcodeWriter writer = new BarcodeWriter(); 31             writer.Format = BarcodeFormat.QR_CODE; 32             QrCodeEncodingOptions options = new QrCodeEncodingOptions() 33 { 34                 DisableECI = true, 35 
36                  //设置内容编码
37                 CharacterSet = "UTF-8", 38                 //设置二维码的宽度和高度
39                 Width = width, 40                 Height = height, 41                 Margin = 1//设置二维码的边距,单位不是固定像素
42 }; 43 
44             writer.Options = options; 45             Bitmap map = writer.Write(text); 46 map.Save(path, ImageFormat.Png); 47             return path1; 48 } 49         public string Tiao(string text) 50 { 51             int width = 80; int height = 60; 52             Random rd = new Random(10); 53             string time = DateTime.Now.ToString("yyyyMMdd")+rd.Next().ToString(); 54             string path = Server.MapPath("~/Images" + "//" + time + ".Png"); 55             string path1 = $"http://localhost:53183/Images/{time}" + ".Png"; 56             BarcodeWriter writer = new BarcodeWriter(); 57             //使用ITF 格式,不能被如今经常使用的支付宝、微信扫出来
58             //若是想生成可识别的能够使用 CODE_128 格式
59             //writer.Format = BarcodeFormat.ITF;
60             writer.Format = BarcodeFormat.CODE_39; 61             EncodingOptions options = new EncodingOptions() 62 { 63                 Width = width, 64                 Height = height, 65                 Margin = 2
66 }; 67             writer.Options = options; 68             Bitmap map = writer.Write(text); 69 map.Save(path, ImageFormat.Png); 70             return path1; 71 } 72 
73 } 74 } 

 

以上的功能呢,咱们是借助zxing.dll实现的功能

我能帮你们的就这么多了

相关文章
相关标签/搜索