Download the jar Package
http://mvnrepository.com/artifact/com.google.zxing/core/2.2
Code:
Package Com.haier.util.qr;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;public class Matrixtoimagewriter {private static final int BLACK = 0xff000000;private static final int white = 0xffffffff;private Matri Xtoimagewriter () {}public static bufferedimage 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 = Tobuff Eredimage (matrix); if (! Imageio.write (image, format, file)) {throw new IOException ("Could not write a image of format" + format + "to" + file); }}public static void WRItetostream (Bitmatrix matrix, String Format,outputstream stream) throws IOException {BufferedImage image = Tobufferedimage (matrix); if (! Imageio.write (image, format, stream)) {throw new IOException ("Could not write an image of format" + format);}}
Package Com.haier.util.qr;import Java.io.file;import Java.io.outputstream;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;import Com.test.matrixtoimagewriter;public class QRUtil {public static void Create (String text, OutputStream stream, Integer size) throws Exception {int width = + (ten * size); int height = 100 + ( size);//The picture format of the two-dimensional code string formatted = "JPG"; Hashtable hints = new Hashtable ();//The encoding used for the content hints.put (Encodehinttype.character_set, "utf-8"); Bitmatrix Bitmatrix = new Multiformatwriter (). Encode (Text,barcodeformat.qr_code, width, height, hints);//1.1 Go to white edge int[] rec = Bitmatrix.getenclosingrectangle (); int reswidth = rec[2] + 1;int resheight = rec[3] + 1; Bitmatrix Resmatrix = new Bitmatrix (reswidth, resheight); Resmatrix.clear (); for (int i = 0; i < reswidth; i++) {for (int j = 0; J < Resheight; J + +) {if (Bitmatrix.get (i + rec[0], j + rec[1]) {Resmatrix.set (i, j);}}} Matrixtoimagewriter.writetostream (Resmatrix, format, stream);} public static void Main (string[] args) throws Exception {String text = "http://www.baidu.com/action/page/formSub.html? mac=1111111111111111111111111111111111111111111111111111111 "; int width = 300;int height = 300;//Two-dimensional code picture format string = "JPG"; Hashtable hints = new Hashtable ();//The encoding used for the content hints.put (Encodehinttype.character_set, "utf-8"); Bitmatrix Bitmatrix = new Multiformatwriter (). Encode (Text,barcodeformat.qr_code, width, height, hints);//Generate two-dimensional code file OutputFile = new File ("D:" + File.separator + "new.jpg"); Matrixtoimagewriter.writetofile (Bitmatrix, format, outputFile);}}
QR code--google QR code use (Java version)