. Net qr code generation (ThoughtWorks. QRCode), qrcode QR code generator
Reference ThoughtWorks. QRCode. dll (available in source code)
1. Simple QR code generation and decoding:
// Method for generating a QR code: private void CreateCode_Simple (string nr) {QRCodeEncoder qrCodeEncoder = new QRCodeEncoder (); qrCodeEncoder. QRCodeEncodeMode = QRCodeEncoder. ENCODE_MODE.BYTE; qrCodeEncoder. QRCodeScale = 4; qrCodeEncoder. QRCodeVersion = 8; qrCodeEncoder. QRCodeErrorCorrect = QRCodeEncoder. ERROR_CORRECTION.M; // System. drawing. image image = qrCodeEncoder. encode ("4408810820 Shenzhen-Guangzhou Xiaojiang"); System. drawing. image Image = qrCodeEncoder. encode (nr); string filename = DateTime. now. toString ("yyyymmddhhmmssfff "). toString () + ". jpg "; string filepath = Server. mapPath (@"~ \ Upload ") +" \ "+ filename; System. IO. fileStream fs = new System. IO. fileStream (filepath, System. IO. fileMode. openOrCreate, System. IO. fileAccess. write); image. save (fs, System. drawing. imaging. imageFormat. jpeg); fs. close (); image. dispose (); // QR code decoding var codeDecoder = CodeDecoder (filepath );} /// <summary> /// QR code decoding /// </summary> /// <param name = "filePath"> image path </param> /// <returns> </returns> public st Ring CodeDecoder (string filePath) {if (! System. IO. file. exists (filePath) return null; Bitmap myBitmap = new Bitmap (Image. fromFile (filePath); QRCodeDecoder decoder = new QRCodeDecoder (); string decodedString = decoder. decode (new QRCodeBitmapImage (myBitmap); return decodedString ;}
2. Source Code demonstration, a relatively complex generation method:
Interface effect:
Front-end code:
@ {Layout = null ;}<! DOCTYPE html>
Background:
/// Generate a QR code /// </summary> /// <param name = "strData"> the text or number to be generated. Chinese characters are supported. For example, "4408810820 Shenzhen-Guangzhou" Or: 4444444444 </param> // <param name = "qrEncoding"> three dimensions: BYTE, ALPHA_NUMERIC, NUMERIC </param> /// <param name = "level"> size: l m q h </param> /// <param name = "version"> version: for example, 8 </param> /// <param name = "scale"> ratio: for example, 4 </param> /// <returns> </returns> public ActionResult CreateCode_Choose (string strData, string qrEncoding, string level, int version, int scale) {QRCodeEncoder qrCodeEncoder = New QRCodeEncoder (); string encoding = qrEncoding; switch (encoding) {case "Byte": qrCodeEncoder. QRCodeEncodeMode = QRCodeEncoder. ENCODE_MODE.BYTE; break; case "AlphaNumeric": qrCodeEncoder. QRCodeEncodeMode = QRCodeEncoder. ENCODE_MODE.ALPHA_NUMERIC; break; case "Numeric": qrCodeEncoder. QRCodeEncodeMode = QRCodeEncoder. ENCODE_MODE.NUMERIC; break; default: qrCodeEncoder. QRCodeEncodeMode = QRCode Encoder. ENCODE_MODE.BYTE; break;} qrCodeEncoder. QRCodeScale = scale; qrCodeEncoder. QRCodeVersion = version; switch (level) {case "L": qrCodeEncoder. QRCodeErrorCorrect = QRCodeEncoder. ERROR_CORRECTION.L; break; case "M": qrCodeEncoder. QRCodeErrorCorrect = QRCodeEncoder. ERROR_CORRECTION.M; break; case "Q": qrCodeEncoder. QRCodeErrorCorrect = QRCodeEncoder. ERROR_CORRECTION.Q; break; default: qrCodeE Ncoder. QRCodeErrorCorrect = QRCodeEncoder. ERROR_CORRECTION.H; break;} // text-generated Image image Image = qrCodeEncoder. encode (strData); string filename = DateTime. now. toString ("yyyymmddhhmmssfff "). toString () + ". jpg "; string filepath = Server. mapPath (@"~ \ Upload ") +" \ "+ filename; // if the folder does not exist, create // if (! Directory. exists (filepath) // Directory. createDirectory (filepath); System. IO. fileStream fs = new System. IO. fileStream (filepath, System. IO. fileMode. openOrCreate, System. IO. fileAccess. write); image. save (fs, System. drawing. imaging. imageFormat. jpeg); fs. close (); image. dispose (); return Content (@ "/Upload/" + filename );}
Source code demo download: Click to download