自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

幾行代碼,輕松帶你實(shí)現(xiàn)Java生成二維碼功能!

開發(fā) 后端
本文介紹了幾行代碼就可以輕松帶你實(shí)現(xiàn)Java生成二維碼功能,一起來看看吧。

 文章目錄

  • 效果圖
  • 步驟
    • maven依賴
    • 工具類
    • service
  • 擴(kuò)展
    • 局域網(wǎng)調(diào)試
    • 線上調(diào)試

效果圖

步驟

maven依賴 

  1. <!--生成二維碼-->  
  2. <dependency>  
  3.   <groupId>com.google.zxing</groupId>  
  4.   <artifactId>javase</artifactId>  
  5.   <version>3.3.0</version>  
  6. </dependency> 

工具類 

  1. package com.bennyrhys.mall.util;  
  2. import com.google.zxing.BarcodeFormat;  
  3. import com.google.zxing.WriterException;  
  4. import com.google.zxing.client.j2se.MatrixToImageWriter;  
  5. import com.google.zxing.common.BitMatrix;  
  6. import com.google.zxing.qrcode.QRCodeWriter;  
  7. import java.io.IOException;  
  8. import java.nio.file.FileSystems;  
  9. import java.nio.file.Path;  
  10. /**  
  11.  * 描述:     生成二維碼工具  
  12.  */  
  13. public class QRCodeGenerator {  
  14.     public static void generateQRCodeImage(String text, int width, int height, String filePath)  
  15.             throws WriterException, IOException { 
  16.          QRCodeWriter qrCodeWriter = new QRCodeWriter();  
  17.         BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);  
  18.         Path path = FileSystems.getDefault().getPath(filePath);  
  19.         MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);  
  20.     }  
  21.     public static void main(String[] args) {  
  22.         try {  
  23.             generateQRCodeImage("Hello World", 350, 350, "E:/JAVA/mall/src/main/resources/images/QRTest.png");  
  24.         } catch (WriterException e) {  
  25.             e.printStackTrace();  
  26.         } catch (IOException e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.     }  

service 

  1. /**  
  2.   * 生成二維碼  
  3.   * 圖片可解析出訪問的支付對應(yīng)訂單號的支付連接  
  4.   * @param orderNo 訂單號  
  5.   * @return 返回圖片地址  
  6.   */  
  7.  @Override  
  8.  public String qrcode(String orderNo) {  
  9.      ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();  
  10.      HttpServletRequest request = attributes.getRequest();  
  11.      String address = ip + ":" + request.getLocalPort();  
  12.      String payUrl = "http://" + address + "/pay?orderNo=" + orderNo;  
  13.      try {  
  14.          QRCodeGenerator.generateQRCodeImage(payUrl, 350, 350, Constant.FILE_UPLOAD_PATH + orderNo + ".png");  
  15.      } catch (WriterException e) {  
  16.          e.printStackTrace();  
  17.      } catch (IOException e) {  
  18.          e.printStackTrace();  
  19.      }  
  20.      String pngAddress = "http://" + address + "/images-dev/" + orderNo + ".png";  
  21.      return pngAddress;  
  22.  } 

擴(kuò)展

局域網(wǎng)調(diào)試

線上調(diào)試

切換ip 

  1. # 指定IP(防止ip轉(zhuǎn)發(fā)獲取的是內(nèi)網(wǎng)ip)   
  2. file.upload.ip=127.0.0.1  

 

責(zé)任編輯:龐桂玉 來源: Java知音
相關(guān)推薦

2024-06-26 08:46:45

2023-11-17 09:07:51

.NET生成二維碼識別二維碼

2023-11-17 15:44:06

C++庫生成二維碼

2015-09-24 09:56:19

.NET二維碼

2013-01-30 12:16:49

二維碼NFC近場通訊

2013-10-29 09:46:05

二維碼

2013-03-27 16:13:17

Android開發(fā)Android二維碼生QR生成

2023-10-31 08:16:16

Go語言二維碼

2023-08-08 14:16:07

二維碼開發(fā)鴻蒙

2020-08-07 14:18:03

二維碼Go語言編程

2014-12-31 15:37:16

二維碼生成掃描

2021-02-08 08:40:00

Zint二維碼開源

2017-02-21 09:17:46

二維碼漏洞

2024-08-16 08:38:48

2011-12-06 16:40:45

二維碼快拍二維碼靈動快拍

2022-03-31 07:31:45

工具二維碼Java

2015-11-09 13:03:47

JavaServlet創(chuàng)建二維碼

2017-11-21 09:00:15

PHP二維碼LOGO圖像

2023-12-25 14:53:36

2012-04-01 09:53:13

二維碼
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號