1. using Swetakeqrcode to generate a QR code in a Java project
http://swetake.com/qr/
or Http://sourceforge.jp/projects/qrcode/downloads/28391/qrcode.zip .
This was written by the Japanese and generated by our common square QR code .
Can be in Chinese
such as: 5677777GHJJJJJ
Have a friend asked me to this image generated code, I search online and then organized a class, the first to put Swetakeqrcode jar package Qrcode.jar in the project's compilation path, this package is given above the Swetakeqrcode's official website/http Www.swetake.com/qrcode/java/qr_java.html, here is a test class
ImportCom.swetake.util.Qrcode;ImportJavax.imageio.ImageIO;Importjava.awt.*;ImportJava.awt.image.BufferedImage;ImportJava.io.File;Importjava.io.IOException;Importjava.io.UnsupportedEncodingException;/*** Created with IntelliJ idea. * DATE:10/9/13 * time:11:31 AM*/ Public classQrcodetest {Private Static intDefault_width; Private Static intUnit_width = 10; Public Static voidMain (String args[])throwsexception{createimg (); } Public Static voidcreateimg () {QRCode QRCode=NewQRCode (); //Error Correction capacity//l Loadline of level 7% can be modified//Loadline of M level 15% can be corrected//Q-level 25% loadline can be corrected//h-level 30% Loadline can be modified//QR code is fault-tolerant, QR code graphics if damaged, can still be read by the machine, up to 7%~30% area damage can still be read. //relatively, the higher the fault tolerance rate, the larger the QR Code graphics area. So the general tradeoff is to use 15% fault-tolerant capabilities. Qrcode.setqrcodeerrorcorrect (' M ');/*L ', ' M ', ' Q ', ' H '*/Qrcode.setqrcodeencodemode (' B ');/*"N", "A" or other*/Qrcode.setqrcodeversion (3);/*0-20*/String teststring= "5677777GHJJJJJ"; byte[] Buff =NULL; Try{Buff= Teststring.getbytes ("Utf-8"); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } Boolean[] Brect =Qrcode.calqrcode (Buff); Default_width= Brect.length *Unit_width; BufferedImage Bi=Newbufferedimage (Default_width, Default_width, Bufferedimage.type_int_rgb);//int unitwidth = default_width/brect.length;//CreateGraphicsGraphics2D g =bi.creategraphics ();//Set BackgroundG.setbackground (Color.White); G.clearrect (0, 0, Default_width, default_width); G.setcolor (Color.Black); if(Buff.length>0 && buff.length <123){ for(inti=0;i<brect.length;i++){ for(intj=0;j<brect.length;j++){ if(Brect[j][i]) {G.fillrect (J*unit_width, I*unit_width, Unit_width-1, unit_width-1); }}}} g.dispose (); Bi.flush (); String FilePath= "Qrcode.png"; File F=NewFile (FilePath); Try{imageio.write (bi,"PNG", F); } Catch(IOException e) {e.printstacktrace (); } System.out.println ("Create QRCode finished!"); }}
For the code to explain, the others do not say, the main point is how to make qrcode full picture, I here is the first definition of unit_width=10 or other values, and then calculate the screen should be how big, after the painting will be filled with the whole picture, but, so good-looking is good-looking, We can control the size of the screen, so you can also define the default_width is the length of the screen, and then calculate the unit_width, so that the size of the screen is fixed, but this calculated unit Default_width/ Brect.length not necessarily the whole division, so the picture will not be very good, these are not big problems, tune a tune can be. Brect.length this size is determined by the value of Qrcode.setqrcodeversion (3), which is not determined by the length of the string.
And the following zxing can also generate such qrcode.
2. use barcode4j to generate barcodes and QR codes
BARCODE4J website: http://sourceforge.net/projects/barcode4j/
BARCODE4J is a two-dimensional code generation algorithm that uses Datamatrix to support the QR algorithm
Datamatrix is the European and American Standard, the QR is the Japanese standard,
Barcode4j are generally generated as rectangles.
such as: 88777alec000yan
This blog is very clear about this aspect:
http://baijinshan.iteye.com/blog/1004554
3.zxing
Zxing This is Google's ,
Http://code.google.com/p/zxing/downloads/list
ImportJava.io.File;Importjava.util.Hashtable;ImportCom.google.zxing.BarcodeFormat;ImportCom.google.zxing.EncodeHintType;ImportCom.google.zxing.MultiFormatWriter;ImportCom.google.zxing.client.j2se.MatrixToImageWriter;ImportCom.google.zxing.common.BitMatrix;ImportCom.google.zxing.qrcode.QRCodeWriter; Public classqrcodeevents { Public Static voidMain (String []args)throwsexception{String Text= "Hello"; intwidth = 100; intHeight = 100; String format= "png"; Hashtable hints=NewHashtable (); Hints.put (Encodehinttype.character_set,"Utf-8"); Bitmatrix Bitmatrix=NewMultiformatwriter (). Encode (text, Barcodeformat.qr_code, width, height,hints); File OutputFile=NewFile ("New.png"); Matrixtoimagewriter.writetofile (Bitmatrix, format, outputFile); }}
Java three tools to generate a QR code