Now post some of the core code, at the end provide all the code download link. Interested friends, you can click on the link to download the source code
Two-dimensional code, is a black-and-white plane geometry through the corresponding coding algorithm to record text, pictures, URLs and other information of the barcode picture.
The following figure
One, Java
Jar Package Required: Qrcode.jar
http://vdisk.weibo.com/
QRCode class: Two-dimensional code operation core class
Package app. Code;
Import Java.awt.Color;
Import Java.awt.Graphics2D;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Javax.imageio.ImageIO;
Import Jp.sourceforge.qrcode.QRCodeDecoder;
Import jp.sourceforge.qrcode.exception.DecodingFailedException;
Import Com.swetake.util.Qrcode;
public class QRCode {
public void Encoderqrcode (string content, String Imgpath) {
This.encoderqrcode (content, Imgpath, "PNG", 7);
}
public void Encoderqrcode (String content, outputstream output) {
This.encoderqrcode (content, Output, "PNG", 7);
}
public void Encoderqrcode (string content, String Imgpath, String imgtype) {
This.encoderqrcode (content, Imgpath, Imgtype, 7);
}
public void Encoderqrcode (string content, OutputStream output, string imgtype) {
This.encoderqrcode (content, output, imgtype, 7);
}
public void Encoderqrcode (string content, String Imgpath, string imgtype, int size) {
try {
BufferedImage bufimg = This.qrcodecommon (content, imgtype, size);
File Imgfile = new file (Imgpath);
Generate a two-dimensional code qrcode picture
Imageio.write (bufimg, Imgtype, imgfile);
catch (Exception e) {
E.printstacktrace ();
}
}
public void Encoderqrcode (string content, OutputStream output, string imgtype, int size) {
try {
BufferedImage bufimg = This.qrcodecommon (content, imgtype, size);
Generate a two-dimensional code qrcode picture
Imageio.write (bufimg, imgtype, Output);
catch (Exception e) {
E.printstacktrace ();
}
}
Private BufferedImage Qrcodecommon (string content, string imgtype, int size) {
BufferedImage bufimg = null;
try {
QRCode Qrcodehandler = new QRCode ();
Set the two-dimensional code error rate, optional L (7%), M (15%), Q (25%), H (30%), the higher the rate of error, the less information can be stored, but the requirement for two-dimensional code clarity is smaller
Qrcodehandler.setqrcodeerrorcorrect (' M ');
Qrcodehandler.setqrcodeencodemode (' B ');
Set the two-dimensional code size, the value range 1-40, the larger the value, the larger the size of the information can be stored
Qrcodehandler.setqrcodeversion (size);
Gets the byte array of the content, setting the encoding format
byte[] Contentbytes = content.getbytes ("Utf-8");
Picture size
int imgsize = + (size-1);
bufimg = new BufferedImage (imgsize, imgsize, Bufferedimage.type_int_rgb);
graphics2d GS = Bufimg.creategraphics ();
Set Background color
Gs.setbackground (Color.White);
Gs.clearrect (0, 0, imgsize, imgsize);
Set Image Color > Black
Gs.setcolor (Color.Black);
Set offset, no setting may cause parsing error
int pixoff = 2;
Output > Two-dimensional code
if (contentbytes.length > 0 && contentbytes.length < 800) {
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 {
throw new Exception ("qrcode content bytes Length =" + Contentbytes.length + "not in [0, 800].");
}
Gs.dispose ();
Bufimg.flush ();
catch (Exception e) {
E.printstacktrace ();
}
return bufimg;
}
public string Decoderqrcode (string imgpath) {
QRCode Two-dimensional code picture file
File ImageFile = new file (Imgpath);
BufferedImage bufimg = null;
String content = null;
try {
bufimg = Imageio.read (ImageFile);
Qrcodedecoder decoder = new Qrcodedecoder ();
Content = new String (Decoder.decode (New Codeimage (bufimg)), "Utf-8");
catch (IOException e) {
System.out.println ("Error:" + e.getmessage ());
E.printstacktrace ();
catch (Decodingfailedexception DfE) {
System.out.println ("Error:" + dfe.getmessage ());
Dfe.printstacktrace ();
}
return content;
}
Public String Decoderqrcode (InputStream input) {
BufferedImage bufimg = null;
String content = null;
try {
bufimg = Imageio.read (input);
Qrcodedecoder decoder = new Qrcodedecoder ();
Content = new String (Decoder.decode (New Codeimage (bufimg)), "Utf-8");
catch (IOException e) {
System.out.println ("Error:" + e.getmessage ());
E.printstacktrace ();
catch (Decodingfailedexception DfE) {
System.out.println ("Error:" + dfe.getmessage ());
Dfe.printstacktrace ();
}
return content;
}
public static void Main (string[] args) {
String Imgpath = "F:/qrcode.png";
String encodercontent = "hello,world!";
QRCode handler = new QRCode ();
Handler.encoderqrcode (Encodercontent, Imgpath, "PNG");
try {
OutputStream output = new FileOutputStream (Imgpath);
Handler.encoderqrcode (content, output);
catch (Exception e) {
E.printstacktrace ();
// }
System.out.println ("======== wind Blows Rain into flower)";
String decodercontent = Handler.decoderqrcode (Imgpath);
System.out.println ("The analytic result is as follows:");
System.out.println (decodercontent);
System.out.println ("======== time cannot overtake the White Horse =======");
}
}
===================================
Codeimage class: Two-dimensional code Picture object
Package app. Code;
Import Java.awt.image.BufferedImage;
Import Jp.sourceforge.qrcode.data.QRCodeImage;
public class Codeimage implements qrcodeimage{
BufferedImage bufimg;
Public Codeimage (BufferedImage bufferedimage) {
This.bufimg=bufferedimage;
}
public int getheight () {
TODO auto-generated Method Stub
return Bufimg.getheight ();
}
public int GetPixel (int x, int y) {
TODO auto-generated Method Stub
return Bufimg.getrgb (x, y);
}
public int getwidth () {
TODO auto-generated Method Stub
return Bufimg.getwidth ();
}
}
========================================================================
Code Download Address: Instance code download link
One dimensional code generation can refer to: http://blog.csdn.net/cdl0405/article/details/5990545
Two-dimensional code generation can refer to: http://blog.csdn.net/cdl0405/article/details/5990567