日本人寫的解碼與編碼實現Sourceforgejp與
這裡把它們merge到一個jar文件裡
編碼測試
Java代碼
import javaawtColor;
import javaawtGraphicsD;
import javaawtimageBufferedImage;
import javaioFile;
import javaximageioImageIO;
import comswetakeutilQrcode;
public class QRCodeEncoderTest {
public static void main(String[] args) throws Exception {
Qrcode qrcode=new Qrcode();
qrcodesetQrcodeErrorCorrect(M);
qrcodesetQrcodeEncodeMode(B);
qrcodesetQrcodeVersion();
String testString = _+^%$#@!~`=&)*(;
byte[] d =testStringgetBytes(GBK);
BufferedImage bi = new BufferedImage( BufferedImageTYPE_INT_RGB);
// createGraphics
GraphicsD g = bicreateGraphics();
// set background
gsetBackground(ColorWHITE);
gclearRect( );
gsetColor(ColorBLACK);
if (dlength> && dlength <){
boolean[][] b = qrcodecalQrcode(d);
for (int i=;i<blength;i++){
for (int j=;j<blength;j++){
if (b[j][i]) {
gfillRect(j*+i*+);
}
}
}
}
gdispose();
biflush();
String FilePath=TestQRCodepng;
File f = new File(FilePath);
ImageIOwrite(bi png f);
Systemoutprintln(doned!);
}
}
import javaawtColor; import javaawtGraphicsD; import javaawtimageBufferedImage; import javaioFile; import javaximageioImageIO; import comswetakeutilQrcode; public class QRCodeEncoderTest { public static void main(String[] args) throws Exception { Qrcode qrcode=new Qrcode(); qrcodesetQrcodeErrorCorrect(M); qrcodesetQrcodeEncodeMode(B); qrcodesetQrcodeVersion(); String testString = _+^%$#@!~`=&)*(; byte[] d =testStringgetBytes(GBK); BufferedImage bi = new BufferedImage( BufferedImageTYPE_INT_RGB); // createGraphics GraphicsD g = bicreateGraphics(); // set background gsetBackground(ColorWHITE); gclearRect( ); gsetColor(ColorBLACK); if (dlength> && dlength <){ boolean[][] b = qrcodecalQrcode(d); for (int i=;i<blength;i++){ for (int j=;j<blength;j++){ if (b[j][i]) { gfillRect(j*+i*+); } } } } gdispose(); biflush(); String FilePath=TestQRCodepng; File f = new File(FilePath); ImageIOwrite(bi png f); Systemoutprintln(doned!); } }
解碼測試
Java代碼
import javaawtimageBufferedImage;
import javaioFile;
import javaioIOException;
import javaioUnsupportedEncodingException;
import javaximageioImageIO;
import jpsourceforgeqrcodeQRCodeDecoder;
import jpsourceforgeqrcodedataQRCodeImage;
import jpsourceforgeqrcodeexceptionDecodingFailedException;
public class QRCodeDecoderTest {
public QRCodeDecoderTest() {
}
public static void main(String[] args) {
QRCodeDecoder decoder = new QRCodeDecoder();
File imageFile = new File(TestQRCodepng);
BufferedImage image = null;
try {
image = ImageIOread(imageFile);
} catch (IOException e) {
Systemoutprintln(Error: + egetMessage());
}
try {
String decodedData = new String(decoderdecode(new JSEImage(image))GBK);
Systemoutprintln(decodedData);
} catch (DecodingFailedException dfe) {
Systemoutprintln(Error: + dfegetMessage());
} catch (UnsupportedEncodingException e) {
eprintStackTrace();
}
}
}
class JSEImage implements QRCodeImage {
BufferedImage image;
public JSEImage(BufferedImage image) {
thisimage = image;
}
public int getWidth() {
return imagegetWidth();
}
public int getHeight() {
return imagegetHeight();
}
public int getPixel(int x int y) {
return imagegetRGB(x y);
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25835.html