C # Barcode and two-dimensional code

Source: Internet
Author: User

This paper is a combination of the source network knowledge points, thanks to the author, the end of the article to give the query data connection.

Bar code (barcode) is the width of a number of black bars and blanks, according to a certain coding rules, to express a set of information graphical identifiers. A common barcode is a parallel pattern of black strips (short strips) and white bars (short lines) that differ greatly in reflectivity. Barcode can be marked out of the goods producing country, manufacturer, commodity name, production date, book classification number, mail start and end location, category, date and many other information, so in the commodity circulation, book management, postal management, banking system and many other fields have been widely used.

Two-dimensional code (Quick Response code), also known as two-dimensional barcode, it is a certain geometric pattern in the plane (two-dimensional direction) distribution of black and white graphics, is a key to all information data. In modern business activities, can be implemented in a wide range of applications, such as: product security/traceability, advertising push, website links, data download, commodity trading, positioning/navigation, e-commerce applications, vehicle management, information transmission, etc.

The Zxing link library is used in this example:

1, adding references and namespaces

Using com.google.zxing;using com.google.zxing.qrcode.decoder;using COMMON = Com.google.zxing.common;

2, QR code and Barcode generation method (Generate picture)

        public static Bitmap Tobitmap (COMMON. Bytematrix Matrix, String Scolor, String qcolor)        {            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; + +)            {for                (int y = 0; y < height; y++)                {                    bmap. SetPixel (x, Y, matrix.get_renamed (x, y)! =-1?                        Colortranslator.fromhtml (Scolor): colortranslator.fromhtml (Qcolor));                }            }            return bmap;        }
        
public static void Createqrcode (string strcontent, ref Image qrimage)//QR code
{try {string scolor = "0xff000000";//QR code color string qcolor = "0x00ffff FF ";//Background color Hashtable hints = new Hashtable (); var error = ERRORCORRECTIONLEVEL.M; Hints. ADD (Encodehinttype.error_correction, ERROR); COMMON. Bytematrix byteMatrix1 = new Multiformatwriter (). Encode (Strcontent, Barcodeformat.qr_code, $, a, hints); Qrimage = Mycommon.tobitmap (byteMatrix1, Scolor, Qcolor); } catch (Exception ex) {MessageBox.Show (ex. Message); }} public static void Createqrcode (string strcontent, ref Image Qrimage)//barcode, EAN13 code {TR Y {string scolor = "0xff000000";//Barcode Color String qcolor = "0X00FFFFFF";//Background color Hashtable hints = new Hashtable (); var error = ERRORCORRECTIONLEVEL.M; Hints. ADD (Encodehinttype.errOr_correction, error); COMMON. Bytematrix byteMatrix2 = new Multiformatwriter (). Encode (Strcontent, barcodeformat.ean_13, 300, 80); Qrimage = Mycommon.tobitmap (byteMatrix2, Scolor, Qcolor); } catch (Exception ex) {MessageBox.Show (ex. Message); } }

32-dimensional code or barcode storage

In the actual work, often need to store two-dimensional code or bar code, here is a storage method, the two-dimensional code or bar code into binary string, and stored in the database can

          Using System.Drawing;          Using System.Drawing.Drawing2D;          Using System.IO;        public static byte[] Binaryphoto (String filename)        {            FileStream fs = new FileStream (FileName, FileMode.Open, FileAccess.Read);            BinaryReader br = new BinaryReader (fs);            byte[] Pbyte = br. Readbytes ((int) fs. Length);            Fs. Close ();            return pbyte;        }        public static Image Convtophoto (byte[] pbyte)        {            MemoryStream ms = new MemoryStream (pbyte, 0, pbyte.length); C14/>image Gimage = Image.fromstream (ms);            Ms. Close ();            return gimage;        }

Note: Storing binary stream data in a database requires a conversion of the binary data into a Base64-formatted string (which needs to be decoded when acquired), otherwise there will be surprises.

42-dimensional Code added logo

Two-dimensional code in the middle of the principle of adding a picture is the use of two-dimensional code error correction level, in the existing two-dimensional code image allowed (logo) size of the case to increase the logo.

4.1 Error correction level for two-dimensional code

                var errcorrlvl = errorcorrectionlevel.m;                var corrratio = 0.15;                Switch (this.cbocorrection.SelectedIndex)                {case                    0:errcorrlvl = errorcorrectionlevel.l; corrratio = 0.07; break;                    Case 1:ERRCORRLVL = ERRORCORRECTIONLEVEL.M; Corrratio = 0.15; break;                    Case 2:ERRCORRLVL = errorcorrectionlevel.q; Corrratio = 0.25; break;                    Case 3:ERRCORRLVL = ErrorCorrectionLevel.H; Corrratio = 0.30; break;                }

4.2 Determine if the logo is within the allowable range

               var logo = new Bitmap (this.txtLogoFile.Text);               var ratio = (double) (logo. Width * logo. Height)/(double) (bitmap. Width * Bitmap. Height);               if (ratio < Corrratio * 0.6)    //Logo picture size can only account for the maximum fault tolerance area of 60% to ensure that the picture is highly readable {... ........                   ....               

4.3 Add logo method

        <summary> draws a flag on a QR code bitmap. </summary>        private void createqrcodebitmapwithportrait (Bitmap qrcode, Bitmap logo)        {            Graphics g = Graphics.fromimage (QRCode);            int qsize = Int32.Parse (TextBox1.Text);            Sets the position of the avatar to be displayed, which is centered on the            int rectx = (Qsize-logo. Width)/2;            int recty = (Qsize-logo. Height)/2;            G.drawimage (Logo, rectx, recty);            G.dispose ();        }

  

5 network resources

A certain degree of Wikipedia barcode, QR code

Http://www.cnblogs.com/xuhang/p/3831776.html

Http://www.cnblogs.com/xuhang/p/3832118.html

Http://www.cnblogs.com/rophie/archive/2012/03/02/2376946.html

http://blog.csdn.net/suntongo/article/details/8742023

Http://www.cnblogs.com/mzlee/archive/2011/03/19/Lee_Barcode.html

Http://www.cnblogs.com/Soar1991/archive/2012/03/30/2426115.html

C # Barcode and two-dimensional code

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.