Required JAR Package: Qrcode.jar
Effect Chart:
The following are the auxiliary classes I encapsulate, and the APIs provided are:
(1) Encode (String str);
(2) Encode (String str,file f);
(3) Encode (String str,outputstream out);
(4) Decode (File f);
(5) Decode (InputStream in);
Package Org.xiazdong;
Import Java.awt.Color;
Import Java.awt.Graphics2D;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Javax.imageio.ImageIO;
Import Jp.sourceforge.qrcode.QRCodeDecoder;
Import Jp.sourceforge.qrcode.data.QRCodeImage;
Import Com.swetake.util.Qrcode; Public final class Qrcodeutils {private Qrcodeutils () {} public static bufferedimage encode (String str) throws Excep
tion {//1. Create QRCode qrcode qrcode = new QRCode ();
Qrcode.setqrcodeerrorcorrect (' M ');
Qrcode.setqrcodeencodemode (' B ');
Qrcode.setqrcodeversion (7);
2. Number with UTF-8 encoded byte[] D = str.getbytes ("UTF-8");
BufferedImage image = New BufferedImage (139, 139, BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics2D g = image.creategraphics ();
G.setbackground (Color.White);
G.clearrect (0, 0, 139, 139);
G.setcolor (Color.Black); 3. Constructs the two-dimensional code picture if (D.length > 0 && d.length < 123) {boolean[][] b = Qrcode.calqrcode (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);
}}} g.dispose ();
Image.flush ();
return image;
The public static void encode (String str, File f) throws Exception {//1. Create QRCode qrcode qrcode = new QRCode ();
Qrcode.setqrcodeerrorcorrect (' M ');
Qrcode.setqrcodeencodemode (' B ');
Qrcode.setqrcodeversion (7);
2. Number with UTF-8 encoded byte[] D = str.getbytes ("UTF-8");
BufferedImage image = New BufferedImage (139, 139, BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics2D g = image.creategraphics ();
G.setbackground (Color.White);
G.clearrect (0, 0, 139, 139);
G.setcolor (Color.Black);
3. Constructs the two-dimensional code picture if (D.length > 0 && d.length < 123) {boolean[][] b = Qrcode.calqrcode (d);//Compute two-bit Boolean array 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); Based on a Boolean arrayFull picture of each point}}} g.dispose ();
Image.flush ();
Imageio.write (Image, "PNG", f); public static void Encode (String str, outputstream out) throws Exception {//1. Create QRCode qrcode qrcode = new Qrco
De ();
Qrcode.setqrcodeerrorcorrect (' M ');
Qrcode.setqrcodeencodemode (' B ');
Qrcode.setqrcodeversion (7);
2. Number with UTF-8 encoded byte[] D = str.getbytes ("UTF-8");
BufferedImage image = New BufferedImage (139, 139, BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics2D g = image.creategraphics ();
G.setbackground (Color.White);
G.clearrect (0, 0, 139, 139);
G.setcolor (Color.Black);
3. Constructs the two-dimensional code picture if (D.length > 0 && d.length < 123) {boolean[][] b = Qrcode.calqrcode (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);
}}} g.dispose ();
Image.flush ();
Imageio.write (Image, "PNG", out); public static String decode (File f) throws Exception {Qrcodedecoder decoder = new Qrcodedecoder ();
BufferedImage image = null; Image = Imageio.read (f);
Get bufferedimage string decodeddata = new String (Decoder.decode (image),//decode, and generate string "UTF-8");
return decodeddata;
public static String decode (InputStream in) throws Exception {Qrcodedecoder decoder = new Qrcodedecoder ();
BufferedImage image = null;
Image = Imageio.read (in);
String decodeddata = new String (Decoder.decode (new myimage (image), "UTF-8");
return decodeddata;
} class MyImage implements Qrcodeimage {bufferedimage image;
Public MyImage (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); }
}
Test class:
Package test;
Import Java.io.File;
Import Org.xiazdong.QRCodeUtils;
public class Test {public
static void Main (string[] args) throws Exception {
Qrcodeutils.encode ("Xiazdong", New F Ile ("1.png"));
String str = qrcodeutils.decode (new File ("1.png"));
System.out.println (str);
}
Reference articles:
(1) http://blog.csdn.net/wangpeng047/article/details/7181217
(2) http://zjgzpc.iteye.com/blog/1478106