|
@@ -1,14 +1,22 @@
|
|
|
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.extra.qrcode.QrCodeUtil;
|
|
|
import cn.hutool.extra.qrcode.QrConfig;
|
|
|
+import com.amazonaws.services.dynamodbv2.xspec.S;
|
|
|
+import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.junit.Test;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.util.Date;
|
|
|
|
|
@@ -111,7 +119,8 @@ public class QrCodeUtils {
|
|
|
savePath = savePath + "/qrCode.jpg";
|
|
|
// log.info("savePath: {}", savePath);
|
|
|
// 创建二维码tpt
|
|
|
- QrCodeUtil.generate(url, QrConfig.create().setImg(logoPath).setWidth(300), FileUtil.file(savePath));
|
|
|
+ File file = scaleImage(logoPath);
|
|
|
+ QrCodeUtil.generate(url, QrConfig.create().setImg(file.getAbsoluteFile()).setErrorCorrection(ErrorCorrectionLevel.H).setRatio(12), FileUtil.file(savePath));
|
|
|
|
|
|
// 二维码上传oss
|
|
|
String ossPath = ossBasePath + id + "/qrCode.jpg";
|
|
@@ -126,27 +135,58 @@ public class QrCodeUtils {
|
|
|
/**
|
|
|
* 附带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;
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String logoPath="C:\\Users\\4DAGE\\Downloads\\20230728_100814888.jpg";
|
|
|
+ String savePath="C:\\Users\\4DAGE\\Downloads\\123123.jpg";
|
|
|
+ File file = scaleImage(logoPath);
|
|
|
+ QrCodeUtil.generate("asdasdasdasd", QrConfig.create().setImg(file.getAbsoluteFile()).setErrorCorrection(ErrorCorrectionLevel.H).setRatio(12), FileUtil.file(savePath));
|
|
|
|
|
|
- String logoPath = FileUtils.getResource() + "img/kankan_icon.png";
|
|
|
- log.info("logoPath: {}", logoPath);
|
|
|
+ }
|
|
|
|
|
|
- // 创建目录
|
|
|
- if (!FileUtil.isDirectory(savePath)) {
|
|
|
- FileUtil.mkdir(savePath);
|
|
|
+ public static File scaleImage(String imagePath) {
|
|
|
+ try {
|
|
|
+ File imageFile = new File(imagePath);
|
|
|
+ BufferedImage inputImage = ImageIO.read(imageFile);
|
|
|
+ BufferedImage outputImage = resizeImage(inputImage, 80, 80);
|
|
|
+ fillTransparent(outputImage, 80, 80);
|
|
|
+ String output=imageFile.getParent()+File.separator+imageFile.getName().split("\\.")[0]+".png";
|
|
|
+ File outputFile = new File(output);
|
|
|
+ ImageIO.write(outputImage, "png", outputFile);
|
|
|
+ System.out.println("Image resized and saved successfully.");
|
|
|
+ return outputFile;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
- savePath = savePath + time;
|
|
|
- log.info("savePath: {}", savePath);
|
|
|
- // 创建二维码tpt
|
|
|
- QrCodeUtil.generate(url, QrConfig.create().setImg(logoPath).setWidth(300), FileUtil.file(savePath));
|
|
|
+ return null;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
|
|
|
+ double aspectRatio = (double) originalImage.getWidth() / (double) originalImage.getHeight();
|
|
|
+ int newWidth = targetWidth;
|
|
|
+ int newHeight = (int) (newWidth / aspectRatio);
|
|
|
+
|
|
|
+ if (newHeight > targetHeight) {
|
|
|
+ newHeight = targetHeight;
|
|
|
+ newWidth = (int) (newHeight * aspectRatio);
|
|
|
+ }
|
|
|
+
|
|
|
+ BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
|
|
|
+ Graphics2D g2d = resizedImage.createGraphics();
|
|
|
+ g2d.drawImage(originalImage, 0, 0, newWidth, newHeight, null);
|
|
|
+ g2d.dispose();
|
|
|
+
|
|
|
+ return resizedImage;
|
|
|
}
|
|
|
|
|
|
+ public static void fillTransparent(BufferedImage image, int targetWidth, int targetHeight) {
|
|
|
+ int offsetX = (targetWidth - image.getWidth()) / 2;
|
|
|
+ int offsetY = (targetHeight - image.getHeight()) / 2;
|
|
|
|
|
|
-}
|
|
|
+ Graphics2D g2d = image.createGraphics();
|
|
|
+ g2d.setColor(new Color(0, 0, 0, 0)); // Transparent color
|
|
|
+ g2d.fillRect(0, 0, targetWidth, targetHeight);
|
|
|
+ g2d.drawImage(image, offsetX, offsetY, null);
|
|
|
+ g2d.dispose();
|
|
|
+ }
|
|
|
+ }
|