Two-dimensional Code simple demo
First, the View
@{Layout = null;} <! DOCTYPE html>Second, the 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);//saving bitmap string imageUrl = "/content/images/qrimages/" + timestr + ". jpg";//Display Picture Retu RN 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); }
}}
Three or two-dimensional 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 {///<summary>//Generate QR Code///</summary>//<param name= " Qrcodecontent "> What to encode </param>//<returns> return two-dimensional code bitmap </returns> public static Bitmap QRC Odeencoderutil (String qrcodecontent) {Qrcodeencoder Qrcodeencoder = new Qrcodeencoder (); qrcodeencoder.qrcodeversion = 0; Bitmap img = Qrcodeencoder.encode (qrcodecontent, Encoding.UTF8);//Specify UTF-8 encoding, support Chinese return img; }///<summary>//parsing QR code///</summary>//<param name= "bitmap" > QR code to parse Bitmap </param>////<returns> parsed string </returns> public static string Qrcodedecoderutil (Bitmap Bitmap {Qrcodedecoder decoder = new Qrcodedecoder (); String decodedstring = Decoder.decode (new Qrcodebitmapimage (bitmap), Encoding.UTF8);//Specify UTF-8 encoding, support Chinese return D ecodedstring; } }}
Four, the effect
Five, source code download
Aps.net MVC4 generate two-dimensional code simple demo source Download
Category: ASP. NET MVCTwo-dimensional Code simple demo