上篇中小编主要是对二维码进行了理论上的总结。这篇主要是用三种不同的方式,实现简单的二维码的创建和读取功能。
1、zxing(下载链接:http://download.csdn.net/download/u013035612/10112948)
1)创建java-project,引入zxing.jar
2)生成二维码类:CreateQrCode
public class CreateQRCode {
public static void main(String[] args) throws IOException {
//定义二维码宽高等属性
int width=300;
int height=300;
String format="png";
String content="你扫我呀。";
//定义二维码参数
HashMap hs=new HashMap();
hs.put(EncodeHintType.CHARACTER_SET, "utf-8");//编码格式
hs.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//容错级别
hs.put(EncodeHintType.MARGIN, 2);
//生成二维码
try {
BitMatrix bitMatrix=new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,width,height,hs);
Path file=new File("D:/code/img.png").toPath();
MatrixToImageWriter.writeToPath(bitMatrix,format,file);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3)读取二维码信息类:ReadQRCode
public class ReadQRCode {
public static void main(String[] args) throws IOException, NotFoundException {
MultiFormatReader formatReader=new MultiFormatReader();
File file=new File("D:/code/img.png");
BufferedImage image=ImageIO.read(file);
BinaryBitmap binaryBitmap=new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
//定义二维码参数
HashMap hs=new HashMap();
hs.put(EncodeHintType.CHARACTER_SET, "utf-8");//编码格式
Result result=formatReader.decode(binaryBitmap,hs);
System.out.println("解析结果:"+result.toString());
System.out.println("二维码格式类型:"+result.getBarcodeFormat());
System.out.println("二维码文本内容:"+result.getText());
}
}
2、qrcode
1)引入qrcode.jar
2)新建java-project
3)代码实现
·MYQRCodeImage工具类
public class MYQRCodeImage implements QRCodeImage {
BufferedImage bufferedImage;
public MYQRCodeImage(BufferedImage bufferedImage){
this.bufferedImage=bufferedImage;
}
public int getHeight() {
return bufferedImage.getHeight();
}
@Override
public int getPixel(int arg0, int arg1) {
return bufferedImage.getRGB(arg0, arg1);
}
@Override
public int getWidth() {
return bufferedImage.getWidth();
}
}
public class CreateQRcode {
public static void main(String[] args) throws IOException {
Qrcode x=new Qrcode();
x.setQrcodeErrorCorrect('M');//纠错等级
x.setQrcodeEncodeMode('B');//N代表数字,A代表a-Z,B代表其他字符
x.setQrcodeVersion(7);//版本
String qrData="能扫到我,就成功啦。";
int width=67+12*(7-1);
int height=67+12*(7-1);
BufferedImage bufferedImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D gs=bufferedImage.createGraphics();
gs.setBackground(Color.WHITE);
gs.setColor(Color.BLACK);
gs.clearRect(0, 0, width, height);
int pixoff=2;//偏移量
byte[] d =qrData.getBytes("gb2312");
if (d.length>0 && d.length <120){
boolean[][] s = x.calQrcode(d);
for (int i=0;i<s.length;i++){
for (int j=0;j<s.length;j++){
if (s[j][i]) {
gs.fillRect(j*3+pixoff,i*3,3,3);
}
}
}
}
gs.dispose();
bufferedImage.flush();
ImageIO.write(bufferedImage, "png", new File("D:/code/qrcode.png"));
}
}
·读取二维码类
public class ReadQRCode {
public static void main(String[] args) throws IOException {
File file=new File("D:/code/qrcode.png");
BufferedImage bufferedImage=ImageIO.read(file);
QRCodeDecoder codeDecoder=new QRCodeDecoder();
String result=new String(codeDecoder.decode(new MYQRCodeImage(bufferedImage)),"gb2312");
System.out.println(result);
}
}
3、使用jquery.qrcode.js
1)新建java-web项目
2)引入jquery.js和jquery.qrcode.min.js
3)新建一个jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>生成二维码</title>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.qrcode.min.js"></script>
</head>
<body>
生成的二维码如下:
<div id="qrcode"></div>
<script type="text/javascript">
jQuery('#qrcode').qrcode("http://baidu.com");//可以是文本,也可以是链接(可以直接访问到链接)
</script>
</body>
</html>
小结:
通过这三种方式,都可以生成二维码。非常简单,容易上手。当然还可以将上述二维码中包含的文字换成链接,从而可以访问某个页面。使用VCard实现二维码扫描安装手机软件。(腾讯域名下的apk)等。