Original: http://www.open-open.com/code/view/1430906793866
ImportJava.io.File;Importjava.util.Hashtable; ImportCom.google.zxing.BarcodeFormat;ImportCom.google.zxing.EncodeHintType;ImportCom.google.zxing.MultiFormatWriter;ImportCom.google.zxing.client.j2se.MatrixToImageWriter;ImportCom.google.zxing.common.BitMatrix; //Coding Public classQrencoder { Public Static voidMain (string[] args)throwsException {String content= "Hello World"; String encoding= "Utf-8"; intwidth = 150; intHeight = 150; String format= "JPG"; File ImagePath=NewFile ("D:/test.jpg"); Com.google.zxing.Writer Writer=NewMultiformatwriter (); Hashtable<encodehinttype, string> hints =NewHashtable<encodehinttype, string> (2); Hints.put (encodehinttype.character_set, encoding); Bitmatrix Matrix=Writer.encode (content, Barcodeformat.qr_code, width, height, hints); Matrixtoimagewriter.writetofile (Matrix, Format, ImagePath); } } ImportJava.io.File;Importjava.util.Hashtable; ImportJavax.imageio.ImageIO; ImportCom.google.zxing.Binarizer;ImportCom.google.zxing.BinaryBitmap;ImportCom.google.zxing.DecodeHintType;ImportCom.google.zxing.LuminanceSource;ImportCom.google.zxing.MultiFormatReader;ImportCom.google.zxing.Reader;ImportCom.google.zxing.Result;ImportCom.google.zxing.client.j2se.BufferedImageLuminanceSource;ImportCom.google.zxing.common.GlobalHistogramBinarizer; //decoding Public classQrdecoder { Public Static voidMain (string[] args)throwsException {File ImagePath=NewFile ("D:/test.jpg"); String encoding= "Utf-8"; Reader Reader=NewMultiformatreader (); Luminancesource ls=NewBufferedimageluminancesource (Imageio.read (ImagePath)); Binarizer b=Newglobalhistogrambinarizer (LS); Hashtable<decodehinttype, string> hint1 =NewHashtable<decodehinttype, string> (2); Hint1.put (decodehinttype.character_set, encoding); Result R= Reader.decode (NewBinarybitmap (b), hint1); System.out.println (R.gettext ()); } }
Decoding and encoding of Java QR code