zxing realization of two-dimensional code generation and parsing

Source: Internet
Author: User

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
    1. The class in which the output image and the read image are in the core package
    2. Multiformatreader
    3. Multiformatwriter
    4. The class that generated the matrix is in Javase's bag.
    5. Matrixtoimagewriter

The configuration in Pom.xml is

XML code
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "
  2. xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
  3. <modelversion>4.0.0</modelversion>
  4. <groupId>com.shihy</groupId>
  5. <artifactid>qrcode</artifactid>
  6. <version>0.0.1-snapshot</version>
  7. <packaging>jar</packaging>
  8. <name>qrcode</name>
  9. <URL>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
  12. </Properties>
  13. <dependencies>
  14. <dependency>
  15. <groupId>com.google.zxing</groupId>
  16. <artifactid>core</artifactid>
  17. <version>3.0.0</version>
  18. </Dependency>
  19. <dependency>
  20. <groupId>com.google.zxing</groupId>
  21. <artifactid>javase</artifactid>
  22. <version>3.0.0</version>
  23. </Dependency>
  24. <dependency>
  25. <groupId>junit</groupId>
  26. <artifactid>junit</artifactid>
  27. <version>4.10</version>
  28. </Dependency>
  29. <dependency>
  30. <groupId>com.alibaba</groupId>
  31. <artifactid>fastjson</artifactid>
  32. <version>1.1.29</version>
  33. </Dependency>
  34. </Dependencies>
  35. </Project>

Test tool classes for generating images and parsing images:

Java code
  1. Package Com.polysaas.edu.qrcode;
  2. Import Java.awt.image.BufferedImage;
  3. Import Java.io.File;
  4. Import java.io.IOException;
  5. Import Java.nio.file.FileSystems;
  6. Import Java.nio.file.Path;
  7. Import Java.util.HashMap;
  8. Import Java.util.Map;
  9. Import Javax.imageio.ImageIO;
  10. Import Org.junit.Test;
  11. Import Com.alibaba.fastjson.JSONObject;
  12. Import Com.google.zxing.BarcodeFormat;
  13. Import Com.google.zxing.Binarizer;
  14. Import Com.google.zxing.BinaryBitmap;
  15. Import Com.google.zxing.DecodeHintType;
  16. Import Com.google.zxing.EncodeHintType;
  17. Import Com.google.zxing.LuminanceSource;
  18. Import Com.google.zxing.MultiFormatReader;
  19. Import Com.google.zxing.MultiFormatWriter;
  20. Import com.google.zxing.NotFoundException;
  21. Import Com.google.zxing.Result;
  22. Import com.google.zxing.WriterException;
  23. Import Com.google.zxing.client.j2se.BufferedImageLuminanceSource;
  24. Import Com.google.zxing.client.j2se.MatrixToImageWriter;
  25. Import Com.google.zxing.common.BitMatrix;
  26. Import Com.google.zxing.common.HybridBinarizer;
  27. Public class Qrcodetest {
  28. /** 
  29. * Generate images
  30. *
  31. * @throws writerexception
  32. * @throws IOException
  33. */
  34. @Test
  35. public void Testencode () throws Writerexception, IOException {
  36. String FilePath = "d://";
  37. String fileName = "Zxing.png";
  38. Jsonobject JSON = new Jsonobject ();
  39. Json.put (
  40. "Zxing",
  41. "https://github.com/zxing/zxing/tree/zxing-3.0.0/javase/src/main/java/com/google/zxing");
  42. Json.put ("Author", "Shihy");
  43. String content = json.tojsonstring (); //Content
  44. int width = 200; //Image width
  45. int height = 200; //Image height
  46. String format = "png"; Image Type
  47. Map<encodehinttype, object> hints = new Hashmap<encodehinttype, object> ();
  48. Hints.put (Encodehinttype.character_set, "UTF-8");
  49. Bitmatrix Bitmatrix = new Multiformatwriter (). Encode (content,
  50. Barcodeformat.qr_code, width, height, hints); //Generate matrix
  51. Path PATH = Filesystems.getdefault (). GetPath (FilePath, fileName);
  52. Matrixtoimagewriter.writetopath (bitmatrix, format, path); //Output image
  53. System.out.println ("output succeeded.");
  54. }
  55. /** 
  56. * Parse Image
  57. */
  58. @Test
  59. public void Testdecode () {
  60. String FilePath = "D://zxing.png";
  61. BufferedImage image;
  62. try {
  63. Image = Imageio.read (new File (FilePath));
  64. Luminancesource Source = new Bufferedimageluminancesource (image);
  65. Binarizer Binarizer = new Hybridbinarizer (source);
  66. Binarybitmap Binarybitmap = new Binarybitmap (Binarizer);
  67. Map<decodehinttype, object> hints = new Hashmap<decodehinttype, object> ();
  68. Hints.put (Decodehinttype.character_set, "UTF-8");
  69. Result result = new Multiformatreader (). Decode (Binarybitmap, hints); Decoding an image
  70. Jsonobject content = Jsonobject.parseobject (Result.gettext ());
  71. System.out.println ("content in the Picture:");
  72. System.out.println ("Author:" + content.getstring ("author"));
  73. System.out.println ("zxing:" + content.getstring ("zxing"));
  74. SYSTEM.OUT.PRINTLN ("format in Picture:");
  75. System.out.println ("encode:" + result.getbarcodeformat ());
  76. } catch (IOException e) {
  77. E.printstacktrace ();
  78. } catch (Notfoundexception e) {
  79. E.printstacktrace ();
  80. }
  81. }
  82. }

The test generates the image as:


zxing realization of two-dimensional code generation and parsing

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.