二维码的生成和读取有以下几种方式:
1、使用zxing的jar包
https://github.com/zxing/zxing/releases
生成二维码:
package com.imooc.zxing;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
//生成二维码
public class CreateQRCode {
public static void main(String[] args) {
int width=300;
int height=300;
String format="png";
String content="www.baidu.com";
//定义二维码的参数
HashMap hints=new HashMap();
hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");//字符集
hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);//纠正等级
hints.put(EncodeHintType.MARGIN,2);//边距
//生成二维码
try {
BitMatrix bitMatrix=new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,width,height,hints);
Path file=new File("D:/Downoad/IMOOC/DemoCode/QRCode/img.png").toPath();
MatrixToImageWriter.writeToPath(bitMatrix,format,file);
} catch (Exception e) {
e.printStackTrace();
}
}
}
解析二维码:
package com.imooc.zxing;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
/**
* @Author Rhine
* @Date 2019/1/30 11:21
**/
public class ReadQRCode {
public static void main(String[] args) {
MultiFormatReader formatReader = new MultiFormatReader();
File file = new File("D:/Downoad/IMOOC/DemoCode/QRCode/img.png");
BufferedImage image = null;
try {
image = ImageIO.read(file);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
//定义二维码的参数
HashMap hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");//字符集
Result result = formatReader.decode(binaryBitmap, hints);
System.out.println("解析结果:"+result.toString());
System.out.println("二维码格式类型:"+result.getBarcodeFormat());
System.out.println("二维码文本内容:"+result.getText());
} catch (NotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、使用QRCode 的jar包
生成二维码: http://www.swetake.com/qrcode/index-e.html
读取二维码:https://zh.osdn.net/projects/qrcode/
生成二维码:
package com.imooc.qrcode;
import com.swetake.util.Qrcode;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
/**
* @Author Rhine
* @Date 2019/1/30 14:16
**/
//使用QRCode jar包
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="www.baidu.com";
int width=67+12*(7-1);//公式: 67+12*(版本号-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");//有汉字需要加上GB2312
if (d.length>0 && d.length <120){
boolean[][] s = x.calQrcode(d);
for (int i=0;i
解析二维码:
package com.imooc.qrcode;
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
* @Author Rhine
* @Date 2019/1/30 14:42
**/
public class ReadQRCode {
public static void main(String[] args) throws IOException {
File file = new File("D:/Downoad/IMOOC/DemoCode/QRCode/img2.png");
BufferedImage bufferedImage = ImageIO.read(file);
QRCodeDecoder codeDecoder = new QRCodeDecoder();
String result = new String(codeDecoder.decode(new QRCodeImage() {
@Override
public int getWidth() {
return bufferedImage.getWidth();
}
@Override
public int getHeight() {
return bufferedImage.getHeight();
}
@Override
public int getPixel(int i, int i1) {
return bufferedImage.getRGB(i, i1);
}
}));
System.out.println(result);
}
}
3、jquery-qrcode
https://github.com/jeromeetienne/jquery-qrcode
此种方式通过前端jsp的方式,所以不推荐使用
注意事项:
1、如果扫出来的是一个文本,而不是打开链接,例如扫出 www.baidu.com,而不是打开网页,需要将生成二维码的内容 " www.baidu.com "改为 " http://www.baidu.com "
2、实现二维码扫描安装手机软件
一种情况是通过url访问接口,一种是直接访问静态资源
http://www.baidu.com/mobile/abc.apk这种是可以直接下载的
3、二维码扫描名片
https://en.wikipedia.org/wiki/VCard
VCard规范:(String 进行拼接时,换行的操作使用 \n )
BEGIN:VCARD
VERSION:2.1
N:Gump;Forrest;;Mr. 姓名
FN:Forrest Gump 姓名
ORG:Bubba Gump Shrimp Co. 公司部门
TITLE:Shrimp Man 职位
PHOTO;GIF:http://www.example.com/dir_photos/my_photo.gif
TEL;WORK;VOICE:(111) 555-1212 电话1
TEL;HOME;VOICE:(404) 555-1212 电话
ADR;WORK;PREF:;;100 Waters Edge;Baytown;LA;30314;United States of America 单位地址
LABEL;WORK;PREF;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:100 Waters Edge=0D=
=0ABaytown\, LA 30314=0D=0AUnited States of America
ADR;HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America 家庭地址
LABEL;HOME;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:42 Plantation St.=0D=0A=
Baytown, LA 30314=0D=0AUnited States of America
EMAIL:[email protected] 邮箱地址
REV:20080424T195243Z
END:VCARD