6 changed files with 117 additions and 5 deletions
@ -0,0 +1,87 @@ |
|||||
|
package com.ccsens.carbasics.util; |
||||
|
|
||||
|
import cn.hutool.core.date.DateUtil; |
||||
|
import com.google.zxing.BarcodeFormat; |
||||
|
import com.google.zxing.EncodeHintType; |
||||
|
import com.google.zxing.WriterException; |
||||
|
import com.google.zxing.client.j2se.MatrixToImageWriter; |
||||
|
import com.google.zxing.common.BitMatrix; |
||||
|
import com.google.zxing.qrcode.QRCodeWriter; |
||||
|
import net.glxn.qrgen.core.image.ImageType; |
||||
|
import net.glxn.qrgen.javase.QRCode; |
||||
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
||||
|
import org.springframework.core.io.DefaultResourceLoader; |
||||
|
import org.springframework.core.io.ResourceLoader; |
||||
|
|
||||
|
import javax.imageio.ImageIO; |
||||
|
import java.awt.*; |
||||
|
import java.awt.image.BufferedImage; |
||||
|
import java.io.*; |
||||
|
import java.net.URL; |
||||
|
import java.nio.file.FileSystems; |
||||
|
import java.nio.file.Path; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
public class ScreeningQrCodeUtil { |
||||
|
|
||||
|
|
||||
|
|
||||
|
public static String getScreeningQrCode(String filePath, String qrCodeValue, String str1,String str2) throws IOException, WriterException { |
||||
|
//生成二维码
|
||||
|
String fileName = "qrCode/" + DateUtil.today() + "/" + System.currentTimeMillis() + ".png"; |
||||
|
QRCodeWriter qrCodeWriter = new QRCodeWriter(); |
||||
|
HashMap<EncodeHintType, Object> hints = new HashMap<>(); |
||||
|
hints.put(EncodeHintType.MARGIN, 1); |
||||
|
hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); |
||||
|
BitMatrix bitMatrix =qrCodeWriter.encode(qrCodeValue, BarcodeFormat.QR_CODE, 180, 180,hints); |
||||
|
File file = new File(filePath, fileName); |
||||
|
if (!file.getParentFile().exists()) { |
||||
|
file.getParentFile().mkdirs(); |
||||
|
} |
||||
|
Path path = FileSystems.getDefault().getPath(filePath + fileName); |
||||
|
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); |
||||
|
//将二维码放在推按模板上
|
||||
|
ResourceLoader resourceLoader = new DefaultResourceLoader(); |
||||
|
InputStream is = resourceLoader.getResource("classpath:templates/screeningQrCodeTemplates.png").getInputStream(); |
||||
|
|
||||
|
BufferedImage bigImage = ImageIO.read(is); |
||||
|
BufferedImage smallImage = ImageIO.read(new FileInputStream(path.toFile())); |
||||
|
int x = (bigImage.getWidth() - smallImage.getWidth()) / 2; |
||||
|
int y = (bigImage.getHeight() - smallImage.getHeight()) / 2 + 20; |
||||
|
|
||||
|
try { |
||||
|
Graphics2D g = bigImage.createGraphics(); |
||||
|
//将二维码放在模板上
|
||||
|
g.drawImage(smallImage, x, y, smallImage.getWidth(), smallImage.getHeight(), null); |
||||
|
|
||||
|
Font font = new Font(null, Font.BOLD, 18); |
||||
|
g.setFont(font); |
||||
|
g.setColor(Color.black); |
||||
|
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
||||
|
FontMetrics fontMetrics = g.getFontMetrics(font); |
||||
|
// 计算出中心点 x 位置
|
||||
|
int centerX = bigImage.getWidth() / 2; |
||||
|
// 文字宽度
|
||||
|
int textWidth1 = fontMetrics.stringWidth(str1); |
||||
|
// 计算出中心点,并且绘制出文字
|
||||
|
g.drawString(str1, centerX - textWidth1 / 2, y + smallImage.getHeight() + 30); |
||||
|
|
||||
|
// 文字宽度
|
||||
|
int textWidth2 = fontMetrics.stringWidth(str2); |
||||
|
// 计算出中心点,并且绘制出文字
|
||||
|
g.drawString(str2, centerX - textWidth2 / 2, y + smallImage.getHeight() + 60); |
||||
|
|
||||
|
g.dispose(); |
||||
|
//往输出流中写数据
|
||||
|
|
||||
|
ImageIO.write(bigImage, "png", new File(filePath + fileName)); |
||||
|
|
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
|
||||
|
return fileName; |
||||
|
} |
||||
|
|
||||
|
} |
@ -1,4 +1,4 @@ |
|||||
spring: |
spring: |
||||
profiles: |
profiles: |
||||
active: pre |
active: test |
||||
include: common, util-pre |
include: common, util-test |
||||
|
After Width: | Height: | Size: 61 KiB |
Loading…
Reference in new issue