Java generates a QR code based on qrcode

Source: Internet
Author: User
Package test; import Java. io. *; import Java. util. date; import Java. AWT. *; import Java. AWT. image. *; import javax. imageIO. *; import COM. swetake. util. qrcode; public class qrcodeencodertest {public qrcodeencodertest () {} public static void create_image (string sms_info) throws exception {try {qrcode testqrcode = new qrcode (); testqrcode. setqrcodeerrorcorrect ('M'); testqrcode. setqrcodeencodemode ('B'); testqrcode. se Tqrcodeversion (20); string teststring = sms_info; byte [] d = teststring. getbytes ("GBK"); system. out. println (D. length); bufferedimage Bi = new bufferedimage (98, 98, bufferedimage. type_byte_binary); graphics2d G = Bi. creategraphics (); G. setbackground (color. white); G. clearrect (0, 0, 98, 98); G. setcolor (color. black); // The maximum number of bytes is 120if (D. length> 0 & D. length <300) {Boolean [] [] S = testqrcode. calqrcode (d); (INT I = 0; I <S. length; I ++) {for (Int J = 0; j <S. length; j ++) {If (s [J] [I]) {G. fillrect (J * 2 + 3, I * 2 + 3, 2, 2) ;}}} G. dispose (); bi. flush (); file F = new file ("D: \" + sms_info + ". jpg "); If (! F. exists () {f. createnewfile ();} // Create Image ImageIO. write (Bi, "jpg", f);} // end trycatch (exception e) {e. printstacktrace ();} // end catch} public static void main (string [] ARGs) throws exception {system. out. println (new date (); qrcodeencodertest. create_image ("qrcode"); system. out. println (new date ());}}
 

Jar package:

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.