前几天做了一个智能通行,要求我接收硬件生成卡号让我用Java代码生成二维码,在和硬件对接实现扫描开门
(1)要求流程
1、手机app发送请求给我后端,
2、我后端收到请求后,通过kafka消息列队在去请求硬件给我卡号
3、硬件收到请求后生成卡号和有效时期再放回给我后端
4、我后端在次接收硬件的请求后,把硬件生成的卡号生成二维码
后续有俩种解决方案
5、我在把二维码返回给前端App进行开门
5、或者者把二维码保存到我的服务器,我发个短信给App用户,短信内容中有二维码链接,点击获取二维码进行开门
最后选择了第二种解决方案
(2)代码生成二维码源代码
生成二维吗jar包(zxing)
<!-- 生成二维码依赖 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.0</version>
</dependency>
测试生成二维码源代码
package com.lckj.api.card.util;
import java.io.*;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.UUID;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxingmon.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.springframework.stereotype.Service;
@Service
public class QRcodeUtil {
/**
* 生成发送二维码方法
*
* @param text 二维码生成规则(二维码可以是任何英文字母加数字生成的二维码)
* @param width 宽度
* @param height 高度
* @param filePath 输出图片地址
*/
public void getQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
//生成二维码类
QRCodeWriter qrCodeWriter = new QRCodeWriter();
//生成的二维码
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
//生成图片唯一名称,加上.png格式
String pat = getUUID() + ".png";
//图片路劲加上图片名称 (输出地址)
filePath += pat;
Path path = FileSystems.getDefault().getPath(filePath);
//输出二维码图片
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
}
//运行测试
public static void main(String[] args) {
try {
QRcodeUtil qRcodeUtil = new QRcodeUtil();
//Xiaojie wants to eat eggs就是我们的生成规则,可随便填写
qRcodeUtil.getQRCodeImage("Xiaojie wants to eat eggs", 350, 350, "C:\\Users\\Administrator\\Desktop\\");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 把String自带的UUID去除下划线返回
*
* @return String UUID
*/
public String getUUID() {
String s = UUID.randomUUID().toString();
//去掉“-”符号
return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
}
}
桌面看效果
扫出来就是"Xiaojie wants to eat eggs"
“Xiaojie wants to eat eggs” 就是我们自己定义的规则,在main方法中自己定义的
qRcodeUtil.getQRCodeImage(“Xiaojie wants to eat eggs”, 350, 350, “C:\Users\Administrator\Desktop\”);
如果后期要修改生成二维码的规则自己改参数就好了,我在getQRCodeImage()方法上有写参数
(3)如何生成二维码保存到服务器,在通过短信链接让用户收到开门码怎么做
修改二维码生成方法,添加手机号码参数
package com.lckj.api.card.util;
import java.io.*;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.UUID;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxingmon.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.lckj.sms.ShortMessageManager;
import org.springframework.stereotype.Service;
@Service
public class QRcodeUtil {
//Linux保存二维码图片
public String LINUX_QR_CODE_IMAGE_PATH = "/opt/uploadImage/savefiles/user/";
//外网访问域名,自己服务器定义的
public String LINUX_URL = "http://wy.xjycd/savefiles/user/";
/**
* 生成发送二维码方法
*
* @param text 二维码生成规则(二维码可以是任何英文字母加数字生成的二维码)
* @param width 宽度
* @param height 高度
* @param filePath 输出图片地址
* @param phone 手机号
*/
public void getQRCodeImage(String text, int width, int height, String filePath, String phone) throws WriterException, IOException {
//生成二维码类
QRCodeWriter qrCodeWriter = new QRCodeWriter();
//生成的二维码
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
//生成图片唯一名称,加上.png格式
String pat = getUUID() + ".png";
//图片路劲加上图片名称 (输出地址)
filePath += pat;
Path path = FileSystems.getDefault().getPath(filePath);
//输出二维码图片
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
//发送短信接口,自己事先定义好的(如果要学习代码发送短信验证的话可以dd我)
ShortMessageManager showDocumentMessage = new ShortMessageManager();
//发送短信 参数:手机号,内容... 短信内容包括:外网访问服务器域名 + 上图片名称(实现外网访问二维码)
showDocumentMessage.addSendCode(phone, LINUX_URL + pat);
}
/**
* 把String自带的UUID去除下划线返回
* @return String UUID
*/
public String getUUID() {
String s = UUID.randomUUID().toString();
//去掉“-”符号
return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
}
}
调用端代码
//生成二维码工具类 参数:生成规则,宽,高,生成保存地址,发送人手机号
qRcodeUtil.getQRCodeImage(cardInfo.getCardNo(), 350, 350, qRcodeUtil.LINUX_QR_CODE_IMAGE_PATH, uservo.getMobilePhone());
链接打开就是
建议不要使用阿里云的短信平台,因为想让阿里云的短信支持发送链接不太好弄,贼麻烦
(4)如何把生成的二维码返回给前端输出
直接把图片返回给前端的话,对不起,不行,需要转车base64才可以
package com.lckj.api.card.util;
import java.io.*;
import java.util.Base64;
import java.util.UUID;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxingmon.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.springframework.stereotype.Service;
@Service
public class QRcodeUtil {
/**
* 生成发送二维码方法
*
* @param text 二维码生成规则(二维码可以是任何英文字母加数字生成的二维码)
* @param width 宽度
* @param height 高度
*/
public String getQRCodeImage(String text, int width, int height) throws WriterException, IOException {
//生成二维码类
QRCodeWriter qrCodeWriter = new QRCodeWriter();
//生成的二维码
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
//如果要把二维码在前台显示的话需要转成Base64
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", baos);
byte[] bytes = baos.toByteArray();
String encode = Base64.getEncoder().encodeToString(bytes);
return encode;
}
public static void main(String[] args) {
try {
QRcodeUtil qRcodeUtil = new QRcodeUtil();
String dd = qRcodeUtil.getQRCodeImage("Xiaojie wants to eat eggs", 350, 350);
System.out.println(dd);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 把String自带的UUID去除下划线返回
*
* @return String UUID
*/
public String getUUID() {
String s = UUID.randomUUID().toString();
//去掉“-”符号
return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
}
}
输出生成后是一串大概这样的字符串:
iVBORw0KGgoAAAANSUhEUgAAAV4AAAFeAQAAAADlUEq3AAABYklEQVR42u3aUQ6DIAyAYRIP4JG4ukfyACTdoC3gMt0eS/L7YGR+PnVCW0zy/1ESGAwGg8FgcEB8JDvk2EX0qt61YweHx2N4VnduN3fBYXEN7V40yBb93a7Ai+H2/m76G3hB/H5XU5Y58OAl8BiWx7vgoHhKe8p0+pkjgYPgcTSS61tr5KlOAYfBdZX0Kfcd3iwe8+8vLDgc7r+1SNuViEypEDg0bgFt826NtLqsgb9ZYcHBsHZs+vLpd9sT4AVwKzg07fHJ1+oPrUTA0bGSWvRr9+30mI//ATg4nntu7TE92coJDo89u1Gaew/grtsDDoc9SdX+abIZ2J8FR8cf5b/OttcTODQeLTjNc86xkbGBl8CX7WBNgGzK3eR5Qx8cBJd5/3f6GqMlswJeCHvdqJW/3CSu4LBY2+C96Ld8CBwf+/AY3TePOXgFPLaD+yp5+PBhQx8cBfP9MxgMBoPB4AXxCz6/fN+Hq0IOAAAAAElFTkSuQmCC
前端直接img标签输出也不行,需要在img标签的src属性中加上data:image/PNG;base64,
例如:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Img show</title>
</head>
<body>
<img height="350px" height="350px" src="https://img-blog.csdnimg/2022010623010075432.PNG"/>
</body>
</html>
运行出来,完美
有可能您还需要:
SpringBoot发送短信验证码:https://blog.csdn/weixin_43122090/article/details/103555621
SpringBoot发送邮件https://blog.csdn/weixin_43122090/article/details/103560516
如果需要邮箱发送二维码,可参考我Java发送邮件配合此篇文章可实现
更多推荐
Java代码生成二维码
发布评论