|
|
@@ -4,8 +4,12 @@ import cn.hutool.core.codec.Base64;
|
|
|
import cn.hutool.core.img.ImgUtil;
|
|
|
import cn.hutool.extra.qrcode.QrCodeUtil;
|
|
|
import cn.hutool.extra.qrcode.QrConfig;
|
|
|
+import com.fs.common.core.domain.entity.SysDictData;
|
|
|
+import com.fs.common.utils.StringUtils;
|
|
|
+import com.fs.common.utils.spring.SpringUtils;
|
|
|
import com.fs.system.oss.CloudStorageService;
|
|
|
import com.fs.system.oss.OSSFactory;
|
|
|
+import com.fs.system.service.ISysDictTypeService;
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
import java.awt.*;
|
|
|
@@ -16,8 +20,19 @@ import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
+import java.util.List;
|
|
|
|
|
|
public class QRutils {
|
|
|
+
|
|
|
+ private static final String QR_CODE_EMBED_POSITION = "qr_code_embed_position";
|
|
|
+ /** 字典 dict_sort=1:是否开启二维码字典定位 */
|
|
|
+ private static final long DICT_SORT_QR_POSITION_ENABLED = 1L;
|
|
|
+ /** 字典 dict_sort=2:二维码X坐标 */
|
|
|
+ private static final long DICT_SORT_QR_POSITION_X = 2L;
|
|
|
+ /** 字典 dict_sort=3:二维码Y坐标 */
|
|
|
+ private static final long DICT_SORT_QR_POSITION_Y = 3L;
|
|
|
+ private static final int DEFAULT_QR_CODE_X = 250;
|
|
|
+ private static final int DEFAULT_QR_CODE_Y = 1160;
|
|
|
public static InputStream downloadAndEncodeImageToInputStream(String imageUrl) throws Exception {
|
|
|
URL url = new URL(imageUrl);
|
|
|
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
|
|
|
@@ -65,14 +80,9 @@ public class QRutils {
|
|
|
// 绘制主图
|
|
|
gMain.drawImage(mainImage, 0, 0, null);
|
|
|
|
|
|
- // 在指定位置绘制二维码图片
|
|
|
-// g.drawImage(qrCodeImage, qrCodeX, qrCodeY, null);
|
|
|
- // 计算二维码图片的放置位置(距主图左边宽10%,距离上边宽10%)
|
|
|
-// int qrCodeX = (int)Math.round(mainImage.getWidth() * 0.1); // 左边距1%
|
|
|
-// int qrCodeY = (int)Math.round(mainImage.getHeight() * 0.1); // 上边距1%
|
|
|
- int qrCodeX = (int)Math.round(250.0); // 左边距1%
|
|
|
- int qrCodeY = (int)Math.round(1160.0); // 上边距1%
|
|
|
- // 在指定位置绘制二维码图片
|
|
|
+ int[] qrPosition = resolveQrCodePosition();
|
|
|
+ int qrCodeX = qrPosition[0];
|
|
|
+ int qrCodeY = qrPosition[1];
|
|
|
gMain.drawImage(resizedQrCodeImage, qrCodeX, qrCodeY, null);
|
|
|
|
|
|
// 释放图形上下文使用的系统资源
|
|
|
@@ -91,6 +101,69 @@ public class QRutils {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 解析二维码在主图上的坐标:默认 250,1160;字典开启时使用 dict_sort=2/3 的 x/y。
|
|
|
+ */
|
|
|
+ private static int[] resolveQrCodePosition() {
|
|
|
+ if (!isQrCodePositionEnabled()) {
|
|
|
+ return new int[]{DEFAULT_QR_CODE_X, DEFAULT_QR_CODE_Y};
|
|
|
+ }
|
|
|
+ return new int[]{
|
|
|
+ parseQrCodeCoordinate(getQrCodeEmbedDictValue(DICT_SORT_QR_POSITION_X), DEFAULT_QR_CODE_X),
|
|
|
+ parseQrCodeCoordinate(getQrCodeEmbedDictValue(DICT_SORT_QR_POSITION_Y), DEFAULT_QR_CODE_Y)
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isQrCodePositionEnabled() {
|
|
|
+ List<SysDictData> dictList = getQrCodeEmbedDictList();
|
|
|
+ if (dictList == null || dictList.isEmpty()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return dictList.stream()
|
|
|
+ .filter(item -> "0".equals(item.getStatus()))
|
|
|
+ .filter(item -> item.getDictSort() != null && DICT_SORT_QR_POSITION_ENABLED == item.getDictSort())
|
|
|
+ .anyMatch(item -> "1".equals(StringUtils.trimToEmpty(item.getDictValue())));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getQrCodeEmbedDictValue(long dictSort) {
|
|
|
+ List<SysDictData> dictList = getQrCodeEmbedDictList();
|
|
|
+ if (dictList == null || dictList.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ for (SysDictData item : dictList) {
|
|
|
+ if (!"0".equals(item.getStatus())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (item.getDictSort() != null && dictSort == item.getDictSort()) {
|
|
|
+ return StringUtils.trimToEmpty(item.getDictValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static List<SysDictData> getQrCodeEmbedDictList() {
|
|
|
+ try {
|
|
|
+ ISysDictTypeService sysDictTypeService = SpringUtils.getBean(ISysDictTypeService.class);
|
|
|
+ if (sysDictTypeService == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return sysDictTypeService.selectDictDataByType(QR_CODE_EMBED_POSITION);
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int parseQrCodeCoordinate(String value, int defaultValue) {
|
|
|
+ if (StringUtils.isBlank(value)) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return (int) Math.round(Double.parseDouble(value.trim()));
|
|
|
+ } catch (NumberFormatException ignored) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 生成二维码图片
|