QRCode, developed by the Japanese, is a zxing developed by Google.
QRCode Development requires package http://download.csdn.net/detail/xiaokui_wingfly/7957815
Zxing Development requires package http://download.csdn.net/detail/u010457960/5301392
QRCode Way:
Package Cn.utils;import Java.awt.color;import Java.awt.graphics2d;import java.awt.image;import Java.awt.image.bufferedimage;import Java.io.file;import Java.io.ioexception;import Java.io.unsupportedencodingexception;import Javax.imageio.imageio;import Jp.sourceforge.qrcode.QRCodeDecoder; Import Jp.sourceforge.qrcode.data.qrcodeimage;import jp.sourceforge.qrcode.exception.DecodingFailedException; Import Com.swetake.util.qrcode;public class Qrcodeutils {/** * encode the string contents into the target file object * * @param encodeddata encoded content * @param de Stfile generate file 1381090722 5029067275903 * @throws ioexception */public static void Qrcodeencode (String encodeddata, Fil E destfile) throws IOException {QRCode QRCode = new QRCode (); Qrcode.setqrcodeerrorcorrect (' M ');//Error correction level (L 7%, M 15%, Q 25%, H 30%) and version about Qrcode.setqrcodeencodemode (' B '); qrcode.setqrcodeversion (7);//Set version of QRCode package byte[] D = encodeddata.getbytes ("GBK");//Character Set BufferedImage bi = new BufferedImage (139, 139, BUFFEREDIMAGE.TYPE_INT_RGB);//creategraphics// Create a layer graphics2dg = Bi.creategraphics (); G.setbackground (color.white);//Set background color (white) g.clearrect (0, 0, 139, 139);//Rectangle X, Y, Width, Heightg.setcolor (Color.Black);//Set Image Color (black) if (d.length > 0 && d.length < 123) {boolean[][] b = QRCODE.CALQR Code (d); for (int i = 0; i < b.length; i++) {for (int j = 0; J < B.length; J + +) {if (B[j][i]) {G.fillrect (J * 3 + 2, I * 3 + 2, 3, 3);}}} Image img = imageio.read (New File ("D:/tt.png")); Logo//g.drawimage (IMG, 55,60,50, NULL); G.dispose (); Releases the context of this graphic and all system resources it uses. After you call Dispose, you can no longer use the Graphics object Bi.flush (); Refreshes all the reconfigurable resources that this Image object is using Imageio.write (BI, "PNG", destfile); SYSTEM.OUT.PRINTLN ("Input encoded data is:" + encodeddata);} /** * Parse QR code, return parsing content * * @param imagefile * @return */public static string Qrcodedecode (File imagefile) {string decodeddata = NULL; Qrcodedecoder decoder = new Qrcodedecoder (); BufferedImage image = null;try {image = Imageio.read (imagefile);} catch (IOException e) {System.out.println ("Error:" + E. GetMessage ());} try {DecodeddaTa = new String (Decoder.decode (new j2seimage (image)), "GBK"); System.out.println ("Output decoded Data is:" + decodeddata);} catch (Decodingfailedexception DfE) {System.out.println ("Error:" + dfe.getmessage ());} catch (Unsupportedencodingexception e) {e.printstacktrace ();} return decodeddata;} public static void Main (string[] args) {String FilePath = "D:/qrcode.png"; File Qrfile = new file (FilePath);//QR code content string encodeddata = "Hello X-rapido"; try {Qrcodeutils.qrcodeencode (encodeddata , qrfile);} catch (IOException e) {e.printstacktrace ();} Decode string retext = Qrcodeutils.qrcodedecode (qrfile); System.out.println (Retext);}} Class J2seimage implements Qrcodeimage {bufferedimage image;public j2seimage (bufferedimage image) {this.image = image;} public int getwidth () {return image.getwidth ();} public int getheight () {return image.getheight ();} public int GetPixel (int x, int y) {return Image.getrgb (x, y);}}
Zxing Way
Package Cn.utils.code;import Java.awt.image.bufferedimage;import Java.io.file;import java.io.ioexception;import Java.io.outputstream;import Javax.imageio.imageio;import com.google.zxing.common.bitmatrix;/** * The generation of two-dimensional code requires the help of the Matrixtoimagewriter class, which is provided by Google and can be copied directly into the source using */public class Matrixtoimagewriter {private static Final int BLACK = 0xff000000;private static final int white = 0xffffffff;private Matrixtoimagewriter () {}public static Buf Feredimage tobufferedimage (Bitmatrix matrix) {int width = matrix.getwidth (); int height = matrix.getheight (); BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb); for (INT x = 0; x < width; + +) {for (int y = 0; y < height; y++) {Image.setrgb (x, Y, Matrix.get (x, y)? black:white);}} return image; public static void WriteToFile (Bitmatrix matrix, String format, file file) throws IOException {bufferedimage image = Tobuf Feredimage (matrix); if (! Imageio.write (image, format, file)) {throw new IOException ("Could not write an Image of format "+ format +" to "+ file);}} public static void WriteToStream (Bitmatrix matrix, String format, OutputStream stream) throws IOException {BufferedImage I Mage = Tobufferedimage (matrix); Imageio.write (image, format, stream)) {throw new IOException ("Could not write an image of format" + format);}}
Writing the Main method
Package Cn.utils.code;import Java.io.file;import Java.util.hashtable;import com.google.zxing.barcodeformat;import Com.google.zxing.encodehinttype;import Com.google.zxing.multiformatwriter;import Com.google.zxing.common.bitmatrix;public class Main {public static void Main (string[] args) throws Exception {String text = "Niuyueyue I love you! "; Two-dimensional code content int width = 300; Two-dimensional code picture width int height = 300; Two-dimensional code picture height String format = "gif";//QR code picture format Hashtable<encodehinttype, string> hints = new hashtable< Encodehinttype, string> (); Hints.put (Encodehinttype.character_set, "utf-8");//The character set encoding used for the content bitmatrix Bitmatrix = new Multiformatwriter (). Encode (text, barcodeformat.qr_code, width, height, hints);//Generate two-dimensional code file OutputFile = new file ("D:" + F Ile.separator + "New.gif"); Matrixtoimagewriter.writetofile (Bitmatrix, format, outputFile);}}
Both ways can be created two-dimensional code and resolution, the general project rarely use their own write QR code, on the site to provide a lot of online QR code generator, which has many features in the Advanced design mode
Recommend a website forage QR code http://cli.im/text
Java generates two-dimensional code using QRCode and zxing two different ways