|
@@ -1,14 +1,31 @@
|
|
|
package com.gis.oss.util;
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.img.ImgUtil;
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.lang.UUID;
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.core.util.URLUtil;
|
|
|
import cn.hutool.extra.qrcode.QrCodeUtil;
|
|
|
import cn.hutool.extra.qrcode.QrConfig;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.amazonaws.services.dynamodbv2.xspec.S;
|
|
|
+import com.gis.common.constant.ConfigConstant;
|
|
|
+import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.iherus.codegen.qrcode.SimpleQrcodeGenerator;
|
|
|
import org.junit.Test;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
+import sun.misc.BASE64Encoder;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.util.Date;
|
|
|
|
|
@@ -23,11 +40,16 @@ public class QrCodeUtils {
|
|
|
|
|
|
@Autowired
|
|
|
FileAndOssUtil fileAndOssUtil;
|
|
|
-
|
|
|
+ @Autowired
|
|
|
+ ConfigConstant configConstant;
|
|
|
|
|
|
@Test
|
|
|
public void test(){
|
|
|
- QrCodeUtil.generate("http://ossxiaoan.4dage.com/cms_zhen/image/20201218_142627638.png?123", 300,300, FileUtil.file("F:\\test\\ngin\\cms_wuhu_gov_data\\qrCode\\3.jpg"));
|
|
|
+ try {
|
|
|
+ new SimpleQrcodeGenerator().generate("http://ossxiaoan.4dage.com/cms_zhen/image/20201218_142627638.png?123").toFile("F:\\test\\ngin\\cms_wuhu_gov_data\\qrCode\\3.jpg");
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -49,7 +71,11 @@ public class QrCodeUtils {
|
|
|
savePath = savePath + time;
|
|
|
log.info("savePath: {}", savePath);
|
|
|
// 创建二维码tpt
|
|
|
- QrCodeUtil.generate(url, 300,300, FileUtil.file(savePath));
|
|
|
+ try {
|
|
|
+ new SimpleQrcodeGenerator().generate(url).toFile(savePath);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
@@ -75,8 +101,11 @@ public class QrCodeUtils {
|
|
|
savePath = savePath + "/qrCode.jpg";
|
|
|
// log.info("savePath: {}", savePath);
|
|
|
// 创建二维码tpt
|
|
|
- QrCodeUtil.generate(url, QrConfig.create().setImg(logoPath).setWidth(300), FileUtil.file(savePath));
|
|
|
-
|
|
|
+ try {
|
|
|
+ new SimpleQrcodeGenerator().setLogo(logoPath).generate(url).toFile(savePath);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
// 二维码上传oss
|
|
|
String ossPath = ossBasePath + id + "/qrCode_"+lang+".jpg";
|
|
|
fileAndOssUtil.upload(savePath, ossPath);
|
|
@@ -89,7 +118,6 @@ public class QrCodeUtils {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 自定义logo小图标二维码
|
|
|
* @param url 二维码内容url
|
|
@@ -111,8 +139,11 @@ public class QrCodeUtils {
|
|
|
savePath = savePath + "/qrCode.jpg";
|
|
|
// log.info("savePath: {}", savePath);
|
|
|
// 创建二维码tpt
|
|
|
- QrCodeUtil.generate(url, QrConfig.create().setImg(logoPath).setWidth(300), FileUtil.file(savePath));
|
|
|
-
|
|
|
+ try {
|
|
|
+ new SimpleQrcodeGenerator().setLogo(logoPath).generate(url).toFile(savePath);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
// 二维码上传oss
|
|
|
String ossPath = ossBasePath + id + "/qrCode.jpg";
|
|
|
fileAndOssUtil.upload(savePath, ossPath);
|
|
@@ -121,32 +152,55 @@ public class QrCodeUtils {
|
|
|
|
|
|
return ossUrl;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* 附带logo小图标二维码
|
|
|
*/
|
|
|
- @Test
|
|
|
- public void testGenerateLogoQrCode(){
|
|
|
- String url = "https://www.baidu.com";
|
|
|
- String savePath = "E:\\cache\\";
|
|
|
- // 保存地址
|
|
|
- String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS") + ".jpg";
|
|
|
- String dirType = "qrCode/";
|
|
|
- savePath = savePath + dirType;
|
|
|
-
|
|
|
- String logoPath = FileUtils.getResource() + "img/kankan_icon.png";
|
|
|
- log.info("logoPath: {}", logoPath);
|
|
|
+ public String generateLogoQrCodeBase64(String logo,String text){
|
|
|
+ String serverBasePath=configConstant.serverBasePath;
|
|
|
|
|
|
+ String savePath = serverBasePath +File.separator+"gerQrcode"+File.separator+ IdUtil.getSnowflakeNextId()+File.separator;
|
|
|
// 创建目录
|
|
|
if (!FileUtil.isDirectory(savePath)) {
|
|
|
FileUtil.mkdir(savePath);
|
|
|
}
|
|
|
- savePath = savePath + time;
|
|
|
- log.info("savePath: {}", savePath);
|
|
|
- // 创建二维码tpt
|
|
|
- QrCodeUtil.generate(url, QrConfig.create().setImg(logoPath).setWidth(300), FileUtil.file(savePath));
|
|
|
+ String logoPath = serverBasePath + "img/kankan_icon.png";
|
|
|
+ if (StrUtil.isNotEmpty(logo)){
|
|
|
+ File file = HttpUtil.downloadFileFromUrl(logo, savePath);
|
|
|
+ logoPath=file.getPath();
|
|
|
+ log.info("diy_logoPath{}",logoPath);
|
|
|
+
|
|
|
+ }else {
|
|
|
+ if (!FileUtil.isFile(logoPath)) {
|
|
|
+ InputStream resource = FileUtils.getResource("img/kankan_icon.png");
|
|
|
+ FileUtil.writeFromStream(resource, logoPath);
|
|
|
+ log.info("logo写入完成");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("logoPath: {}", logoPath);
|
|
|
+ BufferedImage test = new SimpleQrcodeGenerator().setLogo(logoPath).generate(text).getImage();
|
|
|
+ System.out.println(test);
|
|
|
+ String png = ImgUtil.toBase64(test, "jpg");
|
|
|
+ png = "data:image/jpg;base64," + png;
|
|
|
+ if (StrUtil.isNotEmpty(logo)&&!logoPath.contains("kankan_icon.png")){
|
|
|
+ boolean del = FileUtil.del(savePath);
|
|
|
+ log.info("删除logo:{}",del);
|
|
|
+ }
|
|
|
+ return png;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 附带logo小图标二维码
|
|
|
+ */
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String logoPath = "C:\\Users\\4DAGE\\Downloads\\img"+File.separator+ IdUtil.getSnowflakeNextId()+File.separator;
|
|
|
+ String logo="https://ossxiaoan.4dage.com/720yun_fd_manage/fd720_gxqoc32gt/vtour/panos/fd720_gxqoc32gt.tiles/thumb.jpg";
|
|
|
+ if (StrUtil.isNotEmpty(logo)) {
|
|
|
+ if (!FileUtil.isDirectory(logoPath)) {
|
|
|
+ FileUtil.mkdir(logoPath);
|
|
|
+ }
|
|
|
+ File file = HttpUtil.downloadFileFromUrl(logo, logoPath);
|
|
|
+ System.out.println(file.getPath());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
-}
|
|
|
+ }
|