jsp mapper.xml mapper service serviceimpl controller util 生成二维码并保存到数据库和文件夹
时间: 2024-05-05 19:21:22 浏览: 113
首先,你需要引入以下依赖:
```
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.1</version>
</dependency>
```
然后,你可以创建一个 `QRCodeUtil` 工具类,包含生成二维码和保存到文件夹和数据库的方法:
```
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Base64;
import javax.imageio.ImageIO;
import org.springframework.util.StringUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class QRCodeUtil {
/**
* 生成二维码并保存到文件夹和数据库
* @param content 二维码内容
* @param path 二维码保存路径
* @param fileName 文件名
* @return 二维码保存路径
* @throws Exception
*/
public static String generateQRCode(String content, String path, String fileName) throws Exception {
if (StringUtils.isEmpty(content) || StringUtils.isEmpty(path) || StringUtils.isEmpty(fileName)) {
return null;
}
File fileDir = new File(path);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
File file = new File(fileDir, fileName);
createQRCode(content, file);
// 将二维码保存到数据库
String base64 = imageToBase64(file);
// 保存到数据库的逻辑
return path + fileName;
}
/**
* 生成二维码
* @param content 二维码内容
* @param file 二维码保存文件
* @throws Exception
*/
private static void createQRCode(String content, File file) throws Exception {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix matrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, 300, 300, getDecodeHintType());
MatrixToImageWriter.writeToFile(matrix, "png", file);
}
/**
* 获取解码类型
* @return
*/
private static java.util.Map<EncodeHintType, Object> getDecodeHintType() {
java.util.Map<EncodeHintType, Object> hints = new java.util.HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.MARGIN, 1);
return hints;
}
/**
* 将图片转换成base64编码
* @param file
* @return
* @throws Exception
*/
private static String imageToBase64(File file) throws Exception {
byte[] bytes = FileUtil.getBytes(file);
return Base64.getEncoder().encodeToString(bytes);
}
}
```
其中,需要引入 `MatrixToImageWriter` 和 `FileUtil` 两个类,可在网络上找到。
在你的 `Controller` 中,可以调用 `QRCodeUtil` 的方法生成并保存二维码:
```
@RestController
@RequestMapping("/qrcode")
public class QRCodeController {
@PostMapping("/generate")
public String generateQRCode(@RequestParam String content) {
try {
String path = "C:/qrcode/";
String fileName = "qrcode.png";
String qrCodePath = QRCodeUtil.generateQRCode(content, path, fileName);
return qrCodePath;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
```
在你的 `Service` 或者 `ServiceImpl` 中,可以将二维码保存到数据库。在 `Mapper.xml` 中,你需要写入保存到数据库的 SQL 语句。
```
<insert id="insertQRCode" parameterType="java.lang.String">
INSERT INTO QRCode(qr_code) VALUES (#{qr_code})
</insert>
```
最后,在 `Service` 或者 `ServiceImpl` 中调用 `insertQRCode` 方法将二维码保存到数据库即可。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)