开发ASP.NET MVC 开发名片二维码生成工具 (原创)
在网上找了很多,都只能生成网址,不能生成名片二维码,于是自己动手。
第一步,写视图界面,主要代码如下:
<script type="text/javascript"> function SubmitURL() { $("#frmMakeQrURLCode").submit(); } function SubmitQrCode() { $("#frmMakeQrCode").submit(); } </script> <div class="jumbotron"> <h1>名片二维码工具</h1> <p class="lead"> <form id="frmMakeQrCode" action="/Home/MakeQrCode" method="post"> <table border="0"> <tbody><tr> <th colspan="3">名片二维码信息</th> </tr> <tr> <td>姓名:</td> <td><input type="text" id="txtUsername" name="Username"></td> </tr> <tr> <td>电话号码:</td> <td><input type="text" id="txtPhone" name="Phone"></td> <td colspan="2"><input type="button" id="btnSubmit" value="生成名片二维码" οnclick="SubmitQrCode()"></td> </tr> </tbody></table> </form> </p> </div>
第二步,写控制器,处理form的post请求。主要是处理vcard的字符串和生成png图片。主要代码:
[HttpPost] public ActionResult MakeQrCode() { string username = Request.Form["Username"]; string phone = Request.Form["Phone"]; StringBuilder Vcard = new StringBuilder(); Vcard.Append("BEGIN:VCARD"); Vcard.Append("FN:").Append(username).Append(";"); Vcard.Append("ORG:").Append("CVTE").Append(";"); Vcard.Append("TEL;WORK;VOICE:").Append(phone).Append(";"); //Vcard.Append("TEL;TYPE=VOICE,WORK;VALUE=text:").Append(phone).Append(";"); Vcard.Append("END:VCARD"); //结束 //这样写也可以 StringBuilder Vcard1 = new StringBuilder(); Vcard1.Append("BEGIN:VCARD"); Vcard1.Append(System.Environment.NewLine); Vcard1.Append("VERSION:3.0") ; Vcard1.Append(System.Environment.NewLine); Vcard1.Append("FN:" + username) ; //Vcard1.Append("TEL;WORK;VOICE:" + phone ); Vcard1.Append(System.Environment.NewLine); Vcard1.Append("TEL;TYPE=VOICE,WORK;VALUE=text:" + phone) ; Vcard1.Append(System.Environment.NewLine); Vcard1.Append("ORG:CVTE") ; Vcard1.Append(System.Environment.NewLine); Vcard1.Append("END:VCARD"); //string vcard = @"BEGIN:VCARD" + System.Environment.NewLine + "VERSION:3.0" + System.Environment.NewLine + "FN:" + username + "CVTE" + System.Environment.NewLine + "TEL;TYPE=VOICE,WORK;VALUE=text:" + phone + "" + System.Environment.NewLine + "END:VCARD"; //string imageUrl = string.Format(@"<img src=""/Home/BarcodeImage?barcodeText={0}"" />",Server.HtmlEncode( vcard) ); string imageUrl = string.Format(@"<img src=""/Home/BarcodeImage?barcodeText={0}"" />", Server.HtmlEncode(Vcard.ToString())); return BarcodeImage(Vcard1.ToString()); } [HttpPost] public MvcHtmlString MakeQrURLCode() { string url = Request.Form["URL"]; string imageUrl = "<img src=\"/Home/BarcodeImage?barcodeText=" + url + "\"/>"; return new MvcHtmlString(imageUrl); } public ActionResult BarcodeImage(String barcodeText) { QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H); QrCode qrCode = new QrCode(); qrEncoder.TryEncode(barcodeText, out qrCode); GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(4, QuietZoneModules.Four), Brushes.Black, Brushes.White); Stream memoryStream = new MemoryStream(); renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, memoryStream); memoryStream.Position = 0; var resultStream = new FileStreamResult(memoryStream, "image/png"); resultStream.FileDownloadName = "cvteQR.png"; return resultStream; }
完整代码下载: Code download