Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use APS.NET MVC4 to generate QR Code

Shulou Source: shulou.com Published: 2022-06-03 10:58:45 09月10日 Update

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!

Tags: 2D QR code generation bitmap content encoding picture utf-8 Chinese learning support practicality deeper interest name character string practicality reality tools Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei MariaDB Docker OPPO Reno Linux