It has to be said that the application of QR code is more and more extensive. Don't say much nonsense.
The jar used here is not many, only use the zxing package inside the Core.jar package can, we download,
If it is Baidu Cloud disk, the address is: Http://pan.baidu.com/s/1eQeWFaq
After a lot of blog resources will be updated to this address, we try to transfer the resources of their own disk, to prevent the link one day suddenly hang.
Instance:
Genqrcode (String content,string URL), where the URL absolute path, is the calling method of this class, return the value two code file name, generate PNG format (if other, self-change), the beginning has already defined the picture size is 300*300
ImportJava.awt.image.BufferedImage;ImportJava.io.File;Importjava.io.IOException;ImportJava.io.OutputStream;ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importjava.util.Hashtable;ImportJavax.imageio.ImageIO;ImportCom.google.zxing.BarcodeFormat;ImportCom.google.zxing.EncodeHintType;ImportCom.google.zxing.MultiFormatWriter;Importcom.google.zxing.WriterException;ImportCom.google.zxing.common.BitMatrix; Public classMatrixtoimagewriter {Private Static Final intBLACK = 0xff000000; Private Static Final intWhite = 0xFFFFFFFF; Private Static Final intWIDTH = 300; Private Static Final intHEIGHT = 300; PrivateMatrixtoimagewriter () {} Public Staticbufferedimage tobufferedimage (Bitmatrix matrix) {intwidth =matrix.getwidth (); intHeight =matrix.getheight (); BufferedImage Image=Newbufferedimage (width, height, bufferedimage.type_int_rgb); for(intx = 0; x < width; X + +) { for(inty = 0; Y < height; y++) {Image.setrgb (x, Y, Matrix.get (x, y)?black:white); } } returnimage; } Public Static voidWriteToFile (Bitmatrix Matrix, String format, file file)throwsIOException {bufferedimage image=tobufferedimage (matrix); if(!imageio.write (image, format, file)) { Throw NewIOException ("Could not write a image of format" + format + "to" +file); } } Public Static voidWriteToStream (Bitmatrix Matrix, String format, OutputStream stream)throwsIOException {bufferedimage image=tobufferedimage (matrix); if(!imageio.write (image, format, stream)) { Throw NewIOException ("Could not write an image of format" +format); } } //generate a QR code picture content: Contents, url: Build Path Public Staticstring Genqrcode (String content,string URL) {//Verify that there are folderscreatedir.createdir (URL); String filename=NULL; //picture format of QR codeString format = "png"; Hashtable hints=NewHashtable (); //the encoding used for the contentHints.put (Encodehinttype.character_set, "Utf-8"); Bitmatrix Bitmatrix; Try{Bitmatrix=NewMultiformatwriter (). Encode (content, Barcodeformat.qr_code, WIDTH, HEIGHT, hints); //generate a name, if you have your own rules, the following 3 lines change themselves intS= (int) (Math.random () *10000);//integer TypeSimpleDateFormat SDF =NewSimpleDateFormat ("Yyyymmddhhmmss"); FileName= "File_" + Sdf.format (NewDate ()) +s + "." +format; //generate two-dimensional codeFile OutputFile =NewFile (url+file.separator+filename); Matrixtoimagewriter.writetofile (Bitmatrix, format, outputFile); } Catch(writerexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } returnfilename; } }
Java QR code generation tool based on Google Development zxing package development