Zxing is an open source Java class library for parsing barcodes and two-dimensional code in multiple formats __java

Source: Internet
Author: User

Blog migration to: http://www.micmiu.com

Following the introduction of a Japanese open source software (the software can only achieve QRCode) Original: Java implementation of two-dimensional code QRCode coding and decoding (http://sjsky.iteye.com/blog/1136934), today found another excellent open source weapon--zxing , it is more flexible and convenient, and can realize many coding formats.

Full-Text catalog: Basic description of two-dimensional code (such as: qrcode) encoding and decoding demo barcode (such as: EAN-13) encoding and decoding demo

"One", the basic introduction:

1-1. Zxing is an open source Java class Library used to parse barcodes and two-dimensional codes in multiple formats.

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

Up to date, the latest version is 1.7, providing support for the following coding formats: Upc-a and UPC-E EAN-8 and EAN-13 code-code-code 128 QR code ITF codabar RSS-14 (all Vari Ants) Data Matrix PDF 417 (' alpha ' quality) Aztec (' alpha ' quality)

At the same time, the official website provides Android, CPP, C #, IPhone, J2ME, J2SE, JRuby, OBJC, RIM, Symbian and other applications of the class library, specific details can refer to the download of the source package.

1-2. This article, like the previous article, mainly in the implementation of the PC Barcode (EAN-13) and two-dimensional code (QRCODE) encoding and decoding examples, for everyone to reference, the source code in the core and javase the relevant sources, the attachment to provide their own compiled LIB package: Zxing.jar Zxing-j2se.jar

For all kinds of mobile phone system applications, interested friends can download the official source code package, under the package has specific detailed application introduction.

"Two", Two-dimensional code (QRCODE) encoding and decoding demo:

 2-1. Coding 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 {           /**       *  code         *  @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 error correction level            hints.put (Encodehinttype.error_correction, &NBSP;ERRORCORRECTIONLEVEL.L);           //  Specify encoding format             hints.put (encodehinttype.character_set,  "GBK");            try {            &nBsp;   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 blog [ http://sjsky.iteye.com ]"                     +  "\nemail [  SJSKY007@GMAIL.COM&NBSP] " + " \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 two-dimensional code picture that is generated after the run is as follows:



The same as the previous introduction, with the mobile phone two-dimensional code scanning software (I use: Android quick two-dimensional code) to test, recognition of the success of the screenshot is 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 be 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 ("Decode the contents as follows:");           system.out.println ( decodecontent);           system.out.println ("MICHAEL&NBSP; You have finished zxing decode. ");           }  }    

The results of the operation are as follows:

The decoding contents are 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.

Visible from test results: decoded content is consistent with previously encoded content

"Three", Barcode (EAN-13) encoding and decoding demo:

 3-1. Coding 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 {          /**        *  coding        *  @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";            //  sugar-free gum barcode             String contents =  "6923450657713";               int width = 105, height = 50;           ZxingEAN13EncoderHandler handler = new  Zxingean13encoderhandler ();           handler.encode (contents ,  width,&Nbsp;height, imgpath);               System.out.println ("Michael ,you have finished zxing ean13 encode.");        }  }  

6 923450 657713 corresponds to the benefit of sugar-free chewing gum:


The barcode picture that is generated after running is as follows:


Using the mobile phone's scanning software, the screenshot of successful recognition is as follows:


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   *&

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.