码迷,mamicode.com
首页 > Web开发 > 详细

.Net mvc 使用QRCode生成和解析二维码

时间:2014-10-29 14:52:53      阅读:646      评论:0      收藏:0      [点我收藏+]

标签: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>


效果:

bubuko.com,布布扣bubuko.com,布布扣




源码下载地址:http://download.csdn.net/detail/pukuimin1226/8095417



.Net mvc 使用QRCode生成和解析二维码

标签:des   style   blog   http   io   os   ar   使用   for   

原文地址:http://blog.csdn.net/pukuimin1226/article/details/40585839

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!