ImportJava.awt.image.BufferedImage;ImportJava.io.File;Importjava.io.IOException;ImportJava.io.OutputStream;ImportJava.util.HashMap;ImportJava.util.Map;ImportJavax.imageio.ImageIO;ImportCom.google.zxing.BarcodeFormat;ImportCom.google.zxing.EncodeHintType;ImportCom.google.zxing.MultiFormatWriter;ImportCom.google.zxing.common.BitMatrix; Public classCodeutil { Public Staticstring createqrcode (string url, string path, String fileName) {Try{Map<encodehinttype, string> hints =NewHashmap<>(); Hints.put (Encodehinttype.character_set,"UTF-8"); Bitmatrix Bitmatrix=NewMultiformatwriter (). Encode (URL, Barcodeformat.qr_code, 400, 400, hints); File File=NewFile (path, fileName); if(File.exists () | | ((File.getparentfile (). Exists () | | file.getparentfile (). Mkdirs ()) &&file.createnewfile ())) {WriteToFile (Bitmatrix,"JPG", file); System.out.println ("Done:" +file); returnfile.tostring (); } } Catch(Exception e) {e.printstacktrace (); } return NULL; } 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); } } 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); } } Private Static Final intBLACK = 0xff000000; Private Static Final intWhite = 0xFFFFFFFF; Private 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 voidMain (string[] args) {Createqrcode ("Www.baidu.com", "d:\\", "code.jpg"); }}
Pom File Dependency
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.1.0</version>
</dependency>
Tool class for converting URL link to QR code