二维码应该称为二维条码,扫描之后可以获得更多数据
QR码全称为快速响应矩阵码,1994年日本一个公司发明
黑色表示1,白色表示0
3个角落比较像“回”字,叫定位点
容错机制,当7%~30%破损任可以被读取,可以利用这个机制在二维码中放入一些小的logo
最多可以存储7089个数字字符或4296个字母或1800个中文汉字(gbk)
目前在Github上最流行的java实现条形码库是:
https://github.com/zxing/zxing
javase的相关jar包下载方式相同
下面这张图片是各个包中的帮助文档,在主页面中
将两个jar包复制到java项目中,并且添加到内路径上
package com.laning.qrcode;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
public class QRCode {
private static int index =1;
//main方法,方法的入口
public static void main(String[] args) {
String str1 = "李长斌有毛病,哈哈哈";
String str2 = "刘志文有毛病,哈哈哈";
String str3 = "李 明有毛病,哈哈哈";
File dstfile1 = new File("E:/大一课程/java编程/JAVA生成二维码jar包");
try {
createQRCode(str1, dstfile1);
createQRCode(str2, dstfile1);
createQRCode(str3, dstfile1);
} catch (WriterException | IOException e) {
e.printStackTrace();
}
createLogoQRCode(new File("E:/大一课程/java编程/JAVA生成二维码jar包/1.jpg"), str1,dstfile1 );
}
//专门用于生成二维码的工具方法
private static void createQRCode(String str ,File dst) throws WriterException, IOException{
//该类用于多种格式二维码的编码
MultiFormatWriter writer = new MultiFormatWriter();
//用来设置二维码的宽高
int width = 200;
int height = 200;
//用来设置二维码的一些属性
HashMap<EncodeHintType,Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
//用来编写二维码,返回二进制矩阵数据
BitMatrix bitmatrix = writer.encode(str,BarcodeFormat.QR_CODE, width, height,hints);
//三个参数,二进制矩阵数组,图片的格式,图片保存的路径
MatrixToImageWriter.writeToPath(bitmatrix,"png",new File(dst.getPath()+"/"+(index++)+".png").toPath());
}
//利用二维码容错机制生成一个简单的logo的工具方法,三个参数分别是目标图片,想要展示的内容,二维码存放地址
private static void createLogoQRCode(File dstimg,String str,File dst){
//该类用于多种格式二维码的编码
MultiFormatWriter writer = new MultiFormatWriter();
//用来设置二维码的宽高
int width = 200;
int height = 200;
//用来设置二维码的一些属性
HashMap<EncodeHintType,Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
//用来编写二维码,返回二进制矩阵数据
BitMatrix bitmatrix = null;
try {
bitmatrix = writer.encode(str,BarcodeFormat.QR_CODE, width, height,hints);
} catch (WriterException e1) {
e1.printStackTrace();
}
//创建图片,宽,高,
BufferedImage qr = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
for(int x=0;x<width;x++){
for(int y=0;y<height;y++){
if(bitmatrix.get(x, y)){//如果矩阵数组中有点的话,可以设置任意颜色
qr.setRGB(x, y, 0xff0000);
}else{
qr.setRGB(x, y, 0xffffff);
}
}
}
Image logoUse = null;
//将logo缩小,宽,高,缩小方式
int logoW=30;
int logoH=30;
try {
//读取logo
BufferedImage logoImg = ImageIO.read(dstimg);
logoUse = logoImg.getScaledInstance(logoW,logoH,Image.SCALE_FAST);
} catch (IOException e) {
e.printStackTrace();
}
//获取画笔
Graphics g = qr.getGraphics();
//绘制Logo,x坐标,y坐标
int logoX = (width-logoW)/2;
int logoY = (height-logoH)/2;
g.drawImage(logoUse,logoX,logoY, null);
try {
ImageIO.write(qr, "png",new File(dst.getPath()+"/"+(index++)+".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
}