In current projects, QR codes are mostly used. I introduced an operation of using Gma to generate QR codes. Now I will introduce a third-party component, which mainly introduces the generation of QR codes, the analysis of QR codes, and the selection of related information of QR codes. Now I will introduce ThoughtWorks for generating QR codes. This essay uses .net4.5 and C#6.0 syntax, and you can also view the underlying source code of ThoughtWorks.
1. Generate QR code:
/// <summary> /// Generate QR code /// </summary> /// <param name="content">String with generated QR code</param> /// <param name="path">path</param> /// <returns></returns> public static string CreatehoughtWorksQrCode(string content, string path) { if ((content)) { throw new ArgumentNullException(content); } if ((path)) { throw new ArgumentNullException(path); } var qrCodeEncoder = new QRCodeEncoder { QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE, QRCodeScale = 4, QRCodeVersion = 8, QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M }; Image image = (content); var filename = ("yyyymmddhhmmssfff") + ".jpg"; var filepath = ("{0}{1}", path, filename); FileStream fs = null; try { fs = new FileStream(filepath, , ); (fs, ); } catch (IOException ex) { throw new IOException(); } finally { if (fs != null) (); (); } return CodeDecoder(filepath); }
2. Analyze the QR code:
/// <summary> /// QR code decoding /// </summary> /// <param name="filePath">Picture Path</param> /// <returns></returns> public static string CodeDecoder(string filePath) { if ((filePath)) { throw new ArgumentNullException(filePath); } try { if (!(filePath)) return null; var myBitmap = new Bitmap((filePath)); var decoder = new QRCodeDecoder(); var decodedString = (new QRCodeBitmapImage(myBitmap)); return decodedString; } catch (Exception ex) { throw new Exception(); } }
3. Select the generated QR code parameters:
/// <summary> /// Select the relevant type to generate the QR code /// <param name="strData">The text or number to be generated is supported in Chinese. For example: "4408810820 Shenzhen-Guangzhou" or: 4444444444</param> /// <param name="qrEncoding">Three sizes: BYTE, ALPHA_NUMERIC, NUMERIC</param> /// <param name="level">Size: L M Q H</param> /// <param name="version">version: such as 8</param> /// <param name="scale">Ratio: such as 4</param> /// <returns></returns> /// </summary> public void CreateCode_Choose(string strData, string qrEncoding, string level, int version, int scale) { var qrCodeEncoder = new QRCodeEncoder(); var encoding = qrEncoding; switch (encoding) { case "Byte": = QRCodeEncoder.ENCODE_MODE.BYTE; break; case "AlphaNumeric": = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC; break; case "Numeric": = QRCodeEncoder.ENCODE_MODE.NUMERIC; break; default: = QRCodeEncoder.ENCODE_MODE.BYTE; break; } = scale; = version; switch (level) { case "L": = QRCodeEncoder.ERROR_CORRECTION.L; break; case "M": = QRCodeEncoder.ERROR_CORRECTION.M; break; case "Q": = QRCodeEncoder.ERROR_CORRECTION.Q; break; default: = QRCodeEncoder.ERROR_CORRECTION.H; break; } Image image = null; FileStream fs = null; try { //Sign up pictures image = (strData); var filename = ("yyyymmddhhmmssfff") + ".jpg"; var filepath = (@"~\Upload") + "\\" + filename; fs = new FileStream(filepath, , ); (fs, ); } catch (IOException ex) { throw new IOException(); } finally { if (fs != null) (); if (image != null) (); } }
The above is the detailed content of how to generate QR codes with ThoughtWorks in C#. For more information about generating QR codes in C#, please pay attention to my other related articles!