Summary of ASP. net qr code generation methods, asp.net Summary

Source: Internet
Author: User

Summary of ASP. net qr code generation methods, asp.net Summary

This example summarizes how ASP. NET generates a QR code. We will share this with you for your reference. The details are as follows:

Share the code for generating a QR code in c # And directly reference the ThoughtWorks. QRCode. dll class to generate a QR code. For more information, see.

Method 1. Directly reference the ThoughtWorks. QRCode. dll class to generate a QR code.

Sample Code:

ThoughtWorks. QRCode. codec. QRCodeEncoder encoder = new QRCodeEncoder (); encoder. QRCodeEncodeMode = QRCodeEncoder. ENCODE_MODE.BYTE; // encoding method (Note: BYTE supports Chinese characters and ALPHA_NUMERIC scans all numbers) encoder. QRCodeScale = 4; // size encoder. QRCodeVersion = 0; // version (Note: setting it to 0 mainly prevents errors when the encoded string is too long) encoder. QRCodeErrorCorrect = QRCodeEncoder. ERROR_CORRECTION.M; String qrdata = "QR code information"; System. drawing. bitmap bp = encoder. encode (qrdata. toString (), Encoding. getEncoding ("GB2312"); Image image = bp; Object oMissing = System. reflection. missing. value; pictureBox1.Image = bp;

Save the QR code image:

Sample Code:

SaveFileDialog sf = new SaveFileDialog (); sf. title = "select the location for saving files"; sf. filter = "Save the image (*. jpg) | *. jpg | all files (*. *) | *. * "; // set the display sequence of default file types sf. filterIndex = 1; // whether to remember the last opened directory sf in the SAVE dialog box. restoreDirectory = true; if (sf. showDialog () = DialogResult. OK) {Image im = this. pictureBox1.Image; // obtain the file path string localFilePath = sf. fileName. toString (); if (sf. fileName! = "") {String fileNameExt = localFilePath. substring (localFilePath. lastIndexOf ("\") + 1); // get the file name without path // newFileName = fileNameExt + DateTime. now. toString ("yyyyMMdd"); // Add the time string FilePath = localFilePath after the file name. substring (0, localFilePath. lastIndexOf (". "); // get the file path, with the file name, without the suffix string fn = sf. fileName; pictureBox1.Image. save (FilePath + "-" + DateTime. now. toString ("yyyyMMdd") + ". jpg ") ;}/// parse the QR code information // QRCodeDecoder decoder = new QRCodeDecoder (); // String decodedString = decoder. decode (new QRCodeBitmapImage (new Bitmap (pictureBox1.Image); // this. label3.Text = decodedString;

Method 2. Reference The ZXing class library.

ZXing is an open-source Java class library used to parse 1D/2D bar codes in multiple formats. The goal is to decode the 1D barcode of QR code, Data Matrix, and UPC. At the same time, it also provides class libraries such as cpp, ActionScript, android, iPhone, rim, j2se, jruby, and C. The main role of zxing class libraries is decoding, which is currently a strong decoding capability in open-source class libraries (in other words, it is really worth the cost of authorizing thousands of class libraries ).

Download the corresponding code from Google code

1. download the latest zxing package

Home to zxing: http://code.google.com/p/zxing/

Find the CSharp folder and open and compile it in vs. Copy zxing. dll in debug under obj and paste it to the binfile directory in your project,
Right-click to add a project reference. Reference zxing. dll to the project so that it can be used as needed.

There are two UTF-8 problems in the source code, it will cause Chinese garbled (modified before compiling. dll)

1. In the com. google. zxing. qrcode. encoder. encoder class

internal const System.String DEFAULT_BYTE_MODE_ENCODING = "ISO-8859-1";

Here, change ISO-8859-1 to UTF-8

Second: com. google. zxing. qrcode. decoder. DecodedBitStreamParser class member

Private const System. String UTF8 = "UTF8 ";

UTF8 should be changed to UTF-8

Sample Code:

using com.google.zxing.qrcode;using com.google.zxing;using com.google.zxing.common;using ByteMatrix = com.google.zxing.common.ByteMatrix;using EAN13Writer = com.google.zxing.oned.EAN13Writer;using EAN8Writer = com.google.zxing.oned.EAN8Writer;using MultiFormatWriter = com.google.zxing.MultiFormatWriter;

Method:

String content = "QR code information"; ByteMatrix byteMatrix = new MultiFormatWriter (). encode (content, BarcodeFormat. QR_CODE, 300,300); Bitmap bitmap = toBitmap (byteMatrix); pictureBox1.Image = bitmap; SaveFileDialog sFD = new SaveFileDialog (); sFD. filter = "Save the image (*. png) | *. png | all files (*. *) | *. * "; sFD. defaultExt = "*. png | *. png "; sFD. addExtension = true; if (sFD. showDialog () = DialogResult. OK) {if (sFD. fileName! = "") {WriteToFile (byteMatrix, System. Drawing. Imaging. ImageFormat. Png, sFD. FileName );}}

Parse the QR code:

Sample Code:

if (this.openFileDialog1.ShowDialog() != DialogResult.OK){return;}Image img = Image.FromFile(this.openFileDialog1.FileName);Bitmap bmap;try{bmap = new Bitmap(img);}catch (System.IO.IOException ioe){MessageBox.Show(ioe.ToString());return;}if (bmap == null){MessageBox.Show("Could not decode image");return;}LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);com.google.zxing.BinaryBitmap bitmap1 = new com.google.zxing.BinaryBitmap(new HybridBinarizer(source));Result result;try{result = new MultiFormatReader().decode(bitmap1);}catch (ReaderException re){MessageBox.Show(re.ToString());return;}MessageBox.Show(result.Text);public static void writeToFile(ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file){    Bitmap bmap = toBitmap(matrix);    bmap.Save(file, format);}public static Bitmap toBitmap(ByteMatrix matrix){    int width = matrix.Width;    int height = matrix.Height;    Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);    for (int x = 0; x < width; x++){for (int y = 0; y < height; y++){  bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));}}return bmap;}

PS: here we recommend a powerful online QR code generation tool for free:

Online QR code generation tool (enhanced edition ):
Http://tools.jb51.net/transcoding/jb51qrcode

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.