欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#如何用ThoughtWorks生成二维码

程序员文章站 2022-04-05 17:02:44
在现在的项目中,较多的使用到二维码,前面介绍过一篇使用gma生成二维码的操作,现在介绍一个第三方组件,主要介绍生成二维码,二维码的解析,以及对二维码的相关信息的选择,现在介绍thoughtworks用...

       在现在的项目中,较多的使用到二维码,前面介绍过一篇使用gma生成二维码的操作,现在介绍一个第三方组件,主要介绍生成二维码,二维码的解析,以及对二维码的相关信息的选择,现在介绍thoughtworks用于生成二维码,本随笔采用.net4.5和c#6.0语法,也可以进行查看thoughtworks的底层源码。

1.生成二维码:

    /// <summary>
    /// 生成二维码
    /// </summary>
    /// <param name="content">带生成二维码的字符串</param>
    /// <param name="path">路径</param>
    /// <returns></returns>
    public static string createhoughtworksqrcode(string content, string path)
    {
      if (string.isnullorempty(content))
      {
        throw new argumentnullexception(content);
      }
      if (string.isnullorempty(path))
      {
        throw new argumentnullexception(path);
      }
      var qrcodeencoder = new qrcodeencoder
      {
        qrcodeencodemode = qrcodeencoder.encode_mode.byte,
        qrcodescale = 4,
        qrcodeversion = 8,
        qrcodeerrorcorrect = qrcodeencoder.error_correction.m
      };
      image image = qrcodeencoder.encode(content);
      var filename = datetime.now.tostring("yyyymmddhhmmssfff") + ".jpg";
      var filepath = string.format("{0}{1}", path, filename);
      filestream fs = null;
      try
      {
        fs = new filestream(filepath, filemode.openorcreate, fileaccess.write);
        image.save(fs, system.drawing.imaging.imageformat.jpeg);
      }
      catch (ioexception ex)
      {
        throw new ioexception(ex.message);
      }
      finally
      {
        if (fs != null) fs.close();
        image.dispose();
      }
      return codedecoder(filepath);
    }

2.解析二维码:

    /// <summary>
    /// 二维码解码
    /// </summary>
    /// <param name="filepath">图片路径</param>
    /// <returns></returns>
    public static string codedecoder(string filepath)
    {
      if (string.isnullorempty(filepath))
      {
        throw new argumentnullexception(filepath);
      }
      try
      {
        if (!system.io.file.exists(filepath))
          return null;
        var mybitmap = new bitmap(image.fromfile(filepath));
        var decoder = new qrcodedecoder();
        var decodedstring = decoder.decode(new qrcodebitmapimage(mybitmap));
        return decodedstring;
      }
      catch (exception ex)
      {
        throw new exception(ex.message);
      }
    }

3.选择生成的二维码参数:

    /// <summary>
    /// 选择生成二维码的相关类型
    /// <param name="strdata">要生成的文字或者数字,支持中文。如: "4408810820 深圳-广州" 或者:4444444444</param>
    /// <param name="qrencoding">三种尺寸:byte ,alpha_numeric,numeric</param>
    /// <param name="level">大小:l m q h</param>
    /// <param name="version">版本:如 8</param>
    /// <param name="scale">比例:如 4</param>
    /// <returns></returns>
    /// </summary>
    public void createcode_choose(string strdata, string qrencoding, string level, int version, int scale)
    {
      var qrcodeencoder = new qrcodeencoder();
      var encoding = qrencoding;
      switch (encoding)
      {
        case "byte":
          qrcodeencoder.qrcodeencodemode = qrcodeencoder.encode_mode.byte;
          break;
        case "alphanumeric":
          qrcodeencoder.qrcodeencodemode = qrcodeencoder.encode_mode.alpha_numeric;
          break;
        case "numeric":
          qrcodeencoder.qrcodeencodemode = qrcodeencoder.encode_mode.numeric;
          break;
        default:
          qrcodeencoder.qrcodeencodemode = qrcodeencoder.encode_mode.byte;
          break;
      }

      qrcodeencoder.qrcodescale = scale;
      qrcodeencoder.qrcodeversion = version;
      switch (level)
      {
        case "l":
          qrcodeencoder.qrcodeerrorcorrect = qrcodeencoder.error_correction.l;
          break;
        case "m":
          qrcodeencoder.qrcodeerrorcorrect = qrcodeencoder.error_correction.m;
          break;
        case "q":
          qrcodeencoder.qrcodeerrorcorrect = qrcodeencoder.error_correction.q;
          break;
        default:
          qrcodeencoder.qrcodeerrorcorrect = qrcodeencoder.error_correction.h;
          break;
      }
      image image = null;
      filestream fs = null;
      try
      {
        //文字生成图片
        image = qrcodeencoder.encode(strdata);
        var filename = datetime.now.tostring("yyyymmddhhmmssfff") + ".jpg";
        var filepath = httpcontext.current.server.mappath(@"~\upload") + "\\" + filename;
        fs = new filestream(filepath, filemode.openorcreate, fileaccess.write);
        image.save(fs, system.drawing.imaging.imageformat.jpeg);
      }
      catch (ioexception ex)
      {
        throw new ioexception(ex.message);
      }
      finally
      {
        if (fs != null) fs.close();
        if (image != null) image.dispose();
      }
    }

以上就是c#如何用thoughtworks生成二维码的详细内容,更多关于c#生成二维码的资料请关注其它相关文章!