Coding, decoding and example parsing of two-dimensional code qrcode in Java _java

Source: Internet
Author: User
Tags getmessage

Coding and decoding of two-dimensional code qrcode in Java

Related to some of the main class library, convenient for everyone to download:

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

Decoding Lib:qrcode.jar (Website introduction-- http://sourceforge.jp/projects/qrcode/)

"One", Code:

Java Code Qrcodeencoderhandler.java

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; /** * Two-dimensional code generator * @blog http://sjsky.iteye.com * @author Michael/public class Qrcodeencoderhandler {/** * generates a two-dimensional code 
 
 (qrcode) Picture * @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 Image Color > Black gs.setcolor (Color.Black); 
 Setting offset without setting may cause parsing error int pixoff = 2; //output > two-dimensional code if (Contentbytes.length > 0 && contentbytes.length <) {boolean[][] codeout = Qrcodeh 
 Andler.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 two-dimensional Code qrcode picture 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 two-dimensional code picture that is generated after the run is as follows:

At this time can be used mobile phone two-dimensional code scanning software (I use: Android quick two-dimensional code) to test, recognition of the success of the screenshot is as follows:

Like friends can be downloaded after a try, do some business cards or their favorite things. Of course, Java can also be a two-dimensional code image decoding, see the following about the decoding content.

"Two", decoding:

Java Code Qrcodedecoderhandler.java

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 {/** * Decoding the two-dimensional code * @param Imgpath * @return String/*/public string Decoderqrcode (string imgpath) {//QRCode two-dimensional code picture files File ImageFile = n 
 
 EW 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 ("The analytic result is as follows:"); 
 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 results of the operation are as follows (the decoded content is the same as what was previously entered):

The results of the analysis are as follows:

Hello big, small, welcome to qrcode!

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

EMail [sjsky007@gmail.com]

Twitter [@suncto]

========decoder Success!!!

The above is the Java implementation of two-dimensional code QRCode coding and decoding of data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

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.