Coding and decoding of QR code QRCode using Java

Source: Internet
Author: User

 

I tried the QR code scanning software on my Android mobile phone over the weekend and scanned my train tickets, business cards, and so on. It was very nice and interesting. Of course, Java can also implement this. Now I will share how to simply use Java to implement the encoding and decoding of QRCode In the QR code (which can be verified by mobile phone scanning ).

 

Some of the main class libraries involved for you to download:

Code lib: Qrcode_swetake.jar (Official Website -- http://www.swetake.com/qr/index-e.html)

Decoding lib: qrcode. jar (Official Website -- http://sourceforge.jp/projects/qrcode)

 

[1]. encoding:

QRCodeEncoderHandler. java

Java code

Package michael. qrcode;

 

Import java. awt. Color;

Import java. awt. Graphics2D;

Import java. awt. image. BufferedImage;

Import java. io. File;

 

Import javax. imageio. ImageIO;

 

Import com. swetake. util. Qrcode;

 

/**

* QR code generator

* @ Blog http://sjsky.iteye.com

* @ Author Michael

*/

Public class QRCodeEncoderHandler {

 

/**

* Generate a QR code Image

* @ Param content

* @ Param imgPath

*/

Public void encoderQRCode (String content, String imgPath ){

Try {

 

Qrcode qrcodeHandler = new Qrcode ();

QrcodeHandler. setQrcodeErrorCorrect ('M ');

QrcodeHandler. setQrcodeEncodeMode ('B ');

QrcodeHandler. setQrcodeVersion (7 );

 

System. out. println (content );

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

 

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].");

}

 

Gs. dispose ();

BufImg. flush ();

 

File imgFile = new File (imgPath );

 

// Generate a QR code QRCode Image

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

 

} Catch (Exception e ){

E. printStackTrace ();

}

 

}

 

/**

* @ Param args the command line arguments

*/

Public static void main (String [] args ){

String imgPath = "D:/test/twocode/Michael_QRCode.png ";

 

String content = "Hello, big, small, welcome to QRCode! "

+ "\ NMyblog [http://sjsky.iteye.com]"

+ "\ NEMail [sjsky007@gmail.com]" + "\ nTwitter [@ suncto]";

 

QRCodeEncoderHandler handler = new QRCodeEncoderHandler ();

Handler. encoderQRCode (content, imgPath );

 

System. out. println ("encoder QRcode success ");

}

}

 

The QR code generated after running is shown as follows:

 

 

In this case, you can use the QR code scanning software of your mobile phone (I used the android quick Bi QR code) to test the recognition success as follows:

 

If you like it, you can download it and try it and make some business cards or things you like. Of course, Java can also decode the QR code image. For details, refer to the following content about decoding.

 

 

[2]. decoding:

QRCodeDecoderHandler. java

Java code

Package michael. 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;

Import jp. sourceforge. qrcode. exception. DecodingFailedException;

 

/**

* @ Blog http://sjsky.iteye.com

* @ Author Michael

*/

Public class QRCodeDecoderHandler {

 

/**

* Decode the QR code

* @ Param imgPath

* @ Return String

*/

Public String decoderQRCode (String imgPath ){

 

// QRCode QR code Image File

File imageFile = new File (imgPath );

 

BufferedImage bufImg = null;

String decodedData = null;

Try {

BufImg = ImageIO. read (imageFile );

 

QRCodeDecoder decoder = new QRCodeDecoder ();

DecodedData = new String (decoder. decode (new J2SEImage (bufImg )));

 

// Try {

// System. out. println (new String (decodedData. getBytes ("gb2312 "),

// "Gb2312 "));

//} Catch (Exception e ){

//// TODO: handle exception

//}

} Catch (IOException e ){

System. out. println ("Error:" + e. getMessage ());

E. printStackTrace ();

} Catch (DecodingFailedException dfe ){

System. out. println ("Error:" + dfe. getMessage ());

Dfe. printStackTrace ();

}

Return decodedData;

}

 

/**

* @ Param args the command line arguments

*/

Public static void main (String [] args ){

QRCodeDecoderHandler handler = new QRCodeDecoderHandler ();

String imgPath = "d:/test/twocode/Michael_QRCode.png ";

String decoderContent = handler. decoderQRCode (imgPath );

System. out. println ("Resolution Result :");

System. out. println (decoderContent );

System. out. println ("======== decoder success !!! ");

}

 

Class J2SEImage implements QRCodeImage {

BufferedImage bufImg;

 

Public J2SEImage (BufferedImage bufImg ){

This. bufImg = bufImg;

}

 

Public int getWidth (){

Return bufImg. getWidth ();

}

 

Public int getHeight (){

Return bufImg. getHeight ();

}

 

Public int getPixel (int x, int y ){

Return bufImg. getRGB (x, y );

}

 

}

}

 

The running result is as follows (the decoded content is the same as the previously entered content ):

The resolution result is as follows:

Hello, big, small, welcome to QRCode!

Myblog [http://sjsky.iteye.com]

EMail [sjsky007@gmail.com]

Twitter [@ suncto]

========= Decoder success !!!

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.