Use Java to generate a QR code image and parse the QR code image.

Source: Internet
Author: User

Http://www.swetake.com/qr/
Download qrcode. jar to generate a QR code, but it cannot be decoded.

Go to the http://www.swetake.com/qr/java/qr_java.html, download qrcode_java0.50beta10.tar.gz, decompress the package in the lib directory qrcode. Jar
That is, the library used to generate the QR code.

Http://sourceforge.jp/projects/qrcode/
Download qrcode. jar to parse the QR code.

Go to http://sourceforge.jp/projects/qrcode/releases/, download qrcode.zip, decompress it, and
The qrcode. jar in the lib directory is the library used to parse the QR code.

Modify the qrcode. Jar name and add it to the Lib of the eclipse project.

Sample Code for generating a QR code:

Package qrcode; import Java. AWT. color; import Java. AWT. graphics2d; import Java. AWT. image. bufferedimage; import Java. io. file; import Java. io. ioexception; import javax. imageIO. imageIO; import COM. swetake. util. qrcode; public class gucasqrcodeencoder {public static int max_data_size_small = 84; public static int Limit = 500;/***** @ Param srcvalue * @ Param qrcodepicfilepath * @ return */Public stat IC Boolean encode (string srcvalue, string qrcodepicfilepath) {// return encode_500 (srcvalue, qrcodepicfilepath); Return encode_84 (srcvalue, qrcodepicfilepath );} /*** encoding the information to a qrcode, size of the information must be less than 84 byte. * @ Param srcvalue * @ Param qrcodepicfilepath * @ return */public static Boolean encode_84 (string srcvalue, string qrcodepicfilepath) {int max_data_l Ength = max_data_size_small; // restrict the maximum data size of the generated QR code. byte [] d = srcvalue. getbytes (); int datalength = D. length; int imagewidth = 113;/* 113 is calculated in advance. note: The image width must be greater than or equal to the width of the generated QR code image. Otherwise, the QR Code cannot be identified */INT imageheight = imagewidth; bufferedimage Bi = new bufferedimage (imagewidth, imageheight, bufferedimage. type_int_rgb); graphics2d G = Bi. creategraphics (); G. setbackground (color. white); G. clearrect (0, 0, imagewidth, imageh Eight); G. setcolor (color. black); If (datalength> 0 & datalength <= max_data_length) {/* generate QR code */qrcode = new qrcode (); qrcode. setqrcodeerrorcorrect ('M'); // L, Q, H, default qrcode. setqrcodeencodemode ('B'); // A, N, qrcode by default. setqrcodeversion (5); // 37 bytes, (37-1) * 3 + 2 + 3-1 + 1 = 113 Boolean [] [] B = qrcode. calqrcode (d); int qrcodedatalen = B. length; For (INT I = 0; I <qrcodedatalen; I ++) {for (Int J = 0; j <Qrcodedatalen; j ++) {If (B [J] [I]) {G. fillrect (J * 3 + 2, I * 3 + 2, 3, 3);/** draw a QR code graph, * draw a graph width is * (qrcodeDataLen-1) * 3 + 2) + * 3-1; * The width of the generated image must be greater than or equal to this value, the peripheral 1 pixel is used to identify this area as the QR code * // ** fillrect (int x, int y, int width, int height) function: * fill the specified rectangle. The left and right sides of the rectangle are located in X and x + width-1. The top side and the bottom side are in Y and Y + * Height-1. The width and height of the area covered by the rectangle are pixels in width and height. * Fill the rectangle with the current color of the graphic context. Parameter: X-x coordinate of the rectangle to be filled. Y-y * coordinates of the rectangle to be filled. Width-the width of the rectangle to be filled. Height-the height of the rectangle to be filled. ** Reference: http://bk.chinaar.com/index.php? Doc-view-2999 */}}} system. Out. println ("QR code Data Length (bytes):" + qrcodedatalen);} else {system. Out. println ("generate qrcode image error! Data size is "+ datalength +", it is lager than 84 bytes. "); Return false;} G. dispose (); bi. flush ();/* generate image */file F = new file (qrcodepicfilepath); string suffix = f. getname (). substring (F. getname (). indexof (". ") + 1, F. getname (). length (); try {ImageIO. write (Bi, suffix, f); // "PNG"} catch (ioexception IOE) {system. out. println ("generate qrcode image error! "+ IOE. getmessage (); Return false;} return true;}/*** encoding the information to a qrcode, size of the information must be less tah 500 byte. * @ Param srcvalue * @ Param qrcodepicfilepath * @ return */public static Boolean encode_500 (string srcvalue, string qrcodepicfilepath) {int max_data_length = bytes; // restrict the maximum data size of the generated QR code. 500 bytes of raw data, which is 89 bytes [] d = srcvalue when a QR code is generated. getbytes (); int d Atalength = D. length; int imagewidth = 269;/* 269 is calculated in advance. note: The image width must be greater than or equal to the width of the generated QR code image. Otherwise, the QR Code cannot be identified */INT imageheight = imagewidth; bufferedimage Bi = new bufferedimage (imagewidth, imageheight, bufferedimage. type_int_rgb); graphics2d G = Bi. creategraphics (); G. setbackground (color. white); G. clearrect (0, 0, imagewidth, imageheight); G. setcolor (color. black); If (datalength> 0 & datalength <= max_data_length) {/* Generate a QR code */qrcode = new qrcode (); qrcode. setqrcodeerrorcorrect ('M'); // L, Q, H, default qrcode. setqrcodeencodemode ('B'); // A, N, qrcode by default. setqrcodeversion (18); // 0 <= version <= 40; 89 bytes, // (89-1) * 3 + 2 + 3-1 + 1 = 269 Boolean [] [] B = qrcode. calqrcode (d); int qrcodedatalen = B. length; For (INT I = 0; I <qrcodedatalen; I ++) {for (Int J = 0; j <qrcodedatalen; j ++) {If (B [J] [I]) {G. fillrect (J * 3 + 2, I * 3 + 2, 3, 3);/** draw a QR code image, * the width of the drawing is * (qrcodeDataLen-1) * 3 + 2) + * 3-1 = 136; * The width of the generated image must be greater than or equal to (136 + 1 ), the peripheral 1 pixel is used to identify this area as the QR code * // ** fillrect (int x, int y, int width, int height) function: * fill the specified rectangle. The left and right sides of the rectangle are located in X and x + width-1. The top side and the bottom side are in Y and Y + * Height-1. The width and height of the area covered by the rectangle are pixels in width and height. * Fill the rectangle with the current color of the graphic context. Parameter: X-x coordinate of the rectangle to be filled. Y-y * coordinates of the rectangle to be filled. Width-the width of the rectangle to be filled. Height-the height of the rectangle to be filled. ** Reference: http://bk.chinaar.com/index.php? Doc-view-2999 */}}system. out. println ("two-dimensional code Data Length (bytes):" + qrcodedatalen);} else {return false;} G. dispose (); bi. flush ();/* generate image */file F = new file (qrcodepicfilepath); string suffix = f. getname (). substring (F. getname (). indexof (". ") + 1, F. getname (). length (); system. out. println (suffix); try {ImageIO. write (Bi, suffix, f); // "PNG"} catch (ioexception IOE) {system. out. println ("generate qrcode image err Or! "+ IOE. getmessage (); Return false;} return true;} public static void main (string [] ARGs) throws exception {string data = "home cooking Guo Lin's home cooking Guo Lin"; system. out. println ("Bytes:" + data. getbytes (). length); gucasqrcodeencoder. encode (data, ". jpg "); system. out. println (gucasqrcodedecoder. decode (". jpg "));}}


Sample Code for parsing the QR code:

Package qrcode; import Java. AWT. image. bufferedimage; import Java. io. file; import Java. io. ioexception; import javax. imageIO. imageIO; import JP. sourceForge. qrcode. qrcodedecoder; import JP. sourceForge. qrcode. data. qrcodeimage; public class gucasqrcodedecoder {public gucasqrcodedecoder () {}/ *** decode qrcode image. * @ Param qrcodepicfilepath * @ return decoding value. */public static string decode (string qrcodepicfilepath) {/* read QR code image data */file imagefile = new file (qrcodepicfilepath); bufferedimage image = NULL; try {image = ImageIO. read (imagefile);} catch (ioexception e) {system. out. println ("decoding failed, read qrcode image error:" + E. getmessage (); return NULL;}/* try {string decodeddata = new string (decoder. decode (New j2seimagegucas (image), "GBK"); system. out. println (decodeddata);} catch (decodingfailedexception DFE) {system. out. println ("error:" + DFE. getmessage ();} catch (unsupportedencodingexception e) {e. printstacktrace ();} * // * solve the QR code */qrcodedecoder decoder = new qrcodedecoder (); string decodeddata = new string (decoder. decode (New j2seimagegucas (image); Return decodeddata;} public static void main (string [] ARGs) {qrcodedecoder decoder = new qrcodedecoder (); file imagefile = new file ("qrcode // testqrcode.png"); bufferedimage image = NULL; try {image = ImageIO. read (imagefile);} catch (ioexception e) {system. out. println ("error:" + E. getmessage ();} system. out. println ("identify QR code");/* try {string decodeddata = new string (decoder. decode (New j2seimagegucas (image), "GBK"); system. out. println (decodeddata);} catch (decodingfailedexception DFE) {system. out. println ("error:" + DFE. getmessage ();} catch (unsupportedencodingexception e) {e. printstacktrace ();} */string decodeddata = new string (decoder. decode (New j2seimagegucas (image); system. out. println (decodeddata);} class j2seimagegucas implements qrcodeimage {bufferedimage image; Public j2seimagegucas (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 );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.