In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces how to use the DotNet QR code operation component ThoughtWorks.QRCode, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.
There is one thing in life that is almost on the verge of becoming another electronic "ID card", and that is the QR code. Whether in the process of software development or in the daily life of ordinary users, it is almost inseparable from the QR code. Two-dimensional code (dimensional barcode), also known as two-dimensional bar code, is a kind of readable bar code extended on the basis of one-dimensional bar code. The equipment scans the two-dimensional bar code and obtains the information contained in it by identifying the binary data recorded in the length and width of the bar code. Compared with the one-dimensional bar code, the two-dimensional code records more complex data, such as pictures, web links and so on.
The ThoughtWorks.QRCode component can generate the QR code we need efficiently and stably. Let's take a detailed look at this component.
I. Overview of ThoughtWorks.QRCode components:
The QRCode library is a. Net component that can be used to encode and decode QRCode. QRCode is a two-dimensional bar code from Japan. Now, it is widely used in a wide range of industrial fields. Used for vehicle parts tracking and inventory management. QR stands for "quick response". It was created by Japanese company Denso-Wave in 1994 to decode content at high speed. Today, QR codes are used in mobile phones to ease data entry. The QRCode can also be printed on a business card or displayed on any monitor and can then be captured by a mobile phone, as long as the mobile phone has software to read QRCode. The functions provided by the QRCode library include: encoding the content as a QR code image, which can be saved in JPEG,GIF,PNG or bitmap format; decoding the QR code image.
The library can be used in any .NET 2.0 Windows application, ASP.NET Web application, or Windows Mobile device application. The following is the component's statement that "this article and any related source code and files are licensed under the Code Project Open license (CPOL)."
2. Core object and method resolution related to ThoughtWorks.QRCode:
The main categories of ThoughtWorks.QRCode are as follows:
The above is using .NET Reflector to decompile the DLL file to view the source code. Since I only downloaded the DLL file, not the source code, I looked at the source code directly using the .NET Reflector. Let's take a look at some of the component classes and methods in detail:
1.QRCodeEncoder: QR code coding class.
Public enum ENCODE_MODE {ALPHA_NUMERIC, NUMERIC, BYTE} public enum ERROR_CORRECTION {L, M, Q, H} public virtual Bitmap Encode (string content, Encoding encoding) {bool [] [] flagArray = this.calQrcode (encoding.GetBytes (content)); SolidBrush brush = new SolidBrush (this.qrCodeBackgroundColor); Bitmap image = new Bitmap ((flagArray.Length * this.qrCodeScale) + 1, (flagArray.Length * this.qrCodeScale) + 1); Graphics graphics = Graphics.FromImage (image) Graphics.FillRectangle (brush, new Rectangle (0,0, image.Width, image.Height); brush.Color = this.qrCodeForegroundColor; for (int I = 0; I
< flagArray.Length; i++) { for (int j = 0; j < flagArray.Length; j++) { if (flagArray[j][i]) { graphics.FillRectangle(brush, j * this.qrCodeScale, i * this.qrCodeScale, this.qrCodeScale, this.qrCodeScale); } } } return image;} 2.QRCodeDecoder:二维码解码类。 public virtual string decode(QRCodeImage qrCodeImage, Encoding encoding){ sbyte[] src = this.decodeBytes(qrCodeImage); byte[] dst = new byte[src.Length]; Buffer.BlockCopy(src, 0, dst, 0, dst.Length); return encoding.GetString(dst);} public virtual sbyte[] decodeBytes(QRCodeImage qrCodeImage){ DecodeResult result; Point[] adjustPoints = this.AdjustPoints; ArrayList list = ArrayList.Synchronized(new ArrayList(10)); while (this.numTryDecode < adjustPoints.Length) { try { result = this.decode(qrCodeImage, adjustPoints[this.numTryDecode]); if (result.CorrectionSucceeded) { return result.DecodedBytes; } list.Add(result); canvas.println("Decoding succeeded but could not correct"); canvas.println("all errors. Retrying.."); } catch (DecodingFailedException exception) { if (exception.Message.IndexOf("Finder Pattern") >= 0) {throw exception;}} finally {this.numTryDecode++;}} if (list.Count = = 0) {throw new DecodingFailedException ("Give up decoding");} int num =-1; int numErrors = 0x7fffffff; for (int I = 0; I < list.Count; ionization +) {result = (DecodeResult) list [I]; if (result.NumErrors < numErrors) {numErrors = result.NumErrors; num = I } canvas.println ("All trials need for correct error"); canvas.println ("Reporting #" + num + "that,"); canvas.println ("corrected minimum errors (" + numErrors + ")"); canvas.println ("Decoding finished."); return ((DecodeResult) list [num]). DecodedBytes;}
3.QRCodeBitmapImage: bitmap image.
Public class QRCodeBitmapImage: QRCodeImage {/ / Fields private Bitmap image; / / Methods public QRCodeBitmapImage (Bitmap image); public virtual int getPixel (int x, int y); / / Properties public virtual int Height {get;} public virtual int Width {get;}} public interface QRCodeImage {/ / Methods int getPixel (int x, int y); / / Properties int Height {get;} int Width {get;}
The above is an introduction to some of the methods of the ThoughtWorks.QRCode component. If you need to know more about them, you can check the corresponding source code.
3. ThoughtWorks.QRCode QR code operation example:
1. Generate a QR code (no QR code is set).
/ / generate QR code / path / public static string CreatehoughtWorksQrCode (string content, string path) {if (string.IsNullOrEmpty (content)) {throw new ArgumentNullException (content);} if (string.IsNullOrEmpty (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 = qrCodeEncoder.Encode (content); var filename = DateTime.Now.ToString ("yyyymmddhhmmssfff") + ".jpg"; var filepath = string.Format ("{0} {1}", path, filename); FileStream fs = null; try {fs = new FileStream (filepath, FileMode.OpenOrCreate, FileAccess.Write) Image.Save (fs, System.Drawing.Imaging.ImageFormat.Jpeg);} catch (IOException ex) {throw new IOException (ex.Message);} finally {if (fs! = null) fs.Close (); image.Dispose ();} return CodeDecoder (filepath);}
two。 Select the relevant type that generates the QR code.
/ Select the relevant type of QR code to generate / the text or number to be generated. Chinese is supported. For example, "4408810820 Shenzhen-Guangzhou" or: 4444444444 / / three sizes: BYTE, ALPHA_NUMERIC,NUMERIC / / size: L M Q H / / version: such as 8 / ratio: such as 4 / public void CreateCode_Choose (string strData, string qrEncoding, string level, int version, int scale) {if (string.IsNullOrEmpty (strData)) {throw new ArgumentNullException (strData) } if (string.IsNullOrEmpty (qrEncoding)) {throw new ArgumentNullException (qrEncoding);} if (string.IsNullOrEmpty (level)) {throw new ArgumentNullException (level);} var qrCodeEncoder = new QRCodeEncoder (); var 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 = QRCodeEncoder.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: qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H; break;} Image image = null; FileStream fs = null; try {/ / text generation picture image = qrCodeEncoder.Encode (strData); var filename = DateTime.Now.ToString ("yyyymmddhhmmssfff") + ".jpg" Var filepath = HttpContext.Current.Server.MapPath (@ "~\ Upload") + "\" + filename; fs = new FileStream (filepath, FileMode.OpenOrCreate, FileAccess.Write); image.Save (fs, System.Drawing.Imaging.ImageFormat.Jpeg);} catch (IOException ioex) {throw new IOException (ioex.Message);} catch (Exception ex) {throw new Exception (ex.Message) } finally {if (fs! = null) fs.Close (); if (image! = null) image.Dispose ();}
3. QR code decoding.
/ QR code decoding / Image path / public static string CodeDecoder (string filePath) {if (string.IsNullOrEmpty (filePath)) {throw new ArgumentNullException (filePath);} try {if (! File.Exists (filePath)) return null; var myBitmap = new Bitmap (Image.FromFile (filePath)); var decoder = new QRCodeDecoder (); var decodedString = decoder.decode (new QRCodeBitmapImage (myBitmap)) Return decodedString;} catch (Exception ex) {throw new Exception (ex.Message);}}
Like the previous introduction of components, the first is the overview of components, the core classes of components, the use of components, these in this component, find and change the relevant overview of components, spent a lot of time, do not know why, this component did not find the relevant information, and even the author is replaced by so-and-so, but this is the case with the Internet, we do not need to know who made it, as long as it is convenient to use. Among the components and js plug-ins that generate QR codes, I personally like this component, and it feels good. Any component and method has personal preferences and usage environment, and readers can choose according to the situation.
About how to use the DotNet QR code operation component ThoughtWorks.QRCode to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.