. Net qr code generation (ThoughtWorks. QRCode), qrcode QR code generator

Source: Internet
Author: User

. 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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.