Java QR code generation tool Class

Source: Internet
Author: User
Tags set background

 Public classQrcodeutils {/*** Fault tolerance class L*/     Public Static Final Charcorrect_l = ' L '; /*** Fault tolerance class M*/     Public Static Final Charcorrect_m = ' M '; /*** Fault tolerance level Q*/     Public Static Final Charcorrect_q = ' Q '; /*** Fault tolerance class H*/     Public Static Final CharCorrect_h = ' H '; /*** Narration occupancy ratio*/     Public Static Final DoublePadding_ratio = 0.05; /*** Generate QR Code *@paramcontent to generate *@paramsize to generate *@paramversoin QR code size, the value range of 1-40, the larger the size of the larger, the more information can be stored *@paramECC Two-dimensional code error rate, selectable L (7%), M (15%), Q (25%), H (30%) *@return     * @throwsException*/     Public StaticBufferedImage Encode (String content,intSizeintVersionCharECC, BufferedImage logo)throwsexception{bufferedimage Image=NULL; QRCode QRCode=NewQRCode (); //set the two-dimensional code error rate, optional L (7%), M (15%), Q (25%), H (30%), the higher the error rate can be stored less information, but the two-dimensional code clarity requirements of the smallerQrcode.setqrcodeerrorcorrect (ECC); Qrcode.setqrcodeencodemode (B); //set the size of the two-dimensional code, the value range of 1-40, the larger the size of the larger, the more information can be storedqrcode.setqrcodeversion (version); //get a byte array of content, set the encoding format        byte[] contentbytes = Content.getbytes ("Utf-8"); DoubleRatio = 3;//three times times magnification by default        intQrcodesize = + 4 * (version-1); if(Size > 0) {ratio= (Double) Size/qrcodesize; }        intIntratio = (int) Math.ceil (ratio); //Narrator        intpadding = (int) Math.ceil (qrcodesize * intratio *padding_ratio); if(Padding < 10) {padding= 10; }         //Picture Size        intImageSize = qrcodesize * intratio + padding * 2; Image=Newbufferedimage (imageSize, ImageSize, Bufferedimage.type_int_rgb); Graphics2D GS=Image.creategraphics (); //Set Background colorGs.setbackground (Color.White); Gs.clearrect (0, 0, ImageSize, imageSize); //set Image Color BLACKGs.setcolor (Color.Black); //output content QR code        if(Contentbytes.length > 0 && contentbytes.length < 800) {              Boolean[] Codeout =Qrcode.calqrcode (contentbytes);  for(inti = 0; i < codeout.length; i++) {                   for(intj = 0; J < Codeout.length; J + +) {                      if(Codeout[j][i]) {Gs.fillrect (J* Intratio + padding, I * intratio +padding, intratio, intratio); }                  }              }          } Else {              Throw NewException ("qrcode content bytes Length =" + Contentbytes.length + "not in [0, 800]."); }                if(Logo! =NULL){            intLogosize = IMAGESIZE/4; intp = (imagesize-logosize)/2; Logo=Imageutils.setradius (logo); Gs.drawimage (Logo, p, p, logosize, Logosize,NULL);        } gs.dispose ();                Image.flush (); if(Intratio >ratio) {Image= Resize (Image, (int) Math.ceil (ratio * ImageSize/intratio)); }        returnimage; }        /*** Generate QR Code *@paramcontent *@paramSize *@paramversion *@paramECC *@return     * @throwsException*/     Public StaticBufferedImage Encode (String content,intSizeintVersionCharEccthrowsexception{returnEncode (content, size, version, ECC,NULL); }        /*** Generate QR Code *@paramcontent *@paramSize *@return     * @throwsException*/     Public StaticBufferedImage Encode (String content,intSizethrowsexception{returnEncode (content, size, 9, correct_q); }        /*** Generate QR Code *@paramcontent *@paramSize *@paramversion *@return     */     Public StaticBufferedImage Encode (String content,intSizeintVersionthrowsexception{returnencode (content, size, version, correct_q); }        /*** Generate QR Code *@paramcontent *@paramSize *@paramlogo *@return     */     Public StaticBufferedImage Encode (String content,intSize, BufferedImage logo)throwsexception{returnEncode (content, size, 9, correct_q, logo); }        /*** Generate QR Code *@paramcontent *@paramSize *@paramECC *@return     */     Public StaticBufferedImage Encode (String content,intSizeCharEccthrowsexception{returnEncode (content, size, 9, ECC); }        /*** Generate QR Code *@paramcontent *@return     */     Public StaticBufferedImage encode (String content)throwsexception{returnEncode (content, 0, 9, correct_q); }        /*** Parse QR code *@throwsunsupportedencodingexception *@throwsdecodingfailedexception*/     Public StaticString decode (BufferedImage bufferedimage)throwsdecodingfailedexception, unsupportedencodingexception{qrcodedecoder decoder=NewQrcodedecoder (); return NewString (Decoder.decode (NewMyqrcodeimage (BufferedImage)), "Utf-8"); }        /*** Image Zoom*/    Private StaticBufferedImage Resize (bufferedimage image,intsize) {BufferedImage BufferedImage=Newbufferedimage (size, size, BUFFEREDIMAGE.TYPE_INT_RGB); Graphics2D GS=Bufferedimage.creategraphics ();          Gs.setbackground (Color.White); Gs.clearrect (0, 0, size, size);        Gs.setcolor (Color.Black); Gs.fillrect (0, 0, size, size); Gs.drawimage (Image,0, 0, size, size,NULL);          Gs.dispose ();        Image.flush (); returnBufferedImage; }    }

Java QR code generation tool Class

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.