|
|
@@ -0,0 +1,205 @@
|
|
|
+package com.ydtech.modules.order.utils;
|
|
|
+
|
|
|
+
|
|
|
+import cn.hutool.core.codec.Base64;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.google.zxing.*;
|
|
|
+import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
|
|
|
+import com.google.zxing.client.j2se.MatrixToImageConfig;
|
|
|
+import com.google.zxing.client.j2se.MatrixToImageWriter;
|
|
|
+import com.google.zxing.common.BitMatrix;
|
|
|
+import com.google.zxing.common.HybridBinarizer;
|
|
|
+import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
|
|
+import com.ydtech.exception.SystemException;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Hashtable;
|
|
|
+/**
|
|
|
+ * @version
|
|
|
+ * @author: hxl
|
|
|
+ * @Date: 2024/8/13 17:34
|
|
|
+ * @Description: 生成二维码的工具类型
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class QrCodeGenerator {
|
|
|
+
|
|
|
+
|
|
|
+ private static final String DEFAULT_CHAR_SET = "UTF-8";
|
|
|
+
|
|
|
+ private static final String DEFAULT_FORMAT_NAME = "JPG";
|
|
|
+
|
|
|
+
|
|
|
+ private static String EXCEL_PATH = "excel//";
|
|
|
+
|
|
|
+ private static String UPLOAD_PATH = "/upload//excel//";
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // 二维码宽度
|
|
|
+ private static final int DEFAULT_QR_CODE_WIDTH = 300;
|
|
|
+ // 二维码高度
|
|
|
+ private static final int DEFAULT_QR_CODE_HEIGHT = 300;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建BitMatrix比特矩阵
|
|
|
+ * @Date 2023/09/24 22:29
|
|
|
+ * @Param contents 二维码里的内容
|
|
|
+ * @Param width 二维码宽度
|
|
|
+ * @param height 二维码高度
|
|
|
+ * @return com.google.zxing.common.BitMatrix
|
|
|
+ */
|
|
|
+ public static BitMatrix createBitMatrix(String contents , int width , int height) throws WriterException, IOException {
|
|
|
+ if (ObjectUtil.isNull(width)) {
|
|
|
+ width = DEFAULT_QR_CODE_WIDTH;
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNull(height)) {
|
|
|
+ height = DEFAULT_QR_CODE_HEIGHT;
|
|
|
+ }
|
|
|
+
|
|
|
+ Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
|
|
|
+ hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 纠错等级L,M,Q,H
|
|
|
+ hints.put(EncodeHintType.CHARACTER_SET, DEFAULT_CHAR_SET);// 编码utf-8
|
|
|
+ hints.put(EncodeHintType.MARGIN, 1); // 边距
|
|
|
+
|
|
|
+ // 创建比特矩阵
|
|
|
+ BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
|
|
|
+ BarcodeFormat.QR_CODE, width, height, hints);
|
|
|
+ return bitMatrix;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建二维码,返回字节数组
|
|
|
+ * @Date 2023/09/24 22:30
|
|
|
+ * @Param contents 二维码里的内容
|
|
|
+ * @Param imageFormat 图片后缀名
|
|
|
+ * @Param width 二维码宽度
|
|
|
+ * @param height 二维码高度
|
|
|
+ * @return byte[]
|
|
|
+ */
|
|
|
+ public static byte[] createQrCode(String contents , String imageFormat , int width , int height) throws WriterException, IOException {
|
|
|
+ if (StrUtil.isBlank(imageFormat)){
|
|
|
+ imageFormat = DEFAULT_FORMAT_NAME;
|
|
|
+ }
|
|
|
+ BitMatrix bitMatrix = createBitMatrix(contents , width, height);
|
|
|
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
+ MatrixToImageWriter.writeToStream(bitMatrix, imageFormat, os);
|
|
|
+ return os.toByteArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建二维码,返回base64字符串
|
|
|
+ * @Date 2023/09/24 22:30
|
|
|
+ * @Param contents 二维码里的内容
|
|
|
+ * @Param imageFormat 图片后缀名
|
|
|
+ * @Param width 二维码宽度
|
|
|
+ * @param height 二维码高度
|
|
|
+ * @return byte[]
|
|
|
+ */
|
|
|
+ public static String createQrCodeBase64(String contents , String imageFormat , int width , int height) throws WriterException, IOException {
|
|
|
+ byte[] bytes =createQrCode(contents , imageFormat , width, height);
|
|
|
+ return Base64.encode(bytes);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解码二维码
|
|
|
+ * @Date 2023/09/24 22:32
|
|
|
+ * @Param [image]
|
|
|
+ * @return java.lang.String
|
|
|
+ */
|
|
|
+ public static String decodeQrCode(BufferedImage image) throws Exception {
|
|
|
+ if (image == null) return StrUtil.EMPTY;
|
|
|
+ BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
|
|
|
+ BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
|
|
|
+ Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
|
|
|
+ hints.put(DecodeHintType.CHARACTER_SET, DEFAULT_CHAR_SET);
|
|
|
+ Result result = new MultiFormatReader().decode(bitmap, hints);
|
|
|
+ return result.getText();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换为BufferedImage
|
|
|
+ * @Date 2023/09/24 22:32
|
|
|
+ * @Param [bitMatrix]
|
|
|
+ * @return java.awt.image.BufferedImage
|
|
|
+ */
|
|
|
+ public static BufferedImage toBufferedImage(BitMatrix bitMatrix) throws IOException, WriterException {
|
|
|
+ MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF);
|
|
|
+ BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix, matrixToImageConfig);
|
|
|
+ return bufferedImage;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 给二维码添加logo
|
|
|
+ * @Date 2023/09/24 22:33
|
|
|
+ * @Param [bufferedImage, logoFile]
|
|
|
+ * @return java.awt.image.BufferedImage
|
|
|
+ */
|
|
|
+ public static BufferedImage addQrCodeLogo(BufferedImage bufferedImage, File logoFile) throws IOException {
|
|
|
+ Graphics2D graphics = bufferedImage.createGraphics();
|
|
|
+ int matrixWidth = bufferedImage.getWidth();
|
|
|
+ int matrixHeigh = bufferedImage.getHeight();
|
|
|
+
|
|
|
+ // 读取logo图片文件
|
|
|
+ BufferedImage logo = ImageIO.read(logoFile);
|
|
|
+ int logoWidth = logo.getWidth();
|
|
|
+ int logoHeight = logo.getHeight();
|
|
|
+
|
|
|
+ // 计算logo放置位置
|
|
|
+ int x = bufferedImage.getWidth() / 5*2;
|
|
|
+ int y = bufferedImage.getHeight() / 5*2;
|
|
|
+ int width = matrixWidth / 5;
|
|
|
+ int height = matrixHeigh / 5;
|
|
|
+
|
|
|
+ // 开始绘制图片
|
|
|
+ graphics.drawImage(logo, x, y, width, height, null);
|
|
|
+ graphics.drawRoundRect(x, y, logoWidth, logoHeight, 15, 15);
|
|
|
+ graphics.setStroke(new BasicStroke(5.0F, 1, 1));
|
|
|
+ graphics.setColor(Color.white);
|
|
|
+ graphics.drawRect(x, y, logoWidth, logoHeight);
|
|
|
+
|
|
|
+ graphics.dispose();
|
|
|
+ bufferedImage.flush();
|
|
|
+ return bufferedImage;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @version
|
|
|
+ * @author: hxl
|
|
|
+ * @Date: 2024/8/13 17:55
|
|
|
+ * @Description: 返回二维码图片路径
|
|
|
+ */
|
|
|
+ public static String getQrCode(String orderNo,String subOrderNo,String url,String uploadPath) {
|
|
|
+ BufferedImage bufferedImage =null;
|
|
|
+ String fileName = orderNo+"_"+subOrderNo+"_rwm.jpg";
|
|
|
+ String saveFilePath = uploadPath+"//" + EXCEL_PATH +fileName;
|
|
|
+ String resultPath = UPLOAD_PATH + fileName;
|
|
|
+ try{
|
|
|
+ bufferedImage = toBufferedImage(createBitMatrix(url, 300, 300));
|
|
|
+ ImageIO.write(bufferedImage, "png", new File(saveFilePath));
|
|
|
+ }catch(Exception e){
|
|
|
+ throw new SystemException("转为二维码报错,请重试!!");
|
|
|
+ }
|
|
|
+ return resultPath;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ BufferedImage bufferedImage = toBufferedImage(createBitMatrix("https://dmzstg3-imcs.pingan.com.cn/icore_imcs/auto/new/signEleApply?systemId=PA18_SHOP_MOBILE¶ms=73C9FEF53D064CF255F2CFDBABFACBA12E9D3C96E1047521652744B5EE5C28B352E38F8E938306220557058195C79E89AF73E3E40CE0DA9650036E08E9AB9F3DD151419E90A464BA45B44645DA6B57C0B998A8B06B5F3018E98C0CD52B2B6A5F40DF7E609F4348BC766F3B9B9D5DAF6CA9C7F80B5FCA66CDD007106A15132525CE522993CB02665803073E9C76E7EE6CE595FFF003E071C12CA95F370A48B38679E5789C27D8EC408213D30D0D0F3EC64BB9ADFA486B87D26425D94CA24DF0A50E7D3B676821216B719AFD7DE2577CCDC55CDD515666C920DB9766DF5904FFB481231936893547E8921170245E7B2C7D34CFA95007A70DC175E947135E97A0411C25C7F7441BB0E91D070194868D47F5F76BAEE7BBCE6067A3C32A44D0EC6ED4C145A10AA04C07860A972E7641FA28CF&signData=da41c267ce742cd668f95401121daca2d53dc50f0b5339754d5b9ff75bc308c5", 300, 300));
|
|
|
+ ImageIO.write(bufferedImage, "png", new File("D:/qrcode.jpg"));
|
|
|
+
|
|
|
+ // System.out.println(decodeQrCode(bufferedImage));
|
|
|
+
|
|
|
+ // BufferedImage logoQrCode = addQrCodeLogo(bufferedImage, new File("D://logo.png"));
|
|
|
+ // ImageIO.write(logoQrCode, "png", new File("D:/logoQrcode.jpg"));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|