C # code operation [Source Code download ],

Source: Internet
Author: User

C # code operation [Source Code download ],

This article describes how to generate and read a QR code and a QR code through C.

Directory

1. Introduction: This section describes the classification of bar codes and the ZXing. Net class libraries.

2. One-dimensional code operation: Includes generating and reading one-dimensional code.

3. QR code operation: Includes generating and reading QR codes and generating QR codes with logos.

4. Source Code download: displays the runtime diagram and source code download.

 

1. Introduction to 1.1 bar code

Bar Code(Barcode):It is a graphic identifier that sorts multiple black bars and white spaces with varying widths according to certain encoding rules to express a set of information.

 

1.2 bar code classification

It can be divided into one-dimensional barcode and two-dimensional Barcode:

One-dimensional Barcode:Information is expressed in one direction (generally in the horizontal direction), but not in the vertical direction.

Two-dimensional Barcode:Store the barcode of information in two-dimensional space in the horizontal and vertical directions.

 

1.3 third-party Class Library: ZXing. Net1.3.1 description

ZXingIs an open-source class library that can generate and read 1D/2D (1D/2D) bar codes. The original Java version was later developed by a third party to support versions such as QT, C ++, And. Net.

Platforms supported by. Net:. Net 2.0, 3.5 and 4.0, Silverlight 4 and 5, Windows Phone 7.0, 7.1 and 8.0,Windows CE,Unity3D,Xamarin. Android and so on.

 

1.3.2

Java version:Https://github.com/zxing/zxing

ZXing. Net version:Http://zxingnet.codeplex.com/

 

2. 1-dimensional code operation 2.1 Introduction

One-dimensional Barcode:Information is expressed in one direction (generally in the horizontal direction), but not in the vertical direction.

Common Code:EAN, 39, 25, UPC, 128, 93, ISBN, and Codabar.

Domestic implementation of the use of EAN commodity barcode, can be divided into EAN-13 (standard) and EAN-8 (short version) two kinds.

Example:

 

2.2 generate a one-dimensional code

Take the generation of EAN-13 code system as an example:

// 1. encodingOptions encodeOption = new EncodingOptions (); encodeOption. height = 130; // specify the Height and width of encodeOption. width = 240; // 2. generate a barcode image and save ZXing. barcodeWriter wr = new BarcodeWriter (); wr. options = encodeOption; wr. format = BarcodeFormat. EAN_13; // barcode specification: EAN13 specification: 12 (No checkpoint) or 13-digit Bitmap img = wr. write (this. contentTxt. text); // generate the image string filePath = System. appDomain. currentDomain. baseDirectory + "\ EAN_13-" + this. contentTxt. text + ". jpg "; img. save (filePath, System. drawing. imaging. imageFormat. jpeg );

 

2.3 read one-dimensional code

Take reading the EAN-13 code of the image as an example:

// 1. set DecodingOptions decodeOption = new DecodingOptions (); decodeOption. possibleFormats = new List <BarcodeFormat> () {BarcodeFormat. EAN_13 ,}; // 2. perform the read operation ZXing. barcodeReader br = new BarcodeReader (); br. options = decodeOption; ZXing. result rs = br. decode (this. barCodeImg. image as Bitmap); if (rs = null) {this. contentTxt. text = "reading failed"; MessageBox. show ("failed to read");} else {this. contentTxt. text = rs. text; MessageBox. show ("read Successful, content:" + rs. text );}

 

3. Introduction to QR code operation 3.1

QR code:Store the barcode of information in two-dimensional space in the horizontal and vertical directions.

Common Code:Listen 417, QR Code, Code 49, Code 16 K, Code One, etc.

Example:

 

3.2 generate QR code

Take the QR code generation as an example:

// 1. set the QR code specification ZXing. qrCode. qrCodeEncodingOptions qrEncodeOption = new ZXing. qrCode. qrCodeEncodingOptions (); qrEncodeOption. characterSet = "UTF-8"; // set the encoding format; otherwise, the 'Chinese' garbled qrEncodeOption is read. height = 200; qrEncodeOption. width = 200; qrEncodeOption. margin = 1; // set the surrounding Margin. // 2. generate a barcode image and save ZXing. barcodeWriter wr = new BarcodeWriter (); wr. format = BarcodeFormat. QR_CODE; // two-dimensional code wr. options = qrEncodeOption; Bitmap img = wr. write (this. contentTxt. text); string filePath = System. appDomain. currentDomain. baseDirectory + "\ QR-" + this. contentTxt. text + ". jpg "; img. save (filePath, System. drawing. imaging. imageFormat. jpeg );

 

3.3 read QR code

The following uses a QR code-based image as an example:

// 1. set DecodingOptions decodeOption = new DecodingOptions (); decodeOption. possibleFormats = new List <BarcodeFormat> () {BarcodeFormat. QR_CODE,; // 2. perform the read operation ZXing. barcodeReader br = new BarcodeReader (); br. options = decodeOption; ZXing. result rs = br. decode (this. barCodeImg. image as Bitmap); if (rs = null) {this. contentTxt. text = "reading failed"; MessageBox. show ("failed to read");} else {this. contentTxt. text = rs. text; MessageBox. show ("read Successful, content:" + rs. text );}

 

3.4 generate a QR code with a Logo

The QR code has the verification function. Therefore, images of a certain size can be displayed in the middle area.

Example:

Code:

// 1. set the QR code specification ZXing. qrCode. qrCodeEncodingOptions qrEncodeOption = new ZXing. qrCode. qrCodeEncodingOptions (); qrEncodeOption. characterSet = "UTF-8"; // set the encoding format; otherwise, the 'Chinese' garbled qrEncodeOption is read. height = 200; qrEncodeOption. width = 200; qrEncodeOption. margin = 1; // set the surrounding Margin. // 2. generate a bar code image ZXing. barcodeWriter wr = new BarcodeWriter (); wr. format = BarcodeFormat. QR_CODE; // two-dimensional code wr. options = qrEncodeOption; Bitmap img = wr. write (this. contentTxt. text); // 3. draw the logo image Bitmap logoImg = Bitmap on the Bitmap object of the QR code. fromFile (System. appDomain. currentDomain. baseDirectory + "\ logo.jpg") as Bitmap; Graphics g = Graphics. fromImage (img); Rectangle logoRec = new Rectangle (); // set the size and position of the logo image logoRec. width = img. width/6; logoRec. height = img. height/6; logoRec. X = img. width/2-logoRec. width/2; // The Center logoRec. Y = img. height/2-logoRec. height/2; g. drawImage (logoImg, logoRec); // 4. save the drawn image string filePath = System. appDomain. currentDomain. baseDirectory + "\ QR-" + this. contentTxt. text + ". jpg "; img. save (filePath, System. drawing. imaging. imageFormat. jpeg );

 

4. Download the source code 4.1.

 

4.2

Baidu Network Disk: http://pan.baidu.com/s/1qWRJMAo

Http://download.csdn.net/detail/polk6/9383226 (CSDN)

 

====================================== Series of articles ==============================================

This article: 3.3 C # code operation [Source Code download]

C # Article navigation

 

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.