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

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

条形码: 

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

 

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

 

用MVC实现条形码和二维码

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

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

在视图里写以下代码

第一、先写出 条形码和二维码中我们要出现的内容

 

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

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

 

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

 

以下有图解

 

13 <body>
14     <div>
     //输入文字 15 <input id="txt" type="text" />
   //功能按钮 16 <input id="Button1" type="button" value="生成条形码图片" οnclick="tiao()" /> @*条形码按钮*@ 17 <input id="Button1" type="button" value="生成二维码图片" οnclick="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实现的功能

我能帮大家的就这么多了

 

转载于:https://www.cnblogs.com/jian1125/p/10563860.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值