This example needs to use Google's Open source project Zxing's core jar package
Core-3.2.0.jar
Can baidu search download jar file
You can also use Maven to add dependencies
<Dependency> <groupId>Com.google.zxing</groupId> <Artifactid>Core</Artifactid> <version>3.2.0</version> </Dependency>
The simplest way to generate two-dimensional code,
/*** Generate two-dimensional code images *@paramdir-Stored directory *@paramFileName to end with. jpg *@paramcontent This can be text or links*/ Public voidGenerateqrcode (String dir, String fileName, string content) {//generating the width and height of two-dimensional code intSize = 200; Map<encodehinttype, object> hints =NewHashtable<encodehinttype, object>(); //Specify the level of error correctionhints.put (encodehinttype.error_correction, ERRORCORRECTIONLEVEL.L); //Specify encoding formatHints.put (Encodehinttype.character_set, "UTF-8"); Try { //Encode (String contents, barcodeformat format, int width, int height, map<encodehinttype,?> hints)Bitmatrix Bitmatrix =NewMultiformatwriter (). Encode (content, barcodeformat.qr_code, size, size, hints); Bitmatrix= Updatebit (Bitmatrix, 8); File file1=NewFile (dir); if(!file1.exists ()) {File1.mkdirs (); } //writes the resulting matrix pixel to the specified file, which ends with a JPGMatrixtoimagewriter.writetostream (Bitmatrix, "JPG",NewFileOutputStream (dir + "/" +fileName)); System.out.println ("Created successfully"); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } Catch(writerexception e) {e.printstacktrace (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } }
The resulting two-dimensional code left a little white margin, want to properly reduce the margin, see below
If you do not want the margin is too large, we can get the two-dimensional code is really wide, and then create a new empty Bitmatrix object to put this QR code can be
Margin is the size of the white margin
Private StaticBitmatrix updatebit (Bitmatrix matrix,intmargin) { intTEMPM = margin * 2; //Left,top,width,height//0 1 2 3 the corresponding array subscript//the width and height here refer to the real QR code length and width after the white border is removed, not the length of the picture. int[] rec = Matrix.getenclosingrectangle ();//get the properties of a QR code pattern intReswidth = rec[2] + tempm;//true width plus left and right margins intResheight = rec[3] +tempm; Bitmatrix Resmatrix=NewBitmatrix (Reswidth, resheight);//Create a new Bitmatrix according to a custom borderresmatrix.clear (); for(inti = margin; i < Reswidth-margin; i++) {//loops, drawing the QR code pattern into the new Bitmatrix for(intj = margin; J < Resheight-margin; J + +) { if(Matrix.get (I-margin + rec[0], J-margin + rec[1]) {Resmatrix.set (i, j); } } } returnResmatrix; }
Generate two-dimensional code
So the white margin will not be too big, more beautiful
There is also a way to embed the QR code into a poster or other active image, directly on the code
Public voidInsertqrcode (bufferedimage zxingimage, String backgroundpath) {InputStream dest=NULL; Try{dest=NewFileInputStream (Backgroundpath); BufferedImage Image=Imageio.read (dest); Graphics g=Image.getgraphics (); intLeftMargin = Image.getwidth ()-Zxingimage.getwidth ()-10; intTopMargin = Image.getheight ()-Zxingimage.getheight ()-10; G.drawimage (Zxingimage, LeftMargin, TopMargin, Zxingimage.getwidth (), Zxingimage.getheight (),NULL); Imageio.write (Image,"JPG",NewFileOutputStream ("D:\\qrcode\\zengmei.jpg")); System.out.println ("Created successfully"); } Catch(IOException e) {e.printstacktrace (); } }
There are two-dimensional code to modify the line color, in the QR code to insert logo icon, and so on, next time to continue to add.
Thank you for browsing
Reference Links:
Https://www.cnblogs.com/qwqwQAQ/p/8118109.html
Java implementation of Zxing-based two-dimensional code generation and image synthesis
79744670
Java generates two-dimensional code via zxing
http://blog.51cto.com/9732420/1742136
Open Source Project Address
Https://github.com/zxing/zxing
Java Learning-zxing A simple example of generating a two-dimensional code matrix