zxing realization of two-dimensional code generation and parsing
Blog Category:
- Two-dimensional code zxing
Generation and analysis of two-dimensional code. There are many ways. I choose to use the big brand, Google boss Zxing.
The GitHub link is (I'm using 3.0.0, it's NiO already)
https://github.com/zxing/zxing/tree/zxing-3.0.0
Java code
- The class in which the output image and the read image are in the core package
- Multiformatreader
- Multiformatwriter
- The class that generated the matrix is in Javase's bag.
- Matrixtoimagewriter
The configuration in Pom.xml is
XML code
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "
- xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
- <modelversion>4.0.0</modelversion>
- <groupId>com.shihy</groupId>
- <artifactid>qrcode</artifactid>
- <version>0.0.1-snapshot</version>
- <packaging>jar</packaging>
- <name>qrcode</name>
- <URL>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
- </Properties>
- <dependencies>
- <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>
- <dependency>
- <groupId>junit</groupId>
- <artifactid>junit</artifactid>
- <version>4.10</version>
- </Dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactid>fastjson</artifactid>
- <version>1.1.29</version>
- </Dependency>
- </Dependencies>
- </Project>
Test tool classes for generating images and parsing images:
Java code
- Package Com.polysaas.edu.qrcode;
- Import Java.awt.image.BufferedImage;
- Import Java.io.File;
- Import java.io.IOException;
- Import Java.nio.file.FileSystems;
- Import Java.nio.file.Path;
- Import Java.util.HashMap;
- Import Java.util.Map;
- Import Javax.imageio.ImageIO;
- Import Org.junit.Test;
- Import Com.alibaba.fastjson.JSONObject;
- Import Com.google.zxing.BarcodeFormat;
- Import Com.google.zxing.Binarizer;
- Import Com.google.zxing.BinaryBitmap;
- Import Com.google.zxing.DecodeHintType;
- Import Com.google.zxing.EncodeHintType;
- Import Com.google.zxing.LuminanceSource;
- Import Com.google.zxing.MultiFormatReader;
- Import Com.google.zxing.MultiFormatWriter;
- Import com.google.zxing.NotFoundException;
- Import Com.google.zxing.Result;
- Import com.google.zxing.WriterException;
- 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 Qrcodetest {
- /**
- * Generate images
- *
- * @throws writerexception
- * @throws IOException
- */
- @Test
- public void Testencode () throws Writerexception, IOException {
- String FilePath = "d://";
- String fileName = "Zxing.png";
- Jsonobject JSON = new Jsonobject ();
- Json.put (
- "Zxing",
- "https://github.com/zxing/zxing/tree/zxing-3.0.0/javase/src/main/java/com/google/zxing");
- Json.put ("Author", "Shihy");
- String content = json.tojsonstring (); //Content
- int width = 200; //Image width
- int height = 200; //Image height
- String format = "png"; Image Type
- Map<encodehinttype, object> hints = new Hashmap<encodehinttype, object> ();
- Hints.put (Encodehinttype.character_set, "UTF-8");
- Bitmatrix Bitmatrix = new Multiformatwriter (). Encode (content,
- Barcodeformat.qr_code, width, height, hints); //Generate matrix
- Path PATH = Filesystems.getdefault (). GetPath (FilePath, fileName);
- Matrixtoimagewriter.writetopath (bitmatrix, format, path); //Output image
- System.out.println ("output succeeded.");
- }
- /**
- * Parse Image
- */
- @Test
- public void Testdecode () {
- String FilePath = "D://zxing.png";
- BufferedImage image;
- try {
- Image = Imageio.read (new File (FilePath));
- Luminancesource Source = new Bufferedimageluminancesource (image);
- Binarizer Binarizer = new Hybridbinarizer (source);
- Binarybitmap Binarybitmap = new Binarybitmap (Binarizer);
- Map<decodehinttype, object> hints = new Hashmap<decodehinttype, object> ();
- Hints.put (Decodehinttype.character_set, "UTF-8");
- Result result = new Multiformatreader (). Decode (Binarybitmap, hints); Decoding an image
- Jsonobject content = Jsonobject.parseobject (Result.gettext ());
- System.out.println ("content in the Picture:");
- System.out.println ("Author:" + content.getstring ("author"));
- System.out.println ("zxing:" + content.getstring ("zxing"));
- SYSTEM.OUT.PRINTLN ("format in Picture:");
- System.out.println ("encode:" + result.getbarcodeformat ());
- } catch (IOException e) {
- E.printstacktrace ();
- } catch (Notfoundexception e) {
- E.printstacktrace ();
- }
- }
- }
The test generates the image as:
zxing realization of two-dimensional code generation and parsing