1.下载QRcode的jar包
博主网盘直接下载
链接:https://pan.baidu.com/s/1LitW9vTykg54WJqvF2WXhw
提取码:a89a
2.安装到本地仓库
mvn install:install-file -Dfile=jar包的位置 -DgroupId=上面的groupId -DartifactId=上面的artifactId -Dversion=上面的version -Dpackaging=jar
3.导入依赖
com.qrcode qrcode 1.0 com.google.zxing core 3.3.0 com.google.zxing javase 3.0.0 4.生成以及解析工具
package com.example.qrCodeDemo;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.google.zxing.;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.itextpdf.text.pdf.qrcode.EncodeHintType;
import com.swetake.util.Qrcode;
import javax.imageio.ImageIO;
import java.awt.;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.HashMap;
/**
-
@Description: QRcode生成二维码
-
@Author ajin
-
@Version 1.0
-
@Tool: IntelliJ IDEA
*/
public class QRCodeUtil {
private static final String path = “E:\”;
private static final String fileName = “qrcode.png”;
public static void main(String[] args) throws IOException, ChecksumException, NotFoundException, FormatException {
// new QRCodeUtil().generateCode();
new QRCodeUtil().readCode();
}
/**
- @Author: ajin
- @Description: 生成QRcode
*/
public void generateCode() throws UnsupportedEncodingException {
Qrcode x = new Qrcode();
x.setQrcodeErrorCorrect(‘M’);//纠错等级
x.setQrcodeEncodeMode(‘B’);//N 代表数据; A 代表a-A; B 代表其他字符
x.setQrcodeVersion(7);//版本
String qrData = “https://www.baidu.com/”;
int width = 67 + 12 * (7 - 1);
int height = 67 + 12 * (7 - 1);
int pixoff = 2;//偏移量
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);
byte[] d = qrData.getBytes(“utf-8”);
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 + pixoff, 3, 3);
}
}
}
}
gs.dispose();
bufferedImage.flush();
try {
ImageIO.write(bufferedImage, “png”, new File(path + fileName));
OSS ossClient = new OSSClientBuilder().build(“YourEndpoint”, “YourAccessKeyId”, “YourSecretAccessKey”);
ossClient.putObject(“YourBucket”, fileName, new FileInputStream(path + fileName));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
- @Author: ajin
- @Description: 解析QRcode
*/
public void readCode() throws ChecksumException, NotFoundException, FormatException, IOException {
BufferedImage bufferedImage = ImageIO.read(new URL(“https://picturelive0.oss-cn-beijing.aliyuncs.com/qrcode.png”));
BufferedImageLuminanceSource bufferedImageLuminanceSource = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(bufferedImageLuminanceSource));
HashMap hashMap = new HashMap();
hashMap.put(EncodeHintType.CHARACTER_SET, “utf-8”);//编码方式
hashMap.put(BarcodeFormat.QR_CODE, “QR_CODE”);//二维码类型
hashMap.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);//复杂模式,开启PURE_BARCODE模式
Result decode = new MultiFormatReader().decode(binaryBitmap, hashMap);
String text = decode.getText();
System.out.println(“内容为” + text);
}
}