? ? ? ? 支付系統(tǒng)必不可少的就是生成二維碼,有時(shí)我們會(huì)需要將支付鏈接轉(zhuǎn)換為二維碼.用戶(hù)通過(guò)移動(dòng)設(shè)備掃描二維碼調(diào)起支付. 該篇文章主要使用的是hutool自帶的二維碼生成功能.
?1. 引入依賴(lài)(hutool 可以按需引入這里就直接使用all了)
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.19</version>
</dependency>
2. hutool的官方已經(jīng)有很詳細(xì)的生成方式,可以滿足大部分的要求,如下
// 生成指定url對(duì)應(yīng)的二維碼到文件,寬和高都是300像素
QrCodeUtil.generate("https://hutool.cn/", 300, 300, FileUtil.file("d:/qrcode.jpg"));
3. 在二維碼上添加文字和圖片,有時(shí)我們會(huì)需要在二維碼上添加一些logo或者提醒文字,這就需要我們引入字體和圖片.因?yàn)轫?xiàng)目一般都是部署在linux系統(tǒng),以centos為例,centos本身是不包含中文字體,所以在本地開(kāi)發(fā)時(shí)是可以正常顯示字體,生成環(huán)境全變成了亂碼.
- ? ? ? ?創(chuàng)建字體和圖片對(duì)象(字體文件可以在網(wǎng)上或者window計(jì)算機(jī)中獲取),這里有個(gè)坑,可以查看我另一篇文章解決Java 自定義字體產(chǎn)生大量+~JF***.tmp文件導(dǎo)致硬盤(pán)爆滿
private static final String FONT_PATH = "simhei.ttf";
private static Font font;
private static BufferedImage read;
static {
ClassPathResource resource = new ClassPathResource("logo_small.png");
try (InputStream inputStream = resource.getInputStream()) {
font = Font.createFont(Font.TRUETYPE_FONT, new File(FONT_PATH)).deriveFont(20F);
read = ImgUtil.read(inputStream);
} catch (FontFormatException | IOException e) {
log.error("獲取字體文件失敗", e);
}
}
- ?生成二維碼
private static String createQRCode(String url) {
// generateFilePath為自定義方法,用于生成二維碼存放路徑,和二維碼線上訪問(wèn)地址
// 主要根據(jù)業(yè)務(wù)編寫(xiě)自己的方法
Dict dict = FileUtils.generateFilePath(FileDirectoryEnum.QRCODE, "jpg");
String imgUrl = dict.getStr("url");
String path = dict.getStr("path");
// 生成二維碼,
//參數(shù)1 轉(zhuǎn)換為二維碼的內(nèi)容
//參數(shù)2 添加二維碼中間的小圖
//參數(shù)3 二維碼存放位置
File code = QrCodeUtil.generate(
url,
QrConfig.create().setImg(read),
FileUtil.file(path)
);
// 在圖片上添加文字,Imgutil為hutool的工具類(lèi)
ImgUtil.pressText(
code,
code,
"謹(jǐn)防被騙", Color.RED,
font,
0,
-60,
1f
);
return imgUrl;
}
上述代碼使用了hutool的ImgUtil工具類(lèi),參數(shù)含義如下
?完整代碼文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-507022.html
private static final String FONT_PATH = "simhei.ttf";
private static Font font;
private static BufferedImage read;
static {
ClassPathResource resource = new ClassPathResource("logo_small.png");
try (InputStream inputStream = resource.getInputStream()) {
font = Font.createFont(Font.TRUETYPE_FONT, new File(FONT_PATH)).deriveFont(20F);
read = ImgUtil.read(inputStream);
} catch (FontFormatException | IOException e) {
log.error("獲取字體文件失敗", e);
}
}
private static String createQRCode(String url) {
Dict dict = FileUtils.generateFilePath(FileDirectoryEnum.QRCODE, "jpg");
String imgUrl = dict.getStr("url");
String path = dict.getStr("path");
File code = QrCodeUtil.generate(
url,
QrConfig.create().setImg(read),
FileUtil.file(path)
);
ImgUtil.pressText(
code,
code,
"謹(jǐn)防被騙", Color.RED,
font,
0,
-60,
1f
);
return imgUrl;
}
?調(diào)用方式文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-507022.html
createQRCode("https://blog.csdn.net/qq_39078783");
到了這里,關(guān)于【支付系統(tǒng)】java springboot 生成二維碼,二維碼中文亂碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!