現在貼出部分核心代碼,在最後提供全部代碼下載連結。有感興趣的朋友,可以點選連結下載原始碼
二維碼,是一種採用黑白相間的平面幾何圖形通過相應的編碼演算法來記錄文字、圖片、網址等資訊的條碼圖片。
如下圖
一、Java
所需jar包:QRCode.jar
http://vdisk.weibo.com/
QRCode類:二維碼操作核心類
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);
// 產生二維碼QRCode圖片
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);
// 產生二維碼QRCode圖片
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();
// 設定二維碼排錯率,可選L(7%)、M(15%)、Q(25%)、H(30%),排錯率越高可儲存的資訊越少,但對二維碼清晰度的要求越小
qrcodeHandler.setQrcodeErrorCorrect('M');
qrcodeHandler.setQrcodeEncodeMode('B');
// 設定設定二維碼尺寸,取值範圍1-40,值越大尺寸越大,可儲存的資訊越大
qrcodeHandler.setQrcodeVersion(size);
// 獲得內容的位元組數組,設定編碼格式
byte[] contentBytes = content.getBytes("utf-8");
// 圖片尺寸
int imgSize = 67 + 12 * (size - 1);
bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufImg.createGraphics();
// 設定背景顏色
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, imgSize, imgSize);
// 設定映像顏色> BLACK
gs.setColor(Color.BLACK);
// 設定位移量,不設定可能導致解析出錯
int pixoff = 2;
// 輸出內容> 二維碼
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 二維碼圖片的檔案
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("========風吹雨成花======");
String decoderContent = handler.decoderQRCode(imgPath);
System.out.println("解析結果如下:");
System.out.println(decoderContent);
System.out.println("========時間追不上白馬=======");
}
}
===================================
CodeImage 類:二維碼圖片對象
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();
}
}
========================================================================
代碼下載地址:執行個體代碼下載連結
一維碼產生可以參考:http://blog.csdn.net/cdl0405/article/details/5990545
二維碼產生可以參考:http://blog.csdn.net/cdl0405/article/details/5990567