The code is as follows:
1.maven Jar Required
<dependency>
<groupId>com.google.zxing</groupId>
<artifactid>core</ artifactid>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
< Version>3.0.0</version>
</dependency>
2. Generate two-dimensional code:
Package com;
Import Java.io.File;
Import java.io.IOException;
Import Java.nio.file.Path;
Import java.util.Hashtable;
Import Com.google.zxing.BarcodeFormat;
Import Com.google.zxing.EncodeHintType;
Import Com.google.zxing.MultiFormatWriter;
Import com.google.zxing.WriterException;
Import Com.google.zxing.client.j2se.MatrixToImageWriter;
Import Com.google.zxing.common.BitMatrix;
Import Com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public static void Main (string[] args) throws IOException {String text= "www.baidu.com";
int width=100;
int height=100;
String format= "PNG";
Hashtable hints=new Hashtable ();
Hints.put (Encodehinttype.character_set, "utf-8");
Hints.put (Encodehinttype.error_correction, ERRORCORRECTIONLEVEL.M);
Hints.put (Encodehinttype.margin, 2);
try {Bitmatrix bitmatrix=new multiformatwriter (). Encode (text, Barcodeformat.qr_code, width, height,hints); Path file=new Java.io.File ("D:/new.png"). Topath ()//Generate two-dimensional code picture in D disk MatrixtoimAgewriter.writetopath (Bitmatrix, format, file); catch (Writerexception e) {//TODO auto-generated catch block E.printstacktrace ();}
3. Parsing two-dimensional code:
Package com;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import java.util.Hashtable;
Import Javax.imageio.ImageIO;
Import Com.google.zxing.BinaryBitmap;
Import Com.google.zxing.EncodeHintType;
Import Com.google.zxing.MultiFormatReader;
Import com.google.zxing.NotFoundException;
Import Com.google.zxing.Result;
Import Com.google.zxing.client.j2se.BufferedImageLuminanceSource;
Import Com.google.zxing.common.HybridBinarizer; public static void Main (string[] args) throws notfoundexception {Multiformatreader formatreader=new multiformatreader
();
File file =new file ("D:/new.png");
BufferedImage Image=null;
try {image = Imageio.read (file);
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
} binarybitmap binarybitmap =new binarybitmap (new Hybridbinarizer (image));
Hashtable hints=new Hashtable (); Hints.put (Encodehinttype.character_set, "UTF-8 ");
Result Result=formatreader.decode (binarybitmap,hints);
System.err.println ("Analytic Result:" +result.tostring ());
System.out.println (Result.getbarcodeformat ());
System.out.println (Result.gettext ()); }