Java based on Maven to realize zxing generation of two-dimensional code __java

Source: Internet
Author: User
Tags string format

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 ()); }

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.