Java two-dimensional code, barcode, "zxing"

Source: Internet
Author: User
Tags gettext

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.


The sample code needs to use the jar package Zxing-2.0.zip the Core.jar,javase.jar below.

Two-dimensional code coding, decoding

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 h
		Eight, String Imgpath) {hashtable<encodehinttype, object> hints = new Hashtable<encodehinttype, Object> ();
		Specifies the error correction level Hints.put (encodehinttype.error_correction, ERRORCORRECTIONLEVEL.L);
		Specifies 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 = "Zxing.png";
				String contents = "Hello Michael (Large), Welcome to zxing!"
		+ "\nmichael ' s blog [http://sjsky.iteye.com]" + "\nemail [sjsky007@gmail.com]" + "\ntwitter [@suncto]";
		int width = +, height = 300;
		Zxingencoderhandler handler = new Zxingencoderhandler ();

		Handler.encode (contents, width, height, imgpath);
	System.out.println ("Michael, you have finished zxing encode."); }
}

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 {/** * @p Aram 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 is not exit."); } luminancesource Source = new BufferedimageluminancesOurce (image);  
  
            Binarybitmap bitmap = new Binarybitmap (new Hybridbinarizer (source));  
            Hashtable<decodehinttype, object> hints = new Hashtable<decodehinttype, 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 = "Zxi  
        Ng.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, you have finished zxing decode.");  }  
}

Barcode coding, decoding

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 {/**  * Code * @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 Imgpat  
        h = "Zxing_ean13.png";  
  
        Barcode String contents = "6923450657713" for sugar-free chewing gum;  
        int width = +, height = 50;  
        Zxingean13encoderhandler handler = new Zxingean13encoderhandler ();  
  
        Handler.encode (contents, width, height, imgpath);  
    System.out.println ("Michael, you have finished zxing EAN13 encode."); }  
}

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 ima  
        GE = null;  
        result result = NULL;  
            try {image = Imageio.read (new File (Imgpath));  
            if (image = = null) {SYSTEM.OUT.PRINTLN ("The decode image May is 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 = "Zxi  
        Ng_ean13.png ";  
        Zxingean13decoderhandler handler = new Zxingean13decoderhandler ();  
        String decodecontent = Handler.decode (Imgpath);  
        System.out.println ("Decode the contents as follows:");  
        System.out.println (decodecontent);  
  
    System.out.println ("Michael, you have finished zxing EAN-13 decode.");  }  
}


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.