看慕课网教程学习的。
1、首先下载zxing的代码:https://github.com/zxing/zxing/releases,以3.3.2版本为例,下载后解压
2、eclispe新建一个java项目,然后将core和javase目录中的com包复制到eclispe新建的项目中,合并即可,如果提示错误无需理会。
3、项目右击,点击export导出一个JAR File,这个就是二维码生成和解析用到的jar包。或者点击下载:https://pan.lanzou.com/i0i555i
4、eclispe中再新建一个项目,并创建一个类:CreateQRCode.java,生成二维码图片
package com.zxing;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
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;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class CreateQRCode {
public static void main(String[] args) {
int width = 300; //定义宽和高
int height = 300;
String format = "png"; //图片格式
String content = "Hello World!"; //存储内容
HashMap hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //字符集,包含中文的话就要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:/img.png").toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
} catch (WriterException | IOException e) {
e.printStackTrace();
}
}
}
5、生成二维码后就可以解析,创建一个类:AnalysisQRCode.java,用来读取二维码图片解析:
package com.zxing;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import javax.imageio.ImageIO;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import utils.DateUtils;
public class AnalysisQRCode {
public static void main(String[] args) {
DateUtils dateUtils = new DateUtils();
MultiFormatReader formatReader = new MultiFormatReader();
try {
File file = new File("D:/img.png");
BufferedImage image = ImageIO.read(file);
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
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.getText());
} catch (Exception e) {
System.err.println(e);
}
}
}
基本完成。
如果要生成的图片带有logo的话,在CreateQRCode.java类可以添加:
BufferedImage img = ImageIO.read(new File("D:/img.png")); //读取生成后的二维码图片
Graphics2D g = img.createGraphics(); //构建绘图图像
BufferedImage logo = ImageIO.read(new File("C:\\Users\\bwt\\Downloads\\unlogin_img.png")); //logo文件
int logoWidth = logo.getWidth(null) > img.getWidth() * 2 / 10 ?
(img.getWidth() * 2 / 10) : logo.getWidth(null);
int logoHeight = logo.getHeight(null) > img.getHeight() * 2 / 10 ?
(img.getHeight() * 2 / 10) : logo.getHeight(null); //设置logo大小
int x = (img.getWidth() - logoWidth) / 2; //位置:默认中间
int y = (img.getHeight() - logoHeight) / 2;
g.drawImage(logo, x, y, logoWidth, logoHeight, null); //绘制图片
g.drawRoundRect(x, y, logoWidth, logoHeight, 15, 15);
g.setStroke(new BasicStroke(2)); //边框宽度
g.setColor(Color.white);
g.drawRect(x, y, logoWidth, logoHeight);
g.dispose();
logo.flush();
img.flush();
ImageIO.write(img, "png", new File("D:/img1.png"));