Java creates QR code with a logo image in the middle. You can set the logo size.

Source: Internet
Author: User

Public static int createQRCode (String content, String imgPath, String logo ){

Try {

Qrcode qrcodeHandler = new Qrcode ();

QrcodeHandler. setQrcodeErrorCorrect ('M ');

QrcodeHandler. setQrcodeEncodeMode ('B ');

QrcodeHandler. setQrcodeVersion (7 );

// System. out. println (content );

Byte [] contentBytes = content. getBytes ("gb2312 ");

// Construct a BufferedImage object to set the width and height

BufferedImage bufImg = new BufferedImage (140,140, BufferedImage. TYPE_INT_RGB );

Graphics2D gs = bufImg. createGraphics ();

Gs. setBackground (Color. WHITE );

Gs. clearRect (0, 0,140,140 );

// Set the image color> BLACK

Gs. setColor (Color. BLACK );

// Parsing error may occur if the offset is not set.

Int pixoff = 2;

// Output content> QR code

If (contentBytes. length> 0 & contentBytes. length <120 ){

Boolean [] [] codeOut = qrcodeHandler. calQrcode (contentBytes );

For (int I = 0; I <codeOut. length; I ++ ){

For (int j = 0; j <codeOut. length; j ++ ){

If (codeOut [j] [I]) {

Gs. fillRect (j * 3 + pixoff, I * 3 + pixoff, 3, 3 );

}

}

}

} Else {

System. err. println ("QRCode content bytes length =" + contentBytes. length + "not in [0,120].");

Return-1;

}

Image img = ImageIO. read (new File (logo); // instantiate an Image object.

Gs. drawImage (img, 44, 55, 49, 30, null );

Gs. dispose ();

BufImg. flush ();

// Generate a QR code QRCode Image

File imgFile = new File (imgPath );

ImageIO. write (bufImg, "png", imgFile );

} Catch (Exception e ){

E. printStackTrace ();

Return-100;

}

Return 0;

}

// Add the logo of the tire angel

CreateQRCode ("http://www.tireangel.com/android/TireAngelDemo.apk", "http://www.tireangel.com/images/qrCode.jpg", "http://www.tireangel.com/images/logo.jpg ");

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.