This is the main use of Google's zxing rack package.
<dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId>< Version>3.2.1</version></dependency>
Note: jdk1.7 support is required after version 2.1
Api:
Package crunchify.com.tutorial; Import Java.awt.color;import java.awt.graphics2d;import Java.awt.image.bufferedimage;import Java.io.File;import Java.io.ioexception;import Java.util.enummap;import Java.util.Map; Import Javax.imageio.ImageIO; Import Com.google.zxing.barcodeformat;import Com.google.zxing.encodehinttype;import Com.google.zxing.writerexception;import Com.google.zxing.common.bitmatrix;import Com.google.zxing.qrcode.qrcodewriter;import Com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; /** * @author crunchify.com * updated:03/20/2016-added code to narrow border size */public class Crunchifyqrcode {// tutorial:http://zxing.github.io/zxing/apidocs/index.html public static void Main (string[] args) {String mycodetext = "h ttp://crunchify.com/"; String FilePath = "/users/appshah/documents/crunchifyqr.png"; int size = 250; String fileType = "png"; File MyFile = new file (FilePath); try {map<encodehinttype, object> hintmap = new Enummap<encodehinttype, OBJECT&G t; (EncodehInttype.class); Hintmap.put (Encodehinttype.character_set, "UTF-8");//Now with Zxing version 3.2.1 your could change Border size (white border size to just 1) hintmap.put (Encodehinttype.margin, 1); /* default = 4 */hintmap.put (encodehinttype.error_correction, ERRORCORRECTIONLEVEL.L); Qrcodewriter qrcodewriter = new Qrcodewriter (); Bitmatrix Bytematrix = Qrcodewriter.encode (Mycodetext, Barcodeformat.qr_code, Size,size, hintMap); int CrunchifyWidth = Bytematrix.getwidth (); BufferedImage image = New BufferedImage (crunchifywidth, CRUNCHIFYWIDTH,BUFFEREDIMAGE.TYPE_INT_RGB); Image.creategraphics (); Graphics2D graphics = (graphics2d) image.getgraphics () Graphics.setcolor (Color.White); graphics.fillrect (0, 0, Crunchifywidth, Crunchifywidth); Graphics.setcolor (Color.Black); for (int i = 0; i < crunchifywidth; i++) {for (int j = 0; J < Crunchifywidth; J + +) {if (Bytematrix.get (i, J)) {graph Ics.fillrect (I, J, 1, 1);}}} Imageio.write (image, FileType, myFile);} catch (Writerexception e) {E.printstacktraCE ();} catch (IOException e) {e.printstacktrace ();} System.out.println ("\n\nyou has successfully created QR Code.");}
Generated:
Java generates two-dimensional code