TwoDimensionCode綾伙細浜岀淮鐮佹搷浣滄牳蹇冪被
package qrcode;
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 TwoDimensionCode {
/**
* 鐢熸垚浜岀淮鐮?QRCode)鍥劇墖
* @param content 瀛樺偍鍐呭
* @param imgPath 鍥劇墖璺緞
*/
public void encoderQRCode(String content, String imgPath) {
this.encoderQRCode(content, imgPath, "png", 7);
}
/**
* 鐢熸垚浜岀淮鐮?QRCode)鍥劇墖
* @param content 瀛樺偍鍐呭
* @param output 杈撳嚭嫻?br /> */
public void encoderQRCode(String content, OutputStream output) {
this.encoderQRCode(content, output, "png", 7);
}
/**
* 鐢熸垚浜岀淮鐮?QRCode)鍥劇墖
* @param content 瀛樺偍鍐呭
* @param imgPath 鍥劇墖璺緞
* @param imgType 鍥劇墖綾誨瀷
*/
public void encoderQRCode(String content, String imgPath, String imgType) {
this.encoderQRCode(content, imgPath, imgType, 7);
}
/**
* 鐢熸垚浜岀淮鐮?QRCode)鍥劇墖
* @param content 瀛樺偍鍐呭
* @param output 杈撳嚭嫻?br /> * @param imgType 鍥劇墖綾誨瀷
*/
public void encoderQRCode(String content, OutputStream output, String imgType) {
this.encoderQRCode(content, output, imgType, 7);
}
/**
* 鐢熸垚浜岀淮鐮?QRCode)鍥劇墖
* @param content 瀛樺偍鍐呭
* @param imgPath 鍥劇墖璺緞
* @param imgType 鍥劇墖綾誨瀷
* @param size 浜岀淮鐮佸昂瀵?br /> */
public void encoderQRCode(String content, String imgPath, String imgType, int size) {
try {
BufferedImage bufImg = this.qRCodeCommon(content, imgType, size);
File imgFile = new File(imgPath);
// 鐢熸垚浜岀淮鐮丵RCode鍥劇墖
ImageIO.write(bufImg, imgType, imgFile);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 鐢熸垚浜岀淮鐮?QRCode)鍥劇墖
* @param content 瀛樺偍鍐呭
* @param output 杈撳嚭嫻?br /> * @param imgType 鍥劇墖綾誨瀷
* @param size 浜岀淮鐮佸昂瀵?br /> */
public void encoderQRCode(String content, OutputStream output, String imgType, int size) {
try {
BufferedImage bufImg = this.qRCodeCommon(content, imgType, size);
// 鐢熸垚浜岀淮鐮丵RCode鍥劇墖
ImageIO.write(bufImg, imgType, output);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 鐢熸垚浜岀淮鐮?QRCode)鍥劇墖鐨勫叕鍏辨柟娉?br /> * @param content 瀛樺偍鍐呭
* @param imgType 鍥劇墖綾誨瀷
* @param size 浜岀淮鐮佸昂瀵?br /> * @return
*/
private BufferedImage qRCodeCommon(String content, String imgType, int size) {
BufferedImage bufImg = null;
try {
Qrcode qrcodeHandler = new Qrcode();
// 璁劇疆浜岀淮鐮佹帓閿欑巼錛屽彲閫塋(7%)銆丮(15%)銆丵(25%)銆丠(30%)錛屾帓閿欑巼瓚婇珮鍙瓨鍌ㄧ殑淇℃伅瓚婂皯錛屼絾瀵逛簩緇寸爜娓呮櫚搴︾殑瑕佹眰瓚婂皬
qrcodeHandler.setQrcodeErrorCorrect('M');
qrcodeHandler.setQrcodeEncodeMode('B');
// 璁劇疆璁劇疆浜岀淮鐮佸昂瀵革紝鍙栧艱寖鍥?-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);
// 璁劇疆鍋忕Щ閲忥紝涓嶈緗彲鑳藉鑷磋В鏋愬嚭閿?/span>
int pixoff = 2;
// 杈撳嚭鍐呭> 浜岀淮鐮?/span>
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;
}
/**
* 瑙f瀽浜岀淮鐮侊紙QRCode錛?br /> * @param imgPath 鍥劇墖璺緞
* @return
*/
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 TwoDimensionCodeImage(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;
}
/**
* 瑙f瀽浜岀淮鐮侊紙QRCode錛?br /> * @param input 杈撳叆嫻?br /> * @return
*/
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 TwoDimensionCodeImage(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 = "G:/TDDOWNLOAD/Michael_QRCode.png";
String encoderContent = "Hello 澶уぇ銆佸皬灝?welcome to QRCode!" + "\nMyblog [ http://sjsky.iteye.com ]" + "\nEMail [ sjsky007@gmail.com ]";
TwoDimensionCode handler = new TwoDimensionCode();
handler.encoderQRCode(encoderContent, imgPath, "png");
// try {
// OutputStream output = new FileOutputStream(imgPath);
// handler.encoderQRCode(content, output);
// } catch (Exception e) {
// e.printStackTrace();
// }
System.out.println("========encoder success");
String decoderContent = handler.decoderQRCode(imgPath);
System.out.println("瑙f瀽緇撴灉濡備笅錛?);
System.out.println(decoderContent);
System.out.println("========decoder success!!!");
}
}
TwoDimensionCodeImage 綾伙細浜岀淮鐮佸浘鐗囧璞?/span>
package qrcode;
import java.awt.image.BufferedImage;
import jp.sourceforge.qrcode.data.QRCodeImage;
public class TwoDimensionCodeImage implements QRCodeImage {
BufferedImage bufImg;
public TwoDimensionCodeImage(BufferedImage bufImg) {
this.bufImg = bufImg;
}
@Override
public int getHeight() {
return bufImg.getHeight();
}
@Override
public int getPixel(int x, int y) {
return bufImg.getRGB(x, y);
}
@Override
public int getWidth() {
return bufImg.getWidth();
}
}

]]>