Barcode and QR Code codec implemented in C #

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/tuyile006/archive/2013/01/16/2863367.html

This article describes the 1d/2d codecs that you can use in C #. The application of bar code is already very common, almost all the goods in the supermarket are printed with barcodes, QR Code also began to apply to many occasions, such as fire tickets have two-dimensional code recognition, NetEase's home page has two-dimensional code icons, users just need to scan with a mobile phone can see the mobile version of NetEase's URL, avoid the trouble of inputting long strings

Standard for barcodes :

Barcodes are standard with ENA barcodes, UPC barcodes, 25-bar codes, cross 25-bar codes, Codabar barcodes, Sanjiu barcodes, and 128 bar codes, and the most commonly used products are EAN barcodes. EAN Barcode, also known as the general merchandise barcode, is developed by the International Code of Goods Association, which is common around the world and is currently the most widely used barcode in the international trade. China is currently in the domestic implementation of the use of this commodity barcode. EAN commodity barcode is divided into EAN-13 (Standard Edition) and EAN-8 (shortened version) two kinds.

Two-dimensional code coding standard:

The world's existing QR code of up to 200 kinds, among which the common technical standards are PDF417 (American Standard), QRCode (Japanese standard), CODE49,CODE16K,CODEONE,DM (Korean standard), GM (Chinese standard), CM (Chinese standard) and more than 20 kinds. The most used is qrcode.

The following is the result of using Google's Open source project zxing to encode and decode 1d/2d, as follows:

The official address of zxing is: http://code.google.com/p/zxing/

Zxing is still very powerful, originally written in Java and supported by Android, iOS, Symbian and other mobile phone operating systems.

But I do not know why, the official website has not even an example, the document is also a dictionary of all classes listed, not for the reader's consideration.

Below I would like to use zxing to complete the example shown in this tutorial for beginners reference:

1. We create a new WinForm test project;

2. Download zxing open source project from official website, about 16m appearance, unpack and open Zxing-2.1\csharp directory, copy this directory to our new WinForm project (easy to debug and see the source code, not necessarily so);

Add a reference to the Zxing project in the 3.winform project;

4. Build the control as shown in the example, and the code "generate barcode" is as follows:

?
//生成条形码        privatevoidbutton1_Click(objectsender, EventArgs e)        {            lbshow.Text ="";            Regex rg =newRegex("^[0-9]{13}$");            if(!rg.IsMatch(txtMsg.Text))            {                MessageBox.Show("本例子采用EAN_13编码,需要输入13位数字");                return;            }                       try            {                MultiFormatWriter mutiWriter =new com.google.zxing.MultiFormatWriter();                ByteMatrix bm = mutiWriter.encode(txtMsg.Text, com.google.zxing.BarcodeFormat.EAN_13, 363, 150);                Bitmap img= bm.ToBitmap();                pictureBox1.Image =img;                //自动保存图片到当前目录                stringfilename = System.Environment.CurrentDirectory + "\\EAN_13"+ DateTime.Now.Ticks.ToString() +".jpg";                img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);                lbshow.Text ="图片已保存到:"+ filename;            }            catch(Exception ee)            { MessageBox.Show(ee.Message); }        }

One needs to pay attention to the Barcodeformat parameters, you can open the definition to see the specific encoding method, their own Baidu each encoding method of the input requirements.

Here the EAN_13 code requirement is a 13-bit length number, and satisfies: Sum the number of all even ordinal digits, use the calculated and multiply 3, and then sum up the number of all odd numbers, using the number obtained and added just the even number, and then the sum can be divisible by 10. (This rule is checked in the Upceanreader class of the Checkstandardupceanchecksum method, if not necessary, can be removed)

The code that generates the QR code is similar to the above:

Generate two-dimensional code        private void Button2_Click (object sender, EventArgs e)        {            lbshow. Text = "";            Try            {                Multiformatwriter mutiwriter = new Com.google.zxing.MultiFormatWriter ();                Bytematrix BM = Mutiwriter.encode (Txtmsg.text, Com.google.zxing.BarcodeFormat.QR_CODE, +);                Bitmap img = BM. Tobitmap ();                pictureBox1.Image = img;                Automatically save the picture to the current directory                string filename = System.Environment.CurrentDirectory + "\\QR" + DateTime.Now.Ticks.ToString () + ". JPG ";                Img. Save (filename, System.Drawing.Imaging.ImageFormat.Jpeg);                Lbshow. Text = "Picture saved to:" + filename;            }            catch (Exception ee)            {MessageBox.Show (EE. Message); }        }

Note that the encoding problem, in the Com.google.zxing.qrcode.encoder.Encoder class to modify the default encoding of UTF-8, otherwise decoding occurs garbled.

System.String default_byte_mode_encoding = "UTF-8";

Before this is "iso-8859-1", the reason is changed to UTF-8 because, when decoding the program will guess the possible encoding, if the guess fails the default is UTF-8, The code is in the Guessencoding method of the Com.google.zxing.qrcode.decoder.DecodedBitStreamParser class.

Therefore, this open source project also lacks the overall thinking, even the encoding and decoding of the default encoding method is inconsistent.

After netizens to reflect, or some Chinese will appear decoding into garbled, and some can, estimate or Guessencoding method guess coding appeared deviation, directly guessencoding method written back UTF8 on the line.

4. Realize the image decoding, that is, the barcode or QR code image decoding into its real content, of course, the application of the PC is not small, but may just not found it, the code is as follows:

     Decoding operation        private void Button3_Click (object sender, EventArgs e)        {            Multiformatreader mutireader = new Com.google.zxing.MultiFormatReader ();            Bitmap img = (Bitmap) bitmap.fromfile (Opfilepath);            if (img = = null)                return;            Luminancesource ls = new Rgbluminancesource (IMG, IMG. Width, IMG. Height);            Binarybitmap BB = new Binarybitmap (new Com.google.zxing.common.HybridBinarizer (LS));            Result r= Mutireader.decode (BB);            Txtmsg.text = R.text;        }

Opfilepath is the picture path, you can use the OpenFileDialog control to open the file to get the path.

Download:

Demo

--------------------------------------------------------------------------------------

Note:

The above demo is only generated after the EXE, no source code.

Oneself imitate the author code slightly changed a bit to write a play, the main code is as follows, all source download: http://download.csdn.net/detail/gdjlc/5005921

[CSharp]View Plaincopyprint?
  1. Private void Btndetwo_click (object sender, EventArgs e)
  2. {
  3. using (OpenFileDialog OpenFileDialog = new OpenFileDialog ())
  4. {
  5. Openfiledialog.title = "Select two-dimensional code image";
  6. Openfiledialog.filter = "Images (*. bmp;*. jpg;*. GIF) |*. bmp;*. JPG; ";
  7. Openfiledialog.addextension = true;
  8. Openfiledialog.restoredirectory = true;
  9. if (openfiledialog.showdialog () = = DialogResult.OK)
  10. {
  11. Multiformatreader Mutireader = new Multiformatreader ();
  12. Bitmap img = (Bitmap) bitmap.fromfile (openfiledialog.filename);
  13. if (img = = null)
  14. return;
  15. pictureBox1.Image = img;
  16. Luminancesource ls = new Rgbluminancesource (IMG, IMG. Width, IMG.  Height);
  17. Binarybitmap BB = new Binarybitmap (new Hybridbinarizer (LS));
  18. Result r = Mutireader.decode (BB);
  19. Txtmsg.text = R.text;
  20. }
  21. }
  22. }

Barcode and QR code codecs implemented in C #

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.