[Go] Barcode and QR Code codec implemented in C #

Source: Internet
Author: User

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 = newcom.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 Source

Note: Before clicking the Decode button, select a QR code or barcode image, click the "..." button to select the picture, then click the "Decode" button.

Insert small picture in middle of QR code

We all know that the QR code is getting more and more beautiful, you can add a variety of patterns and images. QRCode with the German-Solomon code to encode, Reed-Solomon code is a fault-tolerant encoding method, using this mechanism to scan the time does not need to read all the bits, it also makes simple in the QRCode small amount of change information, such as the addition of small images, etc., became possible. Before I gave you free to use the Zxing open source project transformed into a one or two-D code codec, but failed to insert the picture. This time, after some efforts, the picture is successfully inserted into the QR code and can be encoded and decoded. The key to inserting a picture is the adjustment of the two-dimensional code fault tolerance coefficient, which is described in detail.

Interface:

[Go] Barcode and QR Code codec implemented in C #

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.