二維碼的生成與解析並且上傳到OSS

1.導入依賴

 

<dependency>           

<groupId>QRCode</groupId>

            <artifactId>QRCode</artifactId>

            <version>3.3.3</version>

        </dependency>

        <dependency>

            <groupId>com.google.zxing</groupId>

            <artifactId>javase</artifactId>

            <version>3.3.1</version>

        </dependency>

2.引入工具類

/**
 * 生成二維碼
 *
 * @return
 */
public static String encodeQRCode(String text, String path) {
   int width = 150;
   int height = 150;
   String format = "png";
   //定義二維碼的參數
   HashMap hints = new HashMap();
   hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
   hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
   hints.put(EncodeHintType.MARGIN, 2);
   //生成二維碼

   BitMatrix bitMatrix = null;
   try {
      bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
   } catch (WriterException e) {
      e.printStackTrace();
   }
   Path file = new File(path).toPath();
   try {
      MatrixToImageWriter.writeToPath(bitMatrix, format, file);
   } catch (IOException e) {
      e.printStackTrace();
   }
   return file.toString();
}

/**
 * 對二維碼圖片進行解碼
 *
 * @param filePath 二維碼路徑
 * @return 解碼後對內容
 */
public static String decodeQRCode(String filePath) {
   MultiFormatReader formatReader = new MultiFormatReader();
   File file = new File(filePath);
   BufferedImage image = null;
   try {
      image = ImageIO.read(file);
   } catch (IOException e) {
      e.printStackTrace();
   }
   BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));

   //定義二維碼的參數
   HashMap hints = new HashMap();
   hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

   //爲了解決com.google.zxing.NotFoundException報錯問題
   hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);

   //結果展示
   Result result = null;
   try {
      result = formatReader.decode(binaryBitmap, hints);
   } catch (NotFoundException e) {
      e.printStackTrace();
   }
   return result.getText();
}

3.測試controller

@Api("二維碼")
@RestController
@RequestMapping(value = "/test/qrCode")
public class QrCodeController {

   @Value("${oss.endPoint}")
   private String endpoint;

   @Value("${oss.accessKeyId}")
   private String accessKeyId;

   @Value("${oss.accessKeySecret}")
   private String accessKeySecret;

   @Value("${oss.bucketName}")
   private String bucketName;

   @Value("${oss.url}")
   private String url;

   @Value("${oss.qrCodeUrl}")
   private String qrCodeUrl;

   @Value("${oss.qrCodeAbsolutePath}")
   private String qrCodeAbsolutePath;


   @ControllerEndpoint(operation = "qrCode", exceptionMessage = "qrCode")
   @ApiOperation("")
   @PostMapping("/qr")
   public String qrCode() {

      User user = (User) SecurityUtils.getSubject().getPrincipal();
      //生成二維碼                 //內容+二維碼保存路徑
      String image = QRCodeUtils.encodeQRCode((user.getUsername()) + "," + "傳參", qrCodeAbsolutePath);
      //解析二維碼
      String qrCodeResult = QRCodeUtils.decodeQRCode(qrCodeAbsolutePath);

      //獲取真實的文件名 去掉後綴
      String fileName = UploadUtils.subFileName(image);
      //生成的文件名
      String uuidFileName = UploadUtils.generateRandonFileName(fileName);
      String dir = "XXXX";
      // 上傳文件流。
      String key = "file/" + dir + "/" + uuidFileName;
      //創建OSSClient實例。
      OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
      // 上傳文件。
      ossClient.putObject(bucketName, key, new File(image));
      // 關閉OSSClient。
      String qrCode = qrCodeUrl + key;
      ossClient.shutdown();

      HashMap map = new HashMap();
      map.put("qrCode", qrCode);
      map.put("qrCodeResult", qrCodeResult);

      return map.toString();

   }

4 .生成的二維碼

 

5.解析的數據

Response body

{qrCode=https://XXXX.com/file/XXX/a54ccffeadf540c1ab58e225642f41ae.jpg, qrCodeResult=XXX,傳參}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章