|
@@ -8,6 +8,7 @@ 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.iherus.codegen.qrcode.SimpleQrcodeGenerator;
|
|
|
import org.junit.Test;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
@@ -35,7 +36,11 @@ public class QrCodeUtils {
|
|
|
|
|
|
@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);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -57,7 +62,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);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
@@ -83,8 +92,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);
|
|
@@ -119,9 +131,11 @@ public class QrCodeUtils {
|
|
|
savePath = savePath + "/qrCode.jpg";
|
|
|
// log.info("savePath: {}", savePath);
|
|
|
// 创建二维码tpt
|
|
|
- File file = scaleImage(logoPath);
|
|
|
- QrCodeUtil.generate(url, QrConfig.create().setImg(file.getAbsoluteFile()).setErrorCorrection(ErrorCorrectionLevel.H).setRatio(12), 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);
|
|
@@ -138,55 +152,12 @@ public class QrCodeUtils {
|
|
|
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));
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- 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;
|
|
|
+ new SimpleQrcodeGenerator().setLogo(logoPath).generate("test").toFile(savePath);
|
|
|
} catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- 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);
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
-
|
|
|
- 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();
|
|
|
- }
|
|
|
}
|