QR Code 編碼
import com.swetake.util.Qrcode;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
public class QRCodeEncoderTest
{
public QRCodeEncoderTest()
{
}
public static void main(String[] args)
{
try{
com.swetake.util.Qrcode testQrcode
=
new com.swetake.util.Qrcode();
testQrcode.setQrcodeErrorCorrect(
'M');
testQrcode.setQrcodeEncodeMode(
'B');
testQrcode.setQrcodeVersion(7);
String testString =
"";
testString = testString+
"JavaWorld .\n";
testString = testString+
"中文Java討論網站\n";
testString = testString+
"http://www.javaworld.com\n";
byte[] d = testString.getBytes(
"Big5");
BufferedImage bi
=
new BufferedImage(140, 140, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.setBackground(Color.WHITE);
g.clearRect(0, 0, 140, 140);
g.setColor(Color.BLACK);
if (d.length>0 && d.length <120)
{
boolean[][] s = testQrcode.calQrcode(d);
for (
int i=0;i<s.length;i++)
{
for (
int j=0;j<s.length;j++)
{
if (s[j][i])
{
g.fillRect(j*3+2,i*3+2,3,3);
}
}
}
}
g.dispose();
bi.flush();
String FilePath=
"D:\\TestQRCode.jpg";
File f =
new File(FilePath);
ImageIO.write(bi,
"jpg", f);
}
catch (Exception e)
{
e.printStackTrace();
}
QR Code 解碼
import jp.sourceforge.qrcode.codec.QRCodeDecoder;
import jp.sourceforge.qrcode.codec.data.QRCodeImage;
import jp.sourceforge.qrcode.codec.exception.DecodingFailedException;
import jp.sourceforge.qrcode.codec.exception.InvalidVersionInfoException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
public class QRCodeDecoderTest {
public QRCodeDecoderTest() {
}
public static void main(String[] args) {
QRCodeDecoder decoder = new QRCodeDecoder();
File imageFile = new File("D:\\TestQRCode.jpg");
BufferedImage image = null;
try {
image = ImageIO.read(imageFile);
} catch (IOException e) {
System.out.println("Error: "+ e.getMessage());
}
try {
String decodedData
= new String(decoder.decode(new J2SEImage(image)));
System.out.println(decodedData);
} catch (DecodingFailedException dfe) {
System.out.println("Error: " + dfe.getMessage());
}
}
}
class J2SEImage implements QRCodeImage {
BufferedImage image;
public J2SEImage(BufferedImage image) {
this.image = image;
}
public int getWidth() {
return image.getWidth();
}
public int getHeight() {
return image.getHeight();
}
public int getPixel(int x, int y) {
return image.getRGB(x, y);
}
}
}
}
posted on 2009-06-29 15:59
kelven 阅读(525)
评论(0) 编辑 收藏 所属分类:
java