Asp.net generate and parse the barcode and QR code

Source: Internet
Author: User

I. Bar Code

One-dimensional code, commonly known as bar code, is widely used in electronics industry and other industries. For example, a bar code is displayed on the back of a common book, And the ISBNInternational standard book number and International standard book number of the book can be obtained by scanning a scanner or other equipment ). There are also many encoding methods, such as Code39 and Code128. Here code128 code is used, which is taken from the Internet and slightly modified, such as text placed in the middle of the bar code ). You can directly copy data from a project. Here is a simple demonstration. Code128 is used as an example.

Code128 code = new Code128 (); code. valueFont = new Font ("Arial", 7); // declare the Font Bitmap bitMap = code below the bar code. getCodeImage (tf6_PERSONIDNUM.Text.Trim (), Code128.Encode. code128A); // obtain the bitmap string fileName = Server by Code128 encoding the text box file. mapPath ("~ ") +" Images \ QRImages \ "+ tf6_PERSONIDNUM.Text.Trim () + ". jpg "; // obtain the image path bitMap. save (fileName, ImageFormat. jpeg); // Save the image this. QRCodeImage. imageUrl = "~ /Images/QRImages/"+ tf6_PERSONIDNUM.Text.Trim () +". jpg "; // display the image control

The usage of Code39 classes is similar. Because Code39 encoding is widely used, many encapsulated dll files can be directly used on the Internet, such as BarCode, which can be directly referenced.

Code that references the BarCode method:

BarcodeControl barcode = new BarcodeControl (); barcode. barcodeType = BarcodeType. CODE128C; barcode. copyRight = ""; // The title is not displayed as a null string; barcode. data = tf6_PERSONIDNUM.Text.Trim (); MemoryStream stream = new MemoryStream (); barcode. makeImage (ImageFormat. png, 1, 1, true, false, null, stream); Bitmap bitMap = new Bitmap (stream); string fileName = Server. mapPath ("~ ") +" Images \ QRImages \ "+ tf6_PERSONIDNUM.Text.Trim () +". jpg "; bitMap. Save (fileName, ImageFormat. Jpeg); this. QRCodeImage. ImageUrl = "~ /Images/QRImages/"+ tf6_PERSONIDNUM.Text.Trim () +". jpg ";

Last:

650) this. width = 650; "src =" http://img.blog.csdn.net/20131106143914734? Watermark/2/text/character =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/Center "style =" border: none; "alt =" Center "/>

Ii. QR code

QR code is becoming more and more popular. You can scan the QR code scanner on your mobile phone or other mobile terminals to obtain relevant information. You can find many QR code tools on the Internet, and even many websites that generate and parse QR codes online. In the business system, for example, in the archive management system, the barcode is usually used to store the basic information of archive workers, but the storage capacity is limited. Now, you can use the QR code to store more capacity information.

We use the class library ThoughtWorks. QRCode. dll. Reference this dll file in the asp.net project and you can use relevant APIs for development.

For example, we encapsulate the most streamlined class:

/// <Summary> /// QR code Assistant class /// </summary> public class QRCodeOp {// <summary> /// generate a QR code /// </summary >/// <param name = "qrCodeContent"> content to be encoded </param> /// <returns> returns the Bitmap of the QR code </returns> public static Bitmap QRCodeEncoderUtil (string qrCodeContent) {QRCodeEncoder qrCodeEncoder = new QRCodeEncoder (); qrCodeEncoder. QRCodeVersion = 0; Bitmap img = qrCodeEncoder. encode (qrCodeContent, Encoding. UTF8); // specifies UTF-8 encoding, which supports Chinese return img ;} /// <summary> /// parse the QR code /// </summary> /// <param name = "bitmap"> the bitmap of the QR code to be parsed </param> // /<returns> parsed string </returns> public static string QRCodeDecoderUtil (Bitmap bitmap) {QRCodeDecoder decoder = new QRCodeDecoder (); string decodedString = decoder. decode (new QRCodeBitmapImage (bitmap), Encoding. UTF8); // specifies UTF-8 encoding and supports Chinese return decodedString ;}}

You can use the above two methods to generate and parse the QR code.

We can call it in a program.

Protected void EncodeClick (object sender, DirectEventArgs e) {StringBuilder sb = new StringBuilder (); sb. append (tf6_PERSONIDNUM.Text.Trim () + ";"); // personal ID card number sb. append (tf_DAID.Text.Trim () + ";"); sb. append (tf_DANAME.Text.Trim () + ";"); sb. append (cb_DABUSKIND.Text.Trim () + ";"); sb. append (cb_DAKIND.Text.Trim () + ";"); sb. append (cb_DALEVELCODE.Text.Trim () + ";"); sb. append (df_DABUILDTIME.Text.Trim () + ";"); Sb. append (tf_DAORG.Text.Trim () + ";"); sb. append (cb_DASTATUS.Text.Trim () + ";"); sb. append (tf_DABUILDER.Text.Trim () + ";"); sb. append (tf_DABUILDORG.Text.Trim () + ";"); Bitmap bitmap = QRCodeOp. QRCodeEncoderUtil (sb. toString (). trim (); // call the QR code encoding method to generate a bitmap string fileName = Server. mapPath ("~ ") +" Images \ QRImages \ "+ tf6_PERSONIDNUM.Text.Trim () + ". jpg "; bitmap. save (fileName); // Save the bitmap. The file name is your ID card number. this. QRCodeImage. imageUrl = "~ /Images/QRImages/"+ tf6_PERSONIDNUM.Text.Trim () + ". jpg "; // display image X. msg. alert ("QR code information", QRCodeOp. QRCodeDecoderUtil (bitmap )). show (); // call the QR code decoding method to pop up the information before encoding}

Here I use the Ext. NET Component in the project. X. Msg. Alert, QRCodeImage. ImageUrl, and DicrectEventArgs are not very important. Interested readers can go to the ext.net website to learn about ext.net.


See the following results:

650) this. width = 650; "src =" http://img.blog.csdn.net/20131025141229765? Watermark/2/text/character =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/Center "style =" border: none; "alt =" Center "/>

Click the button to include numbers, letters, Chinese characters, and time in the text box), generate a QR code image, and parse it in turn.

On this basis, the reader can further encapsulate the extension and provide a web service generated by a QR code in combination with technologies such as WCF.

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.