Introduction to ZXing, an open-source tool for bar code/QR code

Source: Internet
Author: User

 

Following the previous introduction of a Japanese Open-Source Software (this software can only implement QRCode) Original article: Java implements code and decoding of the QR code QRCode. Now we find another excellent open-source tool-ZXing, it is more flexible and convenient, and supports multiple encoding formats.

 

Full text directory:

Basic Introduction

Demonstration of encoding and decoding of two-dimensional codes (such as QRCode)

Code (for example: EAN-13) encoding and decoding demo

[1]. Basic Introduction:

1-1. ZXing is an open source Java class library used to parse barcode and QR code in multiple formats.

Official Website: http://code.google.com/p/zxing/

Up to now, the latest version is 1.7, which supports the following encoding formats:

UPC-A and UPC-E

EAN-8 and EAN-13

Code 39

Code 93

Code 128

QR Code

ITF

Codabar

RSS-14 (all variants)

Data Matrix

PDF 417 ('alpha' quality)

Aztec ('alpha' quality)

At the same time, the official website provides Android, cpp, C #, iPhone, j2se, j2se, jruby, objc, rim, symbian and other application class libraries. For details, refer to the downloaded source code package.

 

1-2. this article, like the previous article, is mainly an example of coding and decoding of bar code (EAN-13) and QR code (QRCode) on PC, for your reference, the source code of core and javase in the source code is used. The attachment provides the compiled lib package:

Zxing. jar

Zxing-j2se.jar

For applications related to various mobile phone systems, if you are interested, you can download the official source code package, which contains detailed application descriptions.

[2] demonstration of encoding and decoding of the QR code (QRCode:

 

2-1. Encoding example:

Java code

Package michael. zxing;

 

Import java. io. File;

Import java. util. Hashtable;

 

Import com. google. zxing. BarcodeFormat;

Import com. google. zxing. EncodeHintType;

Import com. google. zxing. MultiFormatWriter;

Import com. google. zxing. client. j2se. MatrixToImageWriter;

Import com. google. zxing. common. BitMatrix;

Import com. google. zxing. qrcode. decoder. ErrorCorrectionLevel;

 

/**

* @ Blog http://sjsky.iteye.com

* @ Author Michael

*/

Public class ZxingEncoderHandler {

 

/**

* Encoding

* @ Param contents

* @ Param width

* @ Param height

* @ Param imgPath

*/

Public void encode (String contents, int width, int height, String imgPath ){

Hashtable <Object, Object> hints = new Hashtable <Object, Object> ();

// Specify the error correction level

Hints. put (EncodeHintType. ERROR_CORRECTION, ErrorCorrectionLevel. L );

// Specify the encoding format

Hints. put (EncodeHintType. CHARACTER_SET, "GBK ");

Try {

BitMatrix bitMatrix = new MultiFormatWriter (). encode (contents,

BarcodeFormat. QR_CODE, width, height, hints );

 

MatrixToImageWriter

. WriteToFile (bitMatrix, "png", new File (imgPath ));

 

} Catch (Exception e ){

E. printStackTrace ();

}

}

 

/**

* @ Param args

*/

Public static void main (String [] args ){

String imgPath = "d:/test/twocode/michael_zxing.png ";

String contents = "Hello Michael (large), welcome to Zxing! "

+ "\ NMichael's blogs [http://sjsky.iteye.com]"

+ "\ NEMail [sjsky007@gmail.com]" + "\ nTwitter [@ suncto]";

Int width = 300, height = 300;

ZxingEncoderHandler handler = new ZxingEncoderHandler ();

Handler. encode (contents, width, height, imgPath );

 

System. out. println ("Michael, you have finished zxing encode .");

}

}

The QR code generated after running is shown as follows:

 

Like in the previous article, I used the QR code scanning software of my mobile phone (I used the android quick Bi QR code) to test the recognition success as follows:

 

 

2-2. decoding example:

Java code

Package michael. zxing;

 

Import java. awt. image. BufferedImage;

Import java. io. File;

Import java. util. Hashtable;

 

Import javax. imageio. ImageIO;

 

Import com. google. zxing. BinaryBitmap;

Import com. google. zxing. DecodeHintType;

Import com. google. zxing. LuminanceSource;

Import com. google. zxing. MultiFormatReader;

Import com. google. zxing. Result;

Import com. google. zxing. client. j2se. BufferedImageLuminanceSource;

Import com. google. zxing. common. HybridBinarizer;

 

/**

* @ Blog http://sjsky.iteye.com

* @ Author Michael

*/

Public class ZxingDecoderHandler {

 

/**

* @ Param imgPath

* @ Return String

*/

Public String decode (String imgPath ){

BufferedImage image = null;

Result result = null;

Try {

Image = ImageIO. read (new File (imgPath ));

If (image = null ){

System. out. println ("the decode image may not exit .");

}

LuminanceSource source = new BufferedImageLuminanceSource (image );

BinaryBitmap bitmap = new BinaryBitmap (new HybridBinarizer (source ));

 

Hashtable <Object, Object> hints = new Hashtable <Object, Object> ();

Hints. put (DecodeHintType. CHARACTER_SET, "GBK ");

 

Result = new MultiFormatReader (). decode (bitmap, hints );

Return result. getText ();

} Catch (Exception e ){

E. printStackTrace ();

}

Return null;

}

 

/**

* @ Param args

*/

Public static void main (String [] args ){

String imgPath = "d:/test/twocode/michael_zxing.png ";

ZxingDecoderHandler handler = new ZxingDecoderHandler ();

String decodeContent = handler. decode (imgPath );

System. out. println ("the decoded content is as follows :");

System. out. println (decodeContent );

System. out. println ("Michael, you have finished zxing decode .");

 

}

}

 

 

The running result is as follows:

 

The decoded content is as follows:

Hello Michael (BIG), welcome to Zxing!

Michael's blog [http://sjsky.iteye.com]

EMail [sjsky007@gmail.com]

Twitter [@ suncto]

Michael, you have finished zxing decode.

From the test results, we can see that the decoded content is consistent with the previously encoded content.

 

[3], bar code (EAN-13) encoding and decoding Demonstration:

 

3-1. Encoding example:

Java code

Package michael. zxing;

 

Import java. io. File;

 

Import com. google. zxing. BarcodeFormat;

Import com. google. zxing. MultiFormatWriter;

Import com. google. zxing. client. j2se. MatrixToImageWriter;

Import com. google. zxing. common. BitMatrix;

 

/**

* @ Blog http://sjsky.iteye.com

* @ Author Michael

*/

Public class ZxingEAN13EncoderHandler {

 

/**

* Encoding

* @ Param contents

* @ Param width

* @ Param height

* @ Param imgPath

*/

Public void encode (String contents, int width, int height, String imgPath ){

Int codeWidth = 3 + // start guard

(7*6) + // left bars

5 + // middle guard

(7*6) + // right bars

3; // end guard

CodeWidth = Math. max (codeWidth, width );

Try {

BitMatrix bitMatrix = new MultiFormatWriter (). encode (contents,

BarcodeFormat. EAN_13, codeWidth, height, null );

 

MatrixToImageWriter

. WriteToFile (bitMatrix, "png", new File (imgPath ));

 

} Catch (Exception e ){

E. printStackTrace ();

}

}

 

/**

* @ Param args

*/

Public static void main (String [] args ){

String imgPath = "d:/test/twocode/zxing_EAN13.png ";

// Yida sugar-free chewing gum bar code

String contents = "6923450657713 ";

 

Int width = 105, height = 50;

ZxingEAN13EncoderHandler handler = new ZxingEAN13EncoderHandler ();

Handler. encode (contents, width, height, imgPath );

 

System. out. println ("Michael, you have finished zxing EAN13 encode .");

}

}

923450 657713 corresponds to Yida sugar-free chewing gum:

 

 

The generated barcode image after running is as follows:

 

The mobile phone scanning software successfully identified the following:

 

3-2. decoding example:

Java code

Package michael. zxing;

 

Import java. awt. image. BufferedImage;

Import java. io. File;

 

Import javax. imageio. ImageIO;

 

Import com. google. zxing. BinaryBitmap;

Import com. google. zxing. LuminanceSource;

Import com. google. zxing. MultiFormatReader;

Import com. google. zxing. Result;

Import com. google. zxing. client. j2se. BufferedImageLuminanceSource;

Import com. google. zxing. common. HybridBinarizer;

 

/**

* @ Blog http://sjsky.iteye.com

* @ Author Michael

*/

Public class ZxingEAN13DecoderHandler {

 

/**

* @ Param imgPath

* @ Return String

*/

Public String decode (String imgPath ){

BufferedImage image = null;

Result result = null;

Try {

Image = ImageIO. read (new File (imgPath ));

If (image = null ){

System. out. println ("the decode image may not exit .");

}

LuminanceSource source = new BufferedImageLuminanceSource (image );

BinaryBitmap bitmap = new BinaryBitmap (new HybridBinarizer (source ));

 

Result = new MultiFormatReader (). decode (bitmap, null );

Return result. getText ();

} Catch (Exception e ){

E. printStackTrace ();

}

Return null;

}

 

/**

* @ Param args

*/

Public static void main (String [] args ){

String imgPath = "d:/test/twocode/zxing_EAN13.png ";

ZxingEAN13DecoderHandler handler = new ZxingEAN13DecoderHandler ();

String decodeContent = handler. decode (imgPath );

System. out. println ("the decoded content is as follows :");

System. out. println (decodeContent );

System. out. println ("Michael, you have finished zxing EAN-13 decode .");

 

}

}

The running result is as follows:

Write

The decoded content is as follows:

6923450657713

Michael, you have finished zxing decode.

 

From the test results, we can see that the decoded content is consistent with the previously encoded content.

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.