Import Java.awt.image.bufferedimage;import java.io.file;import java.io.ioexception;import java.util.Hashtable; 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.readerexception;import Com.google.zxing.Result;import Com.google.zxing.client.j2se.bufferedimageluminancesource;import Com.google.zxing.common.bitmatrix;import com.google.zxing.common.hybridbinarizer;/** * Using zxing Open Source tool to generate QR code qrcode * * @date 2012-10-26 * @author XHW * */public class QRCode {private static final int BLACK = 0xff000000;private static final int white = 0xffffffff;/** * @param args */publi c static void Main (string[] args) {/** * in Com.google.zxing.MultiFormatWriter class, defines some codes we don't know, QR code is just one of the <br> * Public Bitmatrix EncOde (String contents, barcodeformat format, int width, int height, Map<encodehinttype,?> hints) throws writerexception {writer writer; Switch (format) {case ean_8:writer = new Ean8writer (); Break Case ean_13:writer = new Ean13writer (); Break Case upc_a:writer = new Upcawriter (); Break Case qr_code:writer = new Qrcodewriter (); Break Case code_39:writer = new Code39writer (); Break Case code_128:writer = new Code128writer (); Break Case itf:writer = new Itfwriter (); Break Case pdf_417:writer = new Pdf417writer (); Break Case codabar:writer = new Codabarwriter (); Break Default:throw new IllegalArgumentException ("No encoder available for format" + format); } return Writer.encode (contents, format, Width, height, hints); } */string filepostfix= "PNG"; File File = new file ("C://test_qr_code.") +filepostfix); Qrcode.encode ("Http://www.cnblogs.com/sprinng", File,filepostfix, Barcodeformat.qr_code, $, a, null); Qrcode.decode (file);} /** * Generate qrcode QR code <br> * required to encode Com.google.zxing.qrcode.encoder.Encoder.java <br> static final String DE fault_byte_mode_encoding = "Iso8859-1";<br> * modified to UTF-8, otherwise Chinese compilation cannot parse <br> * @param contents QR Code contents * @param file QR code to save the path, such as: C://test_qr_code.png * @param filepostfix generated two-dimensional code picture format: Png,jpeg,gif and other formats * @param format QRCode code generation formats * @param wid Th picture Width * @param height picture altitude * @param hints */public static void encode (String contents, File file,string filepostfix, Bar Codeformat format, int width, int height, map<encodehinttype,?> hints) {try {Bitmatrix Bitmatrix = new MULTIFORMATW Riter (). Encode (contents, format, width, height); WriteToFile (Bitmatrix, filepostfix, file);} catch (Exception e) {e.printstacktrace ();}} /** * Generate two-dimensional code images <br> * * @param matrix * @param format * Picture format * @param file * Generate QR Code picture location * @throws IOException */public STA tic void WriteToFile (Bitmatrix matrix, String format, file file) throws IOException {bufferedimage image = Tobufferedimage (matrix); Imageio.write (image, format, file);} /** * Generate QR Code content <br> * * @param matrix * @return */public static bufferedimage tobufferedimage (Bitmatrix matrix) {int width = matrix.getwidth (); int height = matrix.getheight (); BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_argb); for (INT x = 0; x < width; + +) {fo R (int y = 0; y < height; y++) {Image.setrgb (x, Y, Matrix.get (x, y) = = true? black:white);}} return image; /** * Parse qrcode QR code */@SuppressWarnings ("unchecked") public static void decode (file file) {try {bufferedimage image;try {ima GE = imageio.read (file), if (image = = null) {System.out.println ("Could not decode image");} Luminancesource Source = new Bufferedimageluminancesource (image); Binarybitmap bitmap = New Binarybitmap (new Hybridbinarizer (source)); Result result, @SuppressWarnings ("Rawtypes") Hashtable hints = new Hashtable ();//decoding setting is encoded by: Utf-8hints.put ( Decodehinttype.character_set, "utf-8"); result = new Multiformatreader (). Decode (bitmap, hints); String resultstr = Result.gettext (); SYSTEM.OUT.PRINTLN ("Parsed content:" + resultstr);} catch (IOException IoE) {System.out.println (ioe.tostring ());} catch (Readerexception re) {System.out.println ( Re.tostring ());}} catch (Exception ex) {System.out.println (ex.tostring ());}}}
Jar Package Download
Java QR code generation tool