.Net mvc 使用QRCode產生和解析二維碼

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   os   ar   使用   for   

//index 控制器中的代碼

using System;using System.Collections.Generic;using System.Drawing;using System.Linq;using System.Web;using System.Web.Mvc;using ThoughtWorks.QRCode.Codec;namespace MyQrCode.Controllers{    public class IndexController : Controller    {        //        // GET: /Index/        public ActionResult Index()        {            return View();        }        /// <summary>        /// 產生二維碼        /// </summary>        /// <param name="url">連結地址資訊</param>        /// <param name="logo">中間的logo,加了後可能會解析失敗</param>        /// <returns></returns>        public ActionResult QrCode(string url = "http://blog.csdn.net/pukuimin1226", string logo = "")        {            string enCodeString = url;//擷取到需要轉化為二維碼的字元            QRCodeEncoder codeEncoder = new QRCodeEncoder();//建立一個編碼器            codeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;            codeEncoder.QRCodeScale = 4;            codeEncoder.QRCodeVersion = 8;            codeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;            Bitmap pic = codeEncoder.Encode(url, System.Text.Encoding.UTF8);            string filePath = "/qrcode.jpg";            if (logo != null && logo != "")            {                CombinImage(pic, Server.MapPath(logo)).Save(Server.MapPath(filePath), System.Drawing.Imaging.ImageFormat.Jpeg);//加了logo,不建議使用            }            else pic.Save(Server.MapPath(filePath), System.Drawing.Imaging.ImageFormat.Jpeg);            if (pic != null) pic.Dispose();            return File(filePath, "image/jpeg");        }        /// <summary>        /// 解析二維碼,輸出url資訊        /// </summary>        /// <param name="filename">二維碼相對路徑</param>        /// <returns></returns>        public ActionResult DeQrCode(string filename="/qrcode.jpg")        {            Bitmap pic = new Bitmap(Server.MapPath(filename));//            QRCodeDecoder qrDecoder = new QRCodeDecoder();//建立一個解碼器            string msg = qrDecoder.decode(new ThoughtWorks.QRCode.Codec.Data.QRCodeBitmapImage(pic), System.Text.Encoding.UTF8);//解碼返回一個字串            if (pic != null) pic.Dispose();            return Content(msg);        }        #region 這部分代碼是網上找的,將產生的二維碼中間加入logo圖片,可能造成掃碼解析失敗,不建議使用        /// <summary>          /// Resize圖片          /// </summary>          /// <param name="bmp">原始Bitmap</param>          /// <param name="newW">新的寬度</param>          /// <param name="newH">新的高度</param>          /// <param name="Mode">保留著,暫時未用</param>          /// <returns>處理以後的圖片</returns>          public static Image KiResizeImage(Image bmp, int newW, int newH, int Mode)        {            try            {                Bitmap b = new Bitmap(newW, newH);                Graphics g = Graphics.FromImage(b);                // 插值演算法的品質                  g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;                g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);                g.Dispose();                return b;            }            catch            {                return null;            }        }        /// <summary>          /// 調用此函數後使此兩種圖片合并,類似相簿,有個          /// 背景圖,中間貼自己的靶心圖表片          /// </summary>          /// <param name="imgBack">粘貼的源圖片</param>          /// <param name="destImg">粘貼的靶心圖表片</param>          public static Image CombinImage(Image imgBack, string destImg)        {            int logo_w_h = 65;            Image img = Image.FromFile(destImg);        //照片圖片                if (img.Height != logo_w_h || img.Width != logo_w_h)            {                img = KiResizeImage(img, logo_w_h, logo_w_h, 0);            }            Graphics g = Graphics.FromImage(imgBack);            g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height);      //g.DrawImage(imgBack, 0, 0, 相框寬, 相框高);               //g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 + 1, imgBack.Width / 2 - img.Width / 2 + 1, img.Width - 2, img.Width - 2);//相片四周刷一層黑色邊框              //g.DrawImage(img, 照片與相框的左邊距, 照片與相框的上邊距, 照片寬, 照片高);              g.DrawImage(img, imgBack.Width / 2 - img.Width / 2, imgBack.Width / 2 - img.Width / 2, img.Width, img.Height);            GC.Collect();            return imgBack;        }         #endregion    }}


//index/index 視圖


@{    ViewBag.Title = "二維碼產生與解析 樣本";    Layout = "~/Views/Shared/_Layout.cshtml";}<h2>Index</h2><a target="_blank" href="/Index/QrCode">產生二維碼,後面可加參數 ?url=</a><br /><a target="_blank" href="/Index/DeQrCode">解析二維碼</a><br /><div style="margin:100px auto auto 200px;">    用或360的掃一掃,能轉到對應的網址:<br/><br />    <img src="/Index/QrCode?url=http://m.baidu.com/" /></div>


效果:





源碼:http://download.csdn.net/detail/pukuimin1226/8095417



.Net mvc 使用QRCode產生和解析二維碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.