zoukankan      html  css  js  c++  java
  • 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="生成条形码图片" 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实现的功能

    我能帮大家的就这么多了

  • 相关阅读:
    cocoaPod 使用
    iOS 7 开启侧滑效果
    数组排序
    JSON 保存到本地
    使用Android Studio时,Gradle同步失败的原因之一
    JDBC连接SQL Server数据库出现“通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败”错误解决办法
    tomcat启动命令行窗口出现乱码的解决方法
    eclipse中启动tomcat后,浏览器中出现404错误的解决办法
    Android Studio 出现Activity supporting ACTION_VIEW is not set as BROWSABLE错误
    解决AndroidStudio的错误:Installation failed with message Failed to finalize session : INSTALL_FAILED_INVALID_APK
  • 原文地址:https://www.cnblogs.com/jian1125/p/10563860.html
Copyright © 2011-2022 走看看