Java generated two-dimensional code
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:
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; /** * Two-dimensional code generator * @blog http://sjsky.iteye.com * @author Michael */public class Qrcodeencoderhandler {/* * * Generated two-dimensional code (qrcode) Picture * @param content * @param imgpath/public void Encoderqrcode (String Co
Ntent, 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 < 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 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[) a
RGS) {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:
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:
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 {/** * solution
Code Two-dimensional code * @param imgpath * @return string/public string Decoderqrcode (string imgpath) {
QRCode Two-dimensional code picture of 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 ("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!!!