| | |
| | | package project.user.internal; |
| | | |
| | | import java.io.File; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.HashMap; |
| | |
| | | public String generate(String content,String imgName) { |
| | | String image_uri = "/qr/" + imgName + ".png"; |
| | | |
| | | // 定义文件夹路径 |
| | | Path directoryPath = Paths.get(Constants.IMAGES_DIR + "/qr/"); |
| | | |
| | | // 检查文件夹是否存在,如果不存在则创建文件夹 |
| | | File directory = directoryPath.toFile(); |
| | | if (!directory.exists()) { |
| | | directory.mkdirs(); // 创建文件夹 |
| | | } |
| | | |
| | | try { |
| | | // 生成二维码图像并保存 |
| | | MatrixToImageWriter.writeToPath( |
| | | new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 260, 260, Collections.singletonMap(EncodeHintType.CHARACTER_SET,"UTF-8")), |
| | | "png", |
| | | new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 260, 260, |
| | | Collections.singletonMap(EncodeHintType.CHARACTER_SET, "UTF-8")), |
| | | "PNG", |
| | | new File(Constants.IMAGES_DIR + image_uri).toPath()); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |