Directly on the code:
Two-dimensional code generation core class:
PackageCom.bbkj.wechat.tool;ImportJava.awt.image.BufferedImage;ImportJava.io.File;Importjava.io.IOException;ImportJava.io.OutputStream;ImportJava.util.EnumMap;ImportJavax.imageio.ImageIO;ImportCom.google.zxing.BarcodeFormat;ImportCom.google.zxing.EncodeHintType;ImportCom.google.zxing.MultiFormatWriter;ImportCom.google.zxing.common.BitMatrix;ImportCom.google.zxing.qrcode.decoder.ErrorCorrectionLevel;ImportOrg.apache.log4j.Logger;Importorg.apache.log4j.spi.LoggerFactory;/*** realize QR code (add logo image) via Google zxing * Created by Administrator on 2017/2/22.*/ Public Final classencodeimgzxing { Public StaticLogger logger= Logger.getlogger (encodeimgzxing.class); //Two-dimensional code color Private Static Final intBLACK = 0xff000000;//0xffff0000, Red//Two-dimensional code background color Private Static Final intWhite = 0xFFFFFFFF;//0xff0000ff, Blue//Note: Two-dimensional code color chromatic aberration, scan fast, but if "black" is set to the other colors outside the color, may not scan//Two-dimensional code image width Private Static Final intwidth = 300; //Two-dimensional code image height Private Static Final intHeight = 300; //Two-dimensional code format parameters Private Static FinalEnummap<encodehinttype, object> hints =NewEnummap<encodehinttype, Object> (Encodehinttype.class); Static{ /*Two-dimensional code error correction level (error rate), 4 levels: L (7%), M (15%), Q (25%), H (30%) (max h) Error correction information is also stored in the QR code, error correction level The higher the error-correcting information takes up more space, the less useful messages can be stored; a total of four levels; Select m, the scanning speed is fast. */hints.put (encodehinttype.error_correction, ErrorCorrectionLevel.H); //Two-dimensional code boundary blank size 1,2,3,4 (4 default, max)Hints.put (Encodehinttype.margin, 1); Hints.put (Encodehinttype.character_set,"UTF-8"); Hints.put (Encodehinttype.max_size,350); Hints.put (Encodehinttype.min_size,150); } /*** Map QR code *@paramcontents Two-dimensional code content *@returnImage Two-dimensional code picture **/ Public Staticbufferedimage encodeimg (String contents) {BufferedImage image=NULL; Try{Bitmatrix Matrix=NewMultiformatwriter (). Encode (contents, barcodeformat.qr_code, width, height, hints); Image=Newbufferedimage (width, height, bufferedimage.type_int_rgb); intwidth =matrix.getwidth (); intHeight =matrix.getheight (); for(intx = 0; x < width; X + +){ for(intY =0;y < height; y++) {Image.setrgb (x, Y, Matrix.get (x, y)?black:white); } } }Catch(Exception e) {logger.error (E.getmessage ()); //System.out.println ("Generate QR code failed" +e.getmessage ()); } returnimage; } /*** QR code output to file *@paramcontents Two-dimensional code content *@paramFormat Picture formats *@paramfile Output Files **/ Public Static voidwritetofile (String contents,string format,file File) {bufferedimage image=encodeimg (contents); Try{imageio.write (image, format, file); } Catch(IOException e) {logger.error (E.getmessage ()); //System.out.println ("QR Code write file failed" +e.getmessage ()); } } /*** Two-dimensional code streaming output *@paramcontents Two-dimensional code content *@paramFormat Picture formats *@paramStream Output stream **/ Public Static voidWriteToStream (String contents,string format,outputstream stream) {bufferedimage image=encodeimg (contents); Try{imageio.write (image, format, stream); } Catch(IOException e) {logger.error (E.getmessage ()); //System.out.println ("Two-dimensional code write stream failed" +e.getmessage ()); } }}
Examples of Use:
Generate two-dimensional code files:
@Test Public void Testwritetofile () { = "Http://blog.csdn.net/typa01_kk"; // * * * here if the format is "GIF", then the logo image is black, other formats OK String Rooturl = systemutil.getprojectclassespath (); // generate two-dimensional code New File (rooturl+file.separator+ "logo.png"); New File (rooturl+file.separator+ "csdn.jpg"); Encodeimgzxing.writetofile (contents, Format, IMG);}
Java QR code generation