生成二维码
package com.test.main;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
public class CreateQRCodeDemo {
public static void main(String[] args) throws IOException {
String content = "http://www.baidu.com";//生成二维码内容
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M');//纠错率L(7%)、M(15%)、Q(25%)、H(30%),char类型
qrcode.setQrcodeEncodeMode('B');//A:a~Z;B:其他字符,包括中文
qrcode.setQrcodeVersion(7);//QRcode版本:1~40
//固定公式 二维码的宽和高
int width = 67+12*(qrcode.getQrcodeVersion()-1);
int height = 67+12*(qrcode.getQrcodeVersion()-1);
try {
//读取图片缓冲区的Image
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
//Graphics2D绘图
Graphics2D gs = bufferedImage.createGraphics();
gs.setColor(Color.BLACK);
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, width, height);//清除画板内容
int pixoff = 2;//偏移量
//输出二维码
byte[] bs = content.getBytes("utf-8");//编码方式
if (bs.length>0 && bs.length<120) {
boolean[][] contentOut = qrcode.calQrcode(bs);
for(int i=0;i<contentOut.length;i++){
for(int j=0;j<contentOut.length;j++){
if(contentOut[i][j]){
//二维码图片填充
gs.fillRect(i*3+pixoff,j*3+pixoff,3,3);
//如果i在外层循环,j在内层循环(个人习惯),应该gs.fillRect(i*3+pixoff,j*3+pixoff, 3, 3);
//而不是gs.fillRect(j*3+pixoff,i*3+pixoff, 3, 3);否则解析为一串数字
}
}
}
}
//释放资源
gs.dispose();
bufferedImage.flush();
//存储二维码
File file = new File("F:/MyLife/二维码.png");//二维码存储路径
//判断文件是否已存在
if (!file.exists()) {
OutputStream outputStream = new FileOutputStream(file);
outputStream.flush();
outputStream.close();
}
//生成二维码图片
ImageIO.write(bufferedImage, "png", file);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
解析二维码
package com.test.main;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import jp.sourceforge.qrcode.QRCodeDecoder;
public class ReadQRCodeDemo {
public static void main(String[] args) throws IOException {
//二维码存储地址
File file = new File("F:/MyLife/二维码.png");
//识别成图片
BufferedImage bufferedImage = ImageIO.read(file);
QRCodeDecoder codeDecoder = new QRCodeDecoder();
//ReadQRCodeDemo2--QRCodeImage
String result = new String(codeDecoder.decode(new ReadQRCodeDemo2(bufferedImage)),"gb2312");
System.out.println("解析二维码结果:"+result);
}
}
实现类
package com.test.main;
import java.awt.image.BufferedImage;
import jp.sourceforge.qrcode.data.QRCodeImage;
public class ReadQRCodeDemo2 implements QRCodeImage {
BufferedImage bufferedImage;
public ReadQRCodeDemo2(BufferedImage bufferedImage){
this.bufferedImage = bufferedImage;
}
@Override
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();
}
}
简单的二维码生成和解析,与君共勉