Use zxing to encode and decode barcodes, two-dimensional codes and __

Source: Internet
Author: User
Tags gettext

First, add the jar pack

    <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>

First, the operation of the bar code

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

Import Java.nio.file.Path;

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 Eancode {public static void encode (string contents, int width, int height, String imgpath) {in  
                T codewidth = 3 +//Start Guard (7 * 6) +//Left bars 5 +//middle Guard (7 * 6) +//right bars 3;  
        End Guard Codewidth = Math.max (codewidth, width); try {Bitmatrix Bitmatrix = new Multiformatwriter(). Encode (contents, barcodeformat.ean_13, codewidth, height, null);
            Path PATH = new File (Imgpath). Topath ();
  
        Matrixtoimagewriter.writetopath (Bitmatrix, "PNG", path);  
        catch (Exception e) {e.printstacktrace ();  
        } public static 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;  
    }/** * @param args */public static void main (string[] args) {String Imgpat  
        h = "D:/123.jpg";  
        String contents = "6943620593115";  
        int width = +, height = 50;
    	
    	Encode (contents, width, height, imgpath);  
        String imgPath2 = "D:/123.jpg";  
        String decodecontent = decode (imgPath2);  
        System.out.println ("Decode the contents as follows:");  
    System.out.println (decodecontent); }
}

Second, the two-dimensional code operation

Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Java.nio.file.Path;
Import Java.util.HashMap;

Import Java.util.Map;

Import Javax.imageio.ImageIO;
Import Com.google.zxing.BarcodeFormat;
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.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.decoder.ErrorCorrectionLevel; public class QRCode {/** * code * @param contents * @param width * @param height * @param im Gpath */public static void encode (string contents, int width, int height, String imgpath) {map<enCodehinttype, object> hints = new Hashmap<encodehinttype, object> ();  
        Specifies the error correction level Hints.put (encodehinttype.error_correction, ERRORCORRECTIONLEVEL.L);  
        Specifies the encoding format hints.put (encodehinttype.character_set, "UTF-8"); try {Bitmatrix Bitmatrix = new Multiformatwriter (). Encode (contents, barcodeformat.qr_  
            CODE, width, height, hints);
            Path PATH = new File (Imgpath). Topath ();  
        Matrixtoimagewriter.writetopath (Bitmatrix, "PNG", path);  
        catch (Exception e) {e.printstacktrace ();  
        } public static 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)); 
            Map<decodehinttype, object> hints = new Hashmap<decodehinttype, object> ();  
  
            Hints.put (Decodehinttype.character_set, "UTF-8");  
            result = new Multiformatreader (). Decode (bitmap, hints);  
        return Result.gettext ();  
        catch (Exception e) {e.printstacktrace ();  
    return null; }/** * @param args */public static void main (string[] args) {String Imgpath = "d:/  
        Michael_zxing.png ";  
        String contents = "http://www.paihang.com";  
        int width = +, height = 300;
        
        Encode (contents, width, height, imgpath);  
        String imgPath2 = "D:/michael_zxing.png";  
        String decodecontent = decode (imgPath2);  
        System.out.println ("Decode the contents as follows:");  
    System.out.println (decodecontent); }
  
}

As for more features not to be studied, simply try the basic operation

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.