ZXing 是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。Zxing 可以实现使用手机的内置的摄像头完成条形码的扫描及解码。本章讲解用 ZXing 生成和扫码二维码。
依赖
在Java项目中pom.xml加入:
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>${version}</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>${version}</version>
</dependency>
当前最新版本是3.4.1,如果是Java开发的Android项目则引入 android-core 。
生成二维码
- 生成普通二维码
// 二维码内容
String text = "https://engr-z.com";
// 二维码大小
int width = 500, height = 500;
// 二维码输出文件
File file = new File("/home/engr-z/qrcode.png");
QRCodeWriter writer = new QRCodeWriter();
BitMatrix m = writer.encode(text, BarcodeFormat.QR_CODE, width, height);
MatrixToImageWriter.writeToPath(m, "png", file.toPath());
如果内容较多,需要增大二维码尺寸。尺寸小内容多,二维码图形越复杂越难识别。
- 生成带Logo二维码
// 二维码内容
String text = "https://engr-z.com";
// 二维码大小
int width = 500, height = 500;
// 二维码参数
CodeStyle style = new CodeStyle();
style.setWidth(width);
style.setHeight(height);
Map<EncodeHintType, Object> hints = new HashMap<>();
//内容编码格式
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 指定纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//设置二维码边的空度,非负数
hints.put(EncodeHintType.MARGIN, 1);
// 生成二维码图片
QRCodeWriter writer = new QRCodeWriter();
BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, style.getWidth(), style.getHeight(), hints);
int margin = style.getMargin();
int tempM = margin*2;
int[] rec = bm.getEnclosingRectangle(); //获取二维码图案的属性
int resWidth = rec[2] + tempM;
int resHeight = rec[3] + tempM;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); // 按照自定义边框生成新的BitMatrix
resMatrix.clear();
for (int i = margin; i < resWidth - margin; i++) { //循环,将二维码图案绘制到新的bitMatrix中
for (int j = margin; j < resHeight - margin; j++){
if (bm.get(i - margin + rec[0], j - margin + rec[1])){
resMatrix.set(i, j);
}
}
}
bm = resMatrix;
int w = bm.getWidth();
int h = bm.getHeight();
BufferedImage qrcodeBuffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
// 开始利用二维码数据创建Bitmap图片
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
qrcodeBuffImg.setRGB(x, y, bm.get(x, y) ? style.getCodeColor() : style.getBackgroundColor());
}
}
/**
* 读取Logo图片
*/
File logoFile = new File("/home/engr-z/logo.png");
BufferedImage logo = ImageIO.read(logoFile);
/**
* 设置logo的大小,设置为二维码图片的20%
*/
int widthLogo = logo.getWidth(null) > qrcodeBuffImg.getWidth() * 3 / 10 ? (qrcodeBuffImg.getWidth() * 3 / 10)
: logo.getWidth(null),
heightLogo = logo.getHeight(null) > qrcodeBuffImg.getHeight() * 3 / 10 ? (qrcodeBuffImg.getHeight() * 3 / 10) : logo.getWidth(null);
/**
* logo在二维码的位置
*/
int x = (qrcodeBuffImg.getWidth() - widthLogo) / 2;
int y = (qrcodeBuffImg.getHeight() - heightLogo) / 2;
Graphics2D qrcodeOutg = qrcodeBuffImg.createGraphics();
// 把logo写到二维码图片中间
qrcodeOutg.drawImage(logo, x, y, widthLogo, heightLogo, null);
qrcodeOutg.dispose();
qrcodeBuffImg.flush();
// 新的图片,把带logo的二维码下面加上文字
String desc = "https://engr-z.com";
int textHeight = 26;
int textMargin = 10;
BufferedImage outImage = new BufferedImage(qrcodeBuffImg.getWidth(), qrcodeBuffImg.getHeight() + textHeight + (textMargin * 2), BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D outg = outImage.createGraphics();
// 画二维码到新的面板
outg.drawImage(qrcodeBuffImg, 0, 0, qrcodeBuffImg.getWidth(), qrcodeBuffImg.getHeight(), null);
outg.setFont(new Font("宋体", Font.BOLD, 26)); // 字体、字型、字号
int strWidth = outg.getFontMetrics().stringWidth(desc);
outg.setColor(Color.BLACK);
outg.drawString(desc, (outImage.getWidth() - strWidth) / 2, outImage.getHeight() - textMargin);
outg.dispose();
// 二维码输出文件
File file = new File("/home/engr-z/qrcode.png");
ImageIO.write(outImage, "png", file);
CodeStyle是我封装的类,用来设置二维码样式:
/**
* @author Engr-Z
* @since 2020/12/18
*/
@Data
public class CodeStyle {
/**
* 背景颜色,如:0xFFFFFFFF
*/
private int backgroundColor = 0xFFFFFFFF;
/**
* 码颜色,如:0xFF000000
*/
private int codeColor = 0xFF000000;
/**
* 二维码宽,px
*/
private int width;
/**
* 二维码高,px
*/
private int height;
/**
* 边框大小
*/
private int margin = 5;
}
以下是我执行生成的二维码:
65a3947e823124cc0cb142688245a685.png读取二维码
File qrcodeFile = new File("D:/qrcode.png");
BufferedImage qrcodeImg = ImageIO.read(qrcodeFile);
MultiFormatReader formatReader = new MultiFormatReader();
//读取指定的二维码文件
BinaryBitmap binaryBitmap= new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(qrcodeImg)));
//定义二维码参数
Map<DecodeHintType, Object> hints= new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
// 读取
Result result = formatReader.decode(binaryBitmap, hints);
log.info("格式类型:{}", result.getBarcodeFormat());
log.info("二维码内容:{}", result.getText());
链接
ZXing GitHub:https://github.com/zxing/zxing
除非注明,否则均为"攻城狮·正"原创文章,转载请注明出处。
本文链接:https://engr-z.com/292.html
网友评论