日本人写的解码与编码实现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);
}
}