Java generated QR code there are many ways, I now use the Google zxing way
1, join the zxing package, I use the MAVEN build project
<Dependency> <groupId>Com.google.zxing</groupId> <Artifactid>Core</Artifactid> <version>3.2.0</version></Dependency>
2, the use of zxing development also requires a class, the code is as follows
Public Final classMatrixtoimagewriter {Private Static Final intBLACK = 0xff000000; Private Static Final intWhite = 0xFFFFFFFF; 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); } } }
3. Test class
Public Static voidMain (string[] args)throwswriterexception, IOException {String text= "120605181003;http://www.baidu.com"; intwidth = 300; intHeight = 300; //picture format of QR codeString format = "gif"; Hashtable hints=NewHashtable (); //the encoding used for the contentHints.put (Encodehinttype.character_set, "Utf-8"); Bitmatrix Bitmatrix=NewMultiformatwriter (). Encode (text, barcodeformat.qr_code, width, height, hints); //generate two-dimensional codeFile OutputFile =NewFile ("D:" +file.separator+ "New.gif"); Matrixtoimagewriter.writetofile (Bitmatrix, format, outputFile); }
Java generates two-dimensional code