二维码的生成和解析
Zxing方式
依赖:
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.1</version>
</dependency>
package com.tobiasy.toolkit.qrcode;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Hashtable;
/**
* @author tobiasy
*/
public class ZxingUtils {
public static void main(String[] args) {
createZxing();
parse();
}
/**
* 创建二维码
*/
public static void createZxing() {
String text = "https://www.cnblogs.com/tobiasy/";
int width = 500;
int height = 500;
String format = "png";
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN, 5);
BitMatrix bitMatrix = null;
try {
bitMatrix = new MultiFormatWriter().encode(
text, BarcodeFormat.QR_CODE, width, height, hints);
Path outputFile = new File("e:/new.png").toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, outputFile);
} catch (WriterException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 解析二维码
*/
public static void parse() {
MultiFormatReader formatReader = new MultiFormatReader();
File file = new File("e:/291625446668002068.png");
BufferedImage image = null;
try {
image = ImageIO.read(file);
BinaryBitmap binaryBitmap = new BinaryBitmap(
new HybridBinarizer(
new BufferedImageLuminanceSource(image)));
//定义二维码参数
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN, 5);
Result result = formatReader.decode(binaryBitmap, hints);
System.out.println(result.getText().toLowerCase());
} catch (IOException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
}
}
}
Qrcode方式
依赖:maven仓库里面没能找到相匹配的依赖
只能引入QRCode.jar包,jar文件地址:链接:https://pan.baidu.com/s/1cp1RNqcAFlToAM_TYCUjAg 密码:oknt
package com.tobiasy.toolkit.qrcode;
import com.google.zxing.NotFoundException;
import com.swetake.util.Qrcode;
import jp.sourceforge.qrcode.QRCodeDecoder;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
* @author tobiasy
*/
public class QrCodeUtils {
public static void main(String[] args) throws IOException, NotFoundException {
createQrcode();
parseQrcode();
}
/**
* Qrcode生成二维码
* @throws IOException
*/
public static void createQrcode() throws IOException {
Qrcode x = new Qrcode();
x.setQrcodeErrorCorrect('M');//纠错等级
x.setQrcodeEncodeMode('B');//N代表数字,A代表a-Z,B代表其他字符
int version = 3;
x.setQrcodeVersion(version);//版本
String qrData = "https://www.cnblogs.com/tobiasy/";
int width = 67 + 12 * (version - 1);
int height = 67 + 12 * (version - 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("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, 3, 3);
}
}
}
}
gs.dispose();
bufferedImage.flush();
ImageIO.write(bufferedImage, "png", new File("E:/qrcode.png"));
}
/**
* Qrcode解析二维码
* @throws IOException
* @throws NotFoundException
*/
public static void parseQrcode() throws IOException, NotFoundException {
File file = new File("E:/qrcode.png");
BufferedImage bufferedImage = ImageIO.read(file);
QRCodeDecoder codeDecoder = new QRCodeDecoder();
String result = new String(codeDecoder.decode(
new MyQRCodeImage(bufferedImage)), "utf-8");
System.out.println(result);
}
}
注意:当二维码中存放网址路径时,需要http://打头二维码才会自动打开网址

浙公网安备 33010602011771号