周末试用下Android手机的二维码扫描软件,扫描了下火车票、名片等等,觉得非常不错很有意思的。当然Java也可以实现这些,现在就分享下如何简单用Java实现二维码中QRCode的编码和解码(可以手机扫描验证)。
涉及到的一些主要类库,方便大家下载:
- 编码 lib:Qrcode_swetake.jar (官网介绍 -- http://www.swetake.com/qr/index-e.html)
- 解码 lib:qrcode.jar (官网介绍 -- http://sourceforge.jp/projects/qrcode/)
后来发现一个更好的条形码和二维码的开源软件(ZXing),详细介绍见:http://sjsky.iteye.com/blog/1142177
【一】、编码:
QRCodeEncoderHandler.java
package michael.qrcode;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
public classQRCodeEncoderHandler {
public voidencoderQRCode(String content, String imgPath) {
try {
Qrcode qrcodeHandler = newQrcode();
qrcodeHandler.setQrcodeErrorCorrect('M');
qrcodeHandler.setQrcodeEncodeMode('B');
qrcodeHandler.setQrcodeVersion(7);
System.out.println(content);
byte[] contentBytes= content.getBytes("gb2312");
BufferedImage bufImg = newBufferedImage(140, 140,
BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufImg.createGraphics();
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, 140, 140);
// 设定图像颜色 > BLACK
gs.setColor(Color.BLACK);
// 设置偏移量 不设置可能导致解析出错
int pixoff = 2;
// 输出内容 > 二维码
if (contentBytes.length> 0 && contentBytes.length< 120) {
boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
for (inti = 0; i < codeOut.length;i++) {
for (intj = 0; j < codeOut.length;j++) {
if (codeOut[j][i]){
gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
} else{
System.err.println("QRCodecontent bytes length = "
+ contentBytes.length+ " not in [ 0,120]. ");
}
gs.dispose();
bufImg.flush();
File imgFile = newFile(imgPath);
// 生成二维码QRCode图片
ImageIO.write(bufImg,"png", imgFile);
} catch(Exception e) {
e.printStackTrace();
}
}
public staticvoid main(String[] args) {
String imgPath = "D:/test/twocode/Michael_QRCode.png";
String content = "Hello大大、小小,welcome to QRCode!"
+ "\nMyblog[ http://sjsky.iteye.com ]"
+ "\nEMail [ chinajoebaby@hotmail.com ]" +"\nTwitter [ @suncto ]";
QRCodeEncoderHandler handler = newQRCodeEncoderHandler();
handler.encoderQRCode(content, imgPath);
System.out.println("encoderQRcode success");
}
}
运行后生成的二维码图片如下:
![](https://img-my.csdn.net/uploads/201208/15/1345023269_1696.png)
此时就可用手机的二维码扫描软件(本人用的:android 快拍二维码)来测试下,识别成功的截图如下:
![](https://img-my.csdn.net/uploads/201208/15/1345023292_6154.png)
喜欢的朋友可以下载后试一试,做一些名片或者自己喜欢的东西。当然Java也可以对二维码图片解码,具体看下面关于解码的内容。
【二】、解码:
QRCodeDecoderHandler.java
package michael.qrcode;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;
import jp.sourceforge.qrcode.exception.DecodingFailedException;
public classQRCodeDecoderHandler {
public StringdecoderQRCode(String imgPath) {
// QRCode 二维码图片的文件
File imageFile = newFile(imgPath);
BufferedImage bufImg = null;
String decodedData = null;
try {
bufImg = ImageIO.read(imageFile);
QRCodeDecoder decoder = newQRCodeDecoder();
decodedData = newString(decoder.decode(newJ2SEImage(bufImg)));
// try {
// System.out.println(new String(decodedData.getBytes("gb2312"),
// "gb2312"));
// } catch (Exception e) {
// // TODO:handle exception
// }
} catch(IOException e) {
System.out.println("Error:" + e.getMessage());
e.printStackTrace();
} catch(DecodingFailedException dfe) {
System.out.println("Error:" + dfe.getMessage());
dfe.printStackTrace();
}
return decodedData;
}
public staticvoid main(String[] args) {
QRCodeDecoderHandler handler = newQRCodeDecoderHandler();
String imgPath = "d:/test/twocode/Michael_QRCode.png";
String decoderContent = handler.decoderQRCode(imgPath);
System.out.println("解析结果如下:");
System.out.println(decoderContent);
System.out.println("========decodersuccess!!!");
}
class J2SEImageimplementsQRCodeImage {
BufferedImage bufImg;
public J2SEImage(BufferedImagebufImg) {
this.bufImg= bufImg;
}
public intgetWidth() {
return bufImg.getWidth();
}
public intgetHeight() {
return bufImg.getHeight();
}
public intgetPixel(intx, inty) {
return bufImg.getRGB(x,y);
}
}
}
运行结果如下(解码出的内容和之前输入的内容一致):
解析结果如下:
Hello 大大、小小,welcome to QRCode!
Myblog [ http://sjsky.iteye.com ]
EMail [ sjsky007@gmail.com ]
Twitter [ @suncto ]
========decoder success!!!
本文连接:http://sjsky.iteye.com/blog/1136934
1.project右键-properties-Java Build path
2.操作图如下: