|
@@ -1,128 +0,0 @@
|
|
|
-package com.rongwei.business.common.utils;
|
|
|
-
|
|
|
-import cn.hutool.core.date.DateUtil;
|
|
|
-import com.google.zxing.BarcodeFormat;
|
|
|
-import com.google.zxing.EncodeHintType;
|
|
|
-import com.google.zxing.MultiFormatWriter;
|
|
|
-import com.google.zxing.WriterException;
|
|
|
-import com.google.zxing.client.j2se.MatrixToImageWriter;
|
|
|
-import com.google.zxing.common.BitMatrix;
|
|
|
-import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
|
|
-import com.rongwei.rwcommon.utils.Constants;
|
|
|
-import com.rongwei.rwcommon.utils.SecurityUtil;
|
|
|
-import com.rongwei.rwcommonentity.commonservers.domain.SysFileFolderDo;
|
|
|
-import com.rongwei.rwcommonentity.commonservers.domain.SysFileItemDo;
|
|
|
-
|
|
|
-import javax.crypto.Cipher;
|
|
|
-import javax.crypto.SecretKey;
|
|
|
-import javax.crypto.SecretKeyFactory;
|
|
|
-import javax.crypto.spec.DESKeySpec;
|
|
|
-import javax.crypto.spec.IvParameterSpec;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.UUID;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author shangmi
|
|
|
- */
|
|
|
-public class QrCodeGeneratorUtils {
|
|
|
- /**
|
|
|
- * 文件上传的保存路径
|
|
|
- */
|
|
|
- public static String UPLOAD_PATH = System.getProperty("user.dir") + File.separator + "upload" + File.separator;
|
|
|
-
|
|
|
- /**
|
|
|
- * 生成二维码,返回相对路径
|
|
|
- *
|
|
|
- * @param text
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String generateQRCodeImage(String text) {
|
|
|
- HashMap hashMap = new HashMap(16);
|
|
|
- // 设置二维码字符编码
|
|
|
- hashMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
|
|
|
- // 设置二维码纠错等级
|
|
|
- hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
|
|
|
- // 设置二维码边距
|
|
|
- hashMap.put(EncodeHintType.MARGIN, 1);
|
|
|
- try {
|
|
|
- // 开始生成二维码
|
|
|
- BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, 300, 300, hashMap);
|
|
|
- SysFileItemDo sysFileItem = new SysFileItemDo();
|
|
|
- String fileId = SecurityUtil.getUUID();
|
|
|
- String fileUrl = "/files/";;
|
|
|
- String filePath = sysConfigService.getContentByConfigCode(Constants.CONFIG_FILE_ROOT_PATH);
|
|
|
- sysFileItem.setExtends1("files");
|
|
|
- SysFileFolderDo sysFileFolderDo = sysFileFolderService.getFolderByFoldercode(foldercode);
|
|
|
- String moduleName = sysFileFolderDo.getFullfolderpath();
|
|
|
- sysFileItem.setFilefolderid(sysFileFolderDo.getId());
|
|
|
- String fileName = UUID.randomUUID() + ".png";
|
|
|
- fileUrl = fileUrl + fileName.substring(fileName.lastIndexOf(".") + 1);
|
|
|
-
|
|
|
- sysFileItem.setFilename(fileName);
|
|
|
-
|
|
|
- sysFileItem.setFiletype(fileName.substring(fileName.lastIndexOf(".") + 1));
|
|
|
- sysFileItem.setStoragetype("1");
|
|
|
- sysFileItem.setFullpath(filePath + fileName);
|
|
|
- sysFileItem.setUrlpath(fileUrl);
|
|
|
- sysFileItem.setRelationid("0");
|
|
|
- sysFileItem.setId(fileId);
|
|
|
- sysFileItem.setCreatedate(new Date());
|
|
|
- sysFileItem.setModifydate(new Date());
|
|
|
-
|
|
|
- File targetFile = new File(filePath);
|
|
|
- if (!targetFile.exists()) {
|
|
|
- targetFile.mkdirs();
|
|
|
- }
|
|
|
- File file = new File(filePath + fileName);
|
|
|
- sysFileItem.setFilesize(file.length());
|
|
|
- // 导出到指定目录
|
|
|
- MatrixToImageWriter.writeToPath(bitMatrix, "png", file.toPath());
|
|
|
-
|
|
|
- return sysFileItem.getUrlpath();
|
|
|
- } catch (WriterException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static byte[] DES_CBC_Encrypt(byte[] content, byte[] keyBytes) {
|
|
|
- try {
|
|
|
- DESKeySpec keySpec = new DESKeySpec(keyBytes);
|
|
|
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
|
|
- SecretKey key = keyFactory.generateSecret(keySpec);
|
|
|
- Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
|
|
|
- cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(keySpec.getKey()));
|
|
|
- byte[] result = cipher.doFinal(content);
|
|
|
- return result;
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println("exception:" + e.toString());
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- public static String byteToHexString(byte[] bytes) {
|
|
|
- StringBuffer sb = new StringBuffer(bytes.length);
|
|
|
- String sTemp;
|
|
|
- for (int i = 0; i < bytes.length; i++) {
|
|
|
- sTemp = Integer.toHexString(0xFF & bytes[i]);
|
|
|
- if (sTemp.length() < 2) {
|
|
|
- sb.append(0);
|
|
|
- }
|
|
|
- sb.append(sTemp.toUpperCase());
|
|
|
- }
|
|
|
- return sb.toString();
|
|
|
- }
|
|
|
-
|
|
|
- public static void main(String[] args) {
|
|
|
- String text = "https://www.baidu.com";
|
|
|
- String s = generateQRCodeImage(text);
|
|
|
- System.out.println(s);
|
|
|
- }
|
|
|
-}
|