C#运用QRCode生成二维码

  在现在的项目中,较多的使用到二维码,现在介绍一个第三方组件,用于生成二维码。

成都创新互联公司是专业的雨城网站建设公司,雨城接单;提供网站建设、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行雨城网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

  1. 生成二维码:

        /// 
        /// 生成二维码
        /// 
        /// 带生成二维码的字符串
        /// 路径
        /// 
        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 p_w_picpath = 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);
                p_w_picpath.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (IOException ex)
            {
                throw new IOException(ex.Message);
            }
            finally
            {
                if (fs != null) fs.Close();
                p_w_picpath.Dispose();
            }
            return CodeDecoder(filepath);
        }

2.解析二维码:

        /// 
        /// 二维码解码
        /// 
        /// 图片路径
        /// 
        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.选择生成的二维码参数:

        /// 
        /// 选择生成二维码的相关类型
        /// 要生成的文字或者数字,支持中文。如: "4408810820 深圳-广州" 或者:4444444444
        /// 三种尺寸:BYTE ,ALPHA_NUMERIC,NUMERIC
        /// 大小:L M Q H
        /// 版本:如 8
        /// 比例:如 4
        /// 
        /// 
        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 p_w_picpath = null;
            FileStream fs = null;
            try
            {
                //文字生成图片
                p_w_picpath = 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);
                p_w_picpath.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (IOException ex)
            {
                throw new IOException(ex.Message);
            }
            finally
            {
                if (fs != null) fs.Close();
                if (p_w_picpath != null) p_w_picpath.Dispose();
            }
        }

网站栏目:C#运用QRCode生成二维码
网页链接:http://abwzjs.com/article/pppgsh.html