博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
生成二维码(支持中文)
阅读量:6068 次
发布时间:2019-06-20

本文共 5640 字,大约阅读时间需要 18 分钟。

using System;using System.Collections.Generic;using System.Text;//using Aspose.BarCode;using System.Drawing.Imaging;//using ThoughtWorks.QRCode.Codec;//using System.Drawing.Imaging;using ThoughtWorks.QRCode.Codec;using ThoughtWorks.QRCode.Codec.Data;using ThoughtWorks.QRCode.Codec.Util;namespace QrCode{    public static class Qr    {        public static string toUrl2(string str)        {            try            {                Encoding utf8 = Encoding.UTF8;                Encoding gb2312 = Encoding.GetEncoding("GB2312");                byte[] unicodeBytes = gb2312.GetBytes(str);                byte[] asciiBytes = Encoding.Convert(gb2312, utf8, unicodeBytes);                char[] asciiChars = new char[utf8.GetCharCount(asciiBytes, 0, asciiBytes.Length)];                utf8.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);                string result = new string(asciiChars);                return result;            }            catch            {                return "";            }        }        public static string toUrl(string str)        {            try            {                Encoding utf8 = Encoding.UTF8;                Encoding gb2312 = Encoding.GetEncoding("GB2312");                byte[] unicodeBytes = utf8.GetBytes(str);                byte[] asciiBytes = Encoding.Convert(utf8, gb2312, unicodeBytes);                char[] asciiChars = new char[utf8.GetCharCount(asciiBytes, 0, unicodeBytes.Length)];                utf8.GetChars(asciiBytes, 0, unicodeBytes.Length, asciiChars, 0);                string result = new string(asciiChars);                return result;            }            catch            {                return "";            }        }        public static bool OutQrInfo(string Info, string picFileName, out string errMsg)        {            errMsg = null;            try            {                QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();                qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;                qrCodeEncoder.QRCodeScale = 4;                qrCodeEncoder.QRCodeVersion = 8;                qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;                System.Drawing.Bitmap image = qrCodeEncoder.Encode(Info, Encoding.UTF8);                string filepath= AppDomain.CurrentDomain.BaseDirectory;                image.Save(filepath + "/QRCodeImg/" + picFileName); //, System.Drawing.Imaging.ImageFormat.Gif);                return true;            }            catch (Exception te)            {                errMsg = te.Message;                return false;            }        }        //public static string toUrlX(string s)        //{        //    return s != null ? HttpContext.Current.Server.UrlEncode(s) : null;        //}        public static string OutQr4Url(string url, string caption)        {            string turl = string.Format("MEBKM:{0};URL:{1};", (caption), (url));            return turl;            //string errMsg;            //OutQrInfo(url,  out errMsg);        }        public static string OutQr4SMS(string Mobile, string Info)        {            string url = string.Format("SMSTO:{0}:{1}", (Mobile), (Info));            return url;            //string errMsg;            //OutQrInfo(url,  out errMsg);        }        public static string OutQr4MMS(string Mobile, string Info)        {            string url = string.Format("MMSTO:{0}:{1}", (Mobile), (Info));            return url;            //string errMsg;            //OutQrInfo(url,  out errMsg);        }        public static string OutQr4Email(string Email, string Caption, string Body)        {            string url = string.Format("MATMSG:TO:{0};SUB:{1};BODY:{2};", (Email), (Caption), (Body));            return url;            //string errMsg;            //OutQrInfo(url,  out errMsg);        }        public static string OutQr4WiFi(string WEP_WPA, string SID, string Password)        {            string url = string.Format("WIFI:T:{0};S:{1};P:{2};", (WEP_WPA), (SID), (Password));            return url;            //string errMsg;            //OutQrInfo(url,  out errMsg);        }        public static string OutQr4Card(string ModelCode,string Factory, string Sex, string RimStyle, string Price)        {            string url = string.Format("GlasessModel: Code:{0};Factory:{3};nSex:{2};Rim style:{1};Price:{4};", (ModelCode), (Factory), (Sex), RimStyle, Price);            return url;        }        public static string OutQr4Telephone(string Phone)        {            string url = string.Format("TEL:{0}",                (Phone));            return url;            //string errMsg;            //OutQrInfo(url,  out errMsg);        }        public static string OutQr4App(string AppID)        {            string url = string.Format("market://details?id={0}",                toUrl(AppID));            return url;            //string errMsg;            //OutQrInfo(url,  out errMsg);        }        public static string OutQr4Location(decimal longitude, decimal latitude, decimal accuracy)        {            string url = string.Format("GEO:{0},{1},{2}", longitude, latitude, accuracy);            return url;            //    string errMsg;            //    OutQrInfo(url,  out errMsg);            //}        }    }}

以上是这个生成二维码的方法类。

下面是生成其中一个类型的方法:

string filename = DateTime.Now.ToFileTime().ToString()+".jpg";            string err = string.Empty;            string s = Qr.OutQr4Card(txtModelCode.Text,txtFactory.Text,Sex,RimStyle,txtPrice.Text);            Qr.OutQrInfo(s, filename, out err);

 

另外还要引用ThoughtWorks.QRCode.dll文件。

转载于:https://www.cnblogs.com/chenhuiling1991/p/QRCode.html

你可能感兴趣的文章
Python黑客编程2 入门demo--zip暴力破解
查看>>
必看 :大数据挖掘中易犯的11大错误
查看>>
宿主系统为Ubuntu 14,CentOS 6.5 安装VirtualBox增强工具失败:Building the OpenGL support module[FAILED]...
查看>>
MVC学习系列14--Bundling And Minification【捆绑和压缩】--翻译国外大牛的文章
查看>>
Android实战简易教程-第十枪(画廊组件Gallery有用研究)
查看>>
POJ 2965:The Pilots Brothers' refrigerator
查看>>
Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy
查看>>
无法启动此程序,因为计算机中丢失 api-ms-win-crt-stdio-l1-1-0.dll 解决
查看>>
java获取指定文件夹下的所有文件名
查看>>
weex 项目开发(一) weex create project 与 weex init project 的区别
查看>>
PCH简单介绍
查看>>
c#实现用SQL池(多线程),定时批量执行SQL语句
查看>>
【译】Immutable.js: Map - 5
查看>>
【移动端 Web】怎么循序渐进地开发一个移动端页面
查看>>
Python把同一个对象循环赋值给另外一个变量
查看>>
SQL优化工具SQLAdvisor使用(转)
查看>>
springboot~为Money类型添加最大值和最小值的注解校验
查看>>
[Javascript Crocks] Safely Access Nested Object Properties with `propPath`
查看>>
微信小程序——button, swiper等默认样式更改
查看>>
PHP-CPP开发扩展(六)
查看>>