Java uses google.zxing, solving two-dimensional code, barcode

Source: Internet
Author: User
Tags gettext

Zxing is a Google dedicated to the compilation, solution two-dimensional code, barcode, open source project, the use and operation are very convenient. Zxing does not support Java and supports other classes of applications such as Android, CPP, C #, IPhone, J2ME, J2SE, JRuby, OBJC, Rim, Symbian, and more. You can add the required class libraries to your needs. This article refers to some of the types in http://www.open-open.com/lib/view/open1345039836522.html.

The first step: Google's official website http://code.google.com/p/zxing/download zxing class library, download it after the decompression. Compressed file box to extract the files and extract the file directory as follows:


Zxing Class Library files


Step Two: Copy all the files under the Zxing-master\core\src\main\java directory and the Zxing-master\javase\src\main\java directory to your project.

At this point your project structure is shown in the following illustration:


Because the Zxing class library is more, may sometimes confuse with own package. There are two options at this time: first, in the current project development of their own projects, their own package creation and zxing class library distinct; the second is to package the package into a jar file, and create a project to develop your own project, as long as you copy the packaged jar files to the newly created project.

Here we use the second method to package the project into a jar file. Using Eclipse's own packaging tools, you can package your project into a jar file. The specific steps are as follows:

1. Right-click item selection---"Export"

2. Select Java->jar File

3, click Next, select the Jar file placement path, and select the relevant options, the following figure:


4. Click Finish to copy the jar file to your project under the specified path.

The third step: the coding and decoding of two-dimensional code, the sample code is as follows:

Package Com.xie.qrcode;


Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Javax.imageio.ImageIO;


Import Com.google.zxing.BarcodeFormat;
Import Com.google.zxing.Binarizer;
Import Com.google.zxing.BinaryBitmap;
Import Com.google.zxing.LuminanceSource;
Import Com.google.zxing.Result;
Import Com.google.zxing.client.j2se.BufferedImageLuminanceSource;
Import Com.google.zxing.client.j2se.MatrixToImageWriter;
Import Com.google.zxing.common.BitMatrix;
Import Com.google.zxing.common.HybridBinarizer;
Import Com.google.zxing.qrcode.QRCodeReader;
Import Com.google.zxing.qrcode.QRCodeWriter;


public class Qrzxing {


@SuppressWarnings ("deprecation")
public static void Main (string[] args) throws Exception {

/** two dimensional code generation **/

/** two dimensional code content **/
String content = "Child, hello zxing";

/** content format, if have Chinese words must transcoding, **/
Content = new String (content.getbytes ("UTF-8"), "iso-8859-1");

/** Two-dimensional code picture save path **/
String ImagePath = "D:\\qrcode.jpg";
File File = new file (ImagePath);

/** constructs the two-dimensional code, the width height respectively is 200, 200**/
Qrcodewriter writer = new Qrcodewriter ();
Bitmatrix matrix = writer.encode (content, Barcodeformat.qr_code, 200, 200);
Matrixtoimagewriter.writetofile (Matrix, "JPG", file);
------------------------------------------------------------------------>
/** two dimensional code parsing **/
Qrcodereader reader = new Qrcodereader ();

/** get two-dimensional code picture object **/
BufferedImage image = Imageio.read (file);
Luminancesource Source = new Bufferedimageluminancesource (image);
Binarizer Binarizer = new Hybridbinarizer (source);
Binarybitmap Imagebinarybitmap = new Binarybitmap (Binarizer);
Result result = Reader.decode (IMAGEBINARYBITMAP);

SYSTEM.OUT.PRINTLN ("result =" + result.tostring ());
System.out.println ("Resultformat =" + Result.getbarcodeformat ());
System.out.println ("Resulttext =" + Result.gettext ());

}
}

Generated two-dimensional code:


The results of two-dimensional code parsing are as follows:



Barcode encoding, decoding and two-dimensional code encoding, decoding similar, here only posted its code.

Package Com.xie.qrcode;


Import Java.awt.image.BufferedImage;
Import Java.io.File;


Import Javax.imageio.ImageIO;
Import Com.google.zxing.BarcodeFormat;
Import Com.google.zxing.BinaryBitmap;
Import Com.google.zxing.LuminanceSource;
Import Com.google.zxing.MultiFormatReader;
Import Com.google.zxing.MultiFormatWriter;
Import Com.google.zxing.Result;
Import Com.google.zxing.client.j2se.BufferedImageLuminanceSource;
Import Com.google.zxing.client.j2se.MatrixToImageWriter;
Import Com.google.zxing.common.BitMatrix;
Import Com.google.zxing.common.HybridBinarizer;


public class BarCode {


/** Barcode Coding **/
public void Encode (string contents, int width, int height, string imgpath) {
int codewidth = 3 + (7 * 6) + 5 + (7 * 6) + 3;
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 ();
}
}


/** Barcode Decoding **/
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));
result = new Multiformatreader (). Decode (bitmap, NULL);
return Result.gettext ();
catch (Exception e) {
E.printstacktrace ();
}
return null;
}


public static void Main (string[] args) {


/** Coding **/
String Imgpath = "D:/zxing_ean13.png";
Bar code for sugar-free chewing gum
String contents = "6923450657713";


int width = +, height = 50;
BarCode BarCode = new BarCode ();
Barcode.encode (contents, width, height, imgpath);


/** decoding **/
String decodecontent = Barcode.decode (Imgpath);
System.out.println ("Decode the contents as follows:");
System.out.println (decodecontent);
}
}

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.