Ext.: http://blog.csdn.net/ljb_blog/article/details/6700834
"One", Code:
Qrcodeencoderhandler.java
1 PackageMichael.qrcode;2 3 ImportJava.awt.Color;4 ImportJava.awt.Graphics2D;5 ImportJava.awt.image.BufferedImage;6 ImportJava.io.File;7 8 ImportJavax.imageio.ImageIO;9 Ten ImportCom.swetake.util.Qrcode; One A /** - * QR code generator - * @bloghttp://sjsky.iteye.com the * @authorMichael - */ - Public classQrcodeencoderhandler { - + /** - * Generate QR Code (qrcode) Pictures + * @paramcontent A * @paramImgpath at */ - Public voidEncoderqrcode (string content, String imgpath) { - Try { - -QRCode Qrcodehandler =NewQRCode (); -Qrcodehandler.setqrcodeerrorcorrect (' M '); inQrcodehandler.setqrcodeencodemode (' B '); -Qrcodehandler.setqrcodeversion (7); to + System.out.println (content); - byte[] contentbytes = Content.getbytes ("gb2312"); the *BufferedImage bufimg =NewBufferedImage (140, 140, $ Bufferedimage.type_int_rgb);Panax Notoginseng -Graphics2D GS =bufimg.creategraphics (); the + Gs.setbackground (color.white); AGs.clearrect (0, 0, 140, 140); the + //set Image Color > BLACK - Gs.setcolor (color.black); $ $ //setting offset does not set may cause parsing errors - intPixoff = 2; - //output content > QR Code the if(Contentbytes.length > 0 && contentbytes.length < 120) { - Boolean[] Codeout =Qrcodehandler.calqrcode (contentbytes);Wuyi for(inti = 0; i < codeout.length; i++) { the for(intj = 0; J < Codeout.length; J + +) { - if(Codeout[j][i]) { WuGs.fillrect (J * 3 + Pixoff, I * 3 + Pixoff, 3, 3); - } About } $ } -}Else { -System.err.println ("qrcode content bytes Length =" -+ Contentbytes.length + "not in [0,120]."); A } + the gs.dispose (); - Bufimg.flush (); $ theFile Imgfile =NewFile (imgpath); the the //generate two-dimensional code QRCode pictures theImageio.write (bufimg, "PNG", imgfile); - in}Catch(Exception e) { the e.printstacktrace (); the } About the } the the /** + * @paramargs the command line arguments - */ the Public Static voidMain (string[] args) {BayiString Imgpath = "D:/test/twocode/michael_qrcode.png"; the theString content = "Hello big, small, welcome to qrcode!" -+ "\nmyblog [http://sjsky.iteye.com]" -+ "\nemail [[email protected]]" + "\ntwitter [@suncto]"; the theQrcodeencoderhandler handler =NewQrcodeencoderhandler (); the Handler.encoderqrcode (content, Imgpath); the -SYSTEM.OUT.PRINTLN ("Encoder qrcode Success"); the } the}
"Two", decoding:
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 {/** * decode QR code * @param imgpath * @return String */public string D Ecoderqrcode (String Imgpath) {//qrcode QR code picture file ImageFile = new files (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.getme Ssage ()); 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) {Qrcodedeco Derhandler handler = new Qrcodedecoderhandler (); String Imgpath = "D:/test/twocode/michael_qrcode.png"; String decodercontent = Handler.decoderqrcode (Imgpath); System.out.println ("Parse the result 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); } }}
Java implements encoding and decoding of QR code QRCode