简介
二维码是将有效信息通过加密算法转换成一个二维图片,其中三个角上大型矩形是用来扫描定位的,其余部分就是一个由0,1组成的二维数组,0表示无色,1表示有色
编写之前需要下载相关工具类QRCode
代码
Demo.java
package qrcode;
public class Demo {
public static void main(String[] args) throws Exception {
String imgPath = "src/二维码.png";
String content = "helloworld";
//String content = "http://www.baidu.com";//网址自动解析
//生成二维码
QRCodeUtil qrUtil = new QRCodeUtil();
//加密:文字->二维码
qrUtil.encoderQRCode(content, imgPath, "png", 20);
// 解密:二维码->文字
String imgContent = qrUtil.decoderQRCode(imgPath);
System.out.println(imgContent);
}
}
QRCodeUtil.java
package qrcode;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
import jp.sourceforge.qrcode.QRCodeDecoder;
public class QRCodeUtil {
// 加密:文字->二维码
public void encoderQRCode(String content, String imgPath, String imgType, int size) throws Exception{
BufferedImage bufImg = qRcodeCommon(content, imgType, size);
File file = new File(imgPath);//"src/二维码.png" -->二维码.png
//API
ImageIO.write(bufImg, imgType, file);
}
//生成一个二维码的BufferdImge
/**
* content:二维码中隐藏广度信息
* imgType:图片格式
* size:二维码边长
* @throws IOException
*/
public BufferedImage qRcodeCommon(String content, String imgType, int size) throws IOException {
BufferedImage bufImg = null;
//Qrcode对象:字符串->boolean[][]
Qrcode qrCodeHandle = new Qrcode();
//设置二维码的拍错率:7%<L<M<Q<H<30%,拍错率越高,可存储的信息越小,但是对二维码的清晰度要求越小
qrCodeHandle.setQrcodeErrorCorrect('M');
//可存放的信息类型:N:数字, A:数字+A-Z, B:所有
qrCodeHandle.setQrcodeEncodeMode('B');
//尺寸:取值范围1-40
qrCodeHandle.setQrcodeVersion(size);
int imgSize = 67 + 12*(size - 1);//放大
//内存中的图片
bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);
//"helloworld" --> byte[]"helloworld"
byte[] contentBytes = content.getBytes("UTF-8");
//-->boolean[][] 二维码的二维数组
boolean[][] codeOut = qrCodeHandle.calQrcode(contentBytes);
Graphics2D gs = bufImg.createGraphics();
gs.setBackground(Color.WHITE);//画板背景为白色
gs.clearRect(0, 0, imgSize, imgSize);//初始化
gs.setColor(Color.BLACK);//二维码颜色
int pixoff = 2; //偏移量
for(int i = 0; i < codeOut.length; i++) {
for(int j = 0; j < codeOut.length; j++) {
if(codeOut[i][j]) {
gs.fillRect(i*3+pixoff, j*3+pixoff, 3, 3);
};
}
}
//增加logo
Image logo = ImageIO.read(new File("src/logo.jpg"));
int maxHeight = bufImg.getHeight();
int maxWdith = bufImg.getWidth();
//在已生成的二维码上画logo
gs.drawImage(logo, imgSize/5*2, imgSize/5*2, maxWdith/5, maxHeight/5, null);
gs.dispose();//释放空间
bufImg.flush();//清理
return bufImg;
}
//解密:二维码->文字
public String decoderQRCode(String imgPath) throws IOException {
//内存中的二维码
BufferedImage bufImg = ImageIO.read(new File(imgPath));
//解密
QRCodeDecoder decoder = new QRCodeDecoder();
TwoDimensionCodeImage tdcImage = new TwoDimensionCodeImage(bufImg);
byte[] bs = decoder.decode(tdcImage);
//byte[] -->string
String content = new String(bs, "utf-8");
return content;
}
}
TwoDimensionCodeImage.java
package qrcode;
import java.awt.image.BufferedImage;
import jp.sourceforge.qrcode.data.QRCodeImage;
public class TwoDimensionCodeImage implements QRCodeImage{
BufferedImage bufImg;// 内存中的二维码
public TwoDimensionCodeImage(BufferedImage bufImg) {
this.bufImg = bufImg;
}
@Override
public int getHeight() {
return bufImg.getHeight();
}
@Override
public int getPixel(int x, int y) {
return bufImg.getRGB(x, y);
}
@Override
public int getWidth() {
return bufImg.getWidth();
}
}