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

用MVC实现条形码和二维码

首先在视图里写以下代码

 1 @{
 2     Layout = null;
 3 }
 4 
 5 <!DOCTYPE html>
 6 
 7 <html>
 8 <head>
 9     <meta name="viewport" content="width=device-width" />
10     <title>Index</title>
11     <script src="~/Scripts/jquery-3.3.1.js"></script>
12 </head>
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>
23 <script>
24     //二维码方法跳转
25     function Er() {
26         $.ajax({
27             url: "/Show/Er",
28             data: { text: $("#txt").val() },
29             dataType: "text",
30             success: function (data) {
31                 $("#erwei").attr("src", data);
32             }
33         })
34     }
35     //条形码方法跳转
36     function tiao() {
37         $.ajax({
38             url: "/Show/Tiao",
39             data: { text: $("#txt").val() },
40             dataType: "text",
41             success: function (data) {
42                 $("#tx").attr("src", data);
43             }
44         })
45     }
46 </script>

分别通过onclick事件跳转到控制器

实现条形码和二维码的生成

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

zxing.dll  文件

文件下载位置

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

以下是视图的功能实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using ZXing; //引用zxing.dll文件之后
using ZXing.Common;
using ZXing.QrCode;
using ZXing.QrCode.Internal;

namespace Ex8.Controllers
{
    public class ShowController : Controller
    {
        // GET: Show
        public ActionResult Index()
        {
            return View();
        }
        public  string Er(string text)
        {
            int width = 60; int height = 60; //定义变量 二维码的宽和高
            Random rd = new Random(10);  //随机数
            string time = DateTime.Now.ToString("yyyyMMdd")+"erwei"; 
            string path = Server.MapPath("~/Images" + "//" + time + ".Png"); //二维码图
            string path1 = $"http://localhost:53183/Images/{time}" + ".Png";
            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;
            Bitmap map = writer.Write(text);
            map.Save(path, ImageFormat.Png);
            return path1;
        }
        public string Tiao(string text)
        {
            int width = 80; int height = 60;
            Random rd = new Random(10);
            string time = DateTime.Now.ToString("yyyyMMdd")+rd.Next().ToString();
            string path = Server.MapPath("~/Images" + "//" + time + ".Png");
            string path1 = $"http://localhost:53183/Images/{time}" + ".Png";
            BarcodeWriter writer = new BarcodeWriter();
            //使用ITF 格式,不能被现在常用的支付宝、微信扫出来
            //如果想生成可识别的可以使用 CODE_128 格式
            //writer.Format = BarcodeFormat.ITF;
            writer.Format = BarcodeFormat.CODE_39;
            EncodingOptions options = new EncodingOptions()
            {
                Width = width,
                Height = height,
                Margin = 2
            };
            writer.Options = options;
            Bitmap map = writer.Write(text);
            map.Save(path, ImageFormat.Png);
            return path1;
        }

    }
}

以上就是条形码和二维码的简单实现  

  

猜你喜欢

转载自www.cnblogs.com/jian1125/p/10563860.html