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實現的功能

我能幫你們的就這麼多了

相關文章
相關標籤/搜索