Required jar package: qrcode. Jar
:
The following is an encapsulated helper class that provides only the following APIs:
(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 exception {// 1. create qrcodeqrcode qrcode = new qrcode (); qrcode. setqrcodeerrorcorrect ('M'); qrcode. setqrcodeencodemode ('B'); qrcode. setqrcodeversion (7); // 2. number encoded in UTF-8 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. construct a QR code image 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;} public static void encode (string STR, file F) throws exception {// 1. create qrcodeqrcode qrcode = new qrcode (); qrcode. setqrcodeerrorcorrect ('M'); qrcode. setqrcodeencodemode ('B'); qrcode. setqrcodeversion (7); // 2. number encoded in UTF-8 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. construct a QR code image if (D. length> 0 & D. length <123) {Boolean [] [] B = qrcode. calqrcode (d); // calculate the two-digit 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); // fill in the image points based on the Boolean array }}} G. dispose (); image. flush (); ImageIO. write (image, "PNG", f);} public static void encode (string STR, outputstream out) throws exception {// 1. create qrcodeqrcode qrcode = new qrcode (); qrcode. setqrcodeerrorcorrect ('M'); qrcode. setqrcodeencodemode ('B'); qrcode. setqrcodeversion (7); // 2. number encoded in UTF-8 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. construct a QR code image 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); // obtain bufferedimagestring decodeddata = new string (decoder. decode (New myimage (image), // decode and generate the 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 File("1.png"));String str = QRCodeUtils.decode(new File("1.png"));System.out.println(str);}}
References:
(1) http://blog.csdn.net/wangpeng047/article/details/7181217
(2) http://zjgzpc.iteye.com/blog/1478106