Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use APS.NET MVC4 to generate QR Code

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to use APS.NET MVC4 to generate QR codes". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use APS.NET MVC4 to generate QR codes.

I. View

@ {Layout = null;} Index $(function () {/ / generate $('# createBtn') .click (function () {createImage ();}); / / parse $('# analysisBtn') .click (function () {analysisImage ();});}); / / generate QR code function createImage () {var content = $('# content'). Val () / / ajax starts $.post ("/ ORCode/GetORImage/", {"content": content}, function (data) {$('# imgDiv') .empty (); $('# imgDiv') .append (')

); $("# ORImage"). Attr ("src", data);}); / / end of ajax} / / parse the QR code function analysisImage () {/ / get the picture name var imageUrl = $('# ORImage'). Attr ("src"); var array = imageUrl.split ('/'); var imageName = array [array.length-1] / for example: 123.jpg / / ajax starts with $.post ("/ ORCode/GetORImageContent/", {"imageName": imageName}, function (data) {$('# ORCodeContent'). Empty (); $('# ORCodeContent') .text (data);}); / / ajax ends}

Second, controller

Using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using Common;using System.Drawing;namespace QRCodeDemo.Controllers {public class ORCodeController: Controller {/ GET: / ORCode/ public ActionResult Index () {return View ();} [HttpPost] public ActionResult GetORImage (string content) {/ / if (string.IsNullOrEmpty (content)) / / {/ / return Content ("); / /} string timeStr = DateTime.Now.ToFileTime (). ToString () Bitmap bitmap = QRCodeOp.QRCodeEncoderUtil (content); string fileName = Server.MapPath ("~") + "Content\\ Images\\ QRImages\\" + timeStr + ".jpg"; bitmap.Save (fileName); / / Save bitmap string imageUrl = "/ Content/Images/QRImages/" + timeStr + ".jpg"; / / display picture return Content (imageUrl) } [HttpPost] public ActionResult GetORImageContent (string imageName) {string fileUrl = Server.MapPath ("~") + "Content\\ Images\\ QRImages\" + imageName; Bitmap bitMap = new Bitmap (fileUrl); string content = QRCodeOp.QRCodeDecoderUtil (bitMap); return Content (content);}

3D code generation tool class

Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using ThoughtWorks.QRCode.Codec;using ThoughtWorks.QRCode.Codec.Data;using System.Drawing;namespace Common {public class QRCodeOp {/ generate the QR code / the content to be encoded / return the 2D code bitmap public static Bitmap QRCodeEncoderUtil (string qrCodeContent) {QRCodeEncoder qrCodeEncoder = new QRCodeEncoder (); qrCodeEncoder.QRCodeVersion = 0 Bitmap img = qrCodeEncoder.Encode (qrCodeContent, Encoding.UTF8); / / specify utf-8 code, support Chinese return img;} / parse QR code / the parsed string public static string QRCodeDecoderUtil (Bitmap bitmap) {QRCodeDecoder decoder = new QRCodeDecoder (); string decodedString = decoder.decode (new QRCodeBitmapImage (bitmap), Encoding.UTF8) / / specify utf-8 code, which supports Chinese return decodedString;}

At this point, I believe you have a deeper understanding of "how to use APS.NET MVC4 to generate QR codes". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report