二维码的生成与解析并且上传到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,传参}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

令人着迷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值