新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
幾行代碼,輕松帶你實現(xiàn)Java生成二維碼功能!
文章目錄

網(wǎng)站建設哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、成都微信小程序、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了觀山湖免費建站歡迎大家使用!
- 效果圖
- 步驟
- maven依賴
- 工具類
- service
- 擴展
- 局域網(wǎng)調試
- 線上調試
效果圖
步驟
maven依賴
com.google.zxing javase 3.3.0
工具類
- package com.bennyrhys.mall.util;
- import com.google.zxing.BarcodeFormat;
- import com.google.zxing.WriterException;
- import com.google.zxing.client.j2se.MatrixToImageWriter;
- import com.google.zxing.common.BitMatrix;
- import com.google.zxing.qrcode.QRCodeWriter;
- import java.io.IOException;
- import java.nio.file.FileSystems;
- import java.nio.file.Path;
- /**
- * 描述: 生成二維碼工具
- */
- public class QRCodeGenerator {
- public static void generateQRCodeImage(String text, int width, int height, String filePath)
- throws WriterException, IOException {
- QRCodeWriter qrCodeWriter = new QRCodeWriter();
- BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
- Path path = FileSystems.getDefault().getPath(filePath);
- MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
- }
- public static void main(String[] args) {
- try {
- generateQRCodeImage("Hello World", 350, 350, "E:/JAVA/mall/src/main/resources/images/QRTest.png");
- } catch (WriterException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
service
- /**
- * 生成二維碼
- * 圖片可解析出訪問的支付對應訂單號的支付連接
- * @param orderNo 訂單號
- * @return 返回圖片地址
- */
- @Override
- public String qrcode(String orderNo) {
- ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
- HttpServletRequest request = attributes.getRequest();
- String address = ip + ":" + request.getLocalPort();
- String payUrl = "http://" + address + "/pay?orderNo=" + orderNo;
- try {
- QRCodeGenerator.generateQRCodeImage(payUrl, 350, 350, Constant.FILE_UPLOAD_PATH + orderNo + ".png");
- } catch (WriterException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- String pngAddress = "http://" + address + "/images-dev/" + orderNo + ".png";
- return pngAddress;
- }
擴展
局域網(wǎng)調試
線上調試
切換ip
- # 指定IP(防止ip轉發(fā)獲取的是內網(wǎng)ip)
- file.upload.ip=127.0.0.1
當前名稱:幾行代碼,輕松帶你實現(xiàn)Java生成二維碼功能!
網(wǎng)頁鏈接:http://www.5511xx.com/article/cddjcgg.html


咨詢
建站咨詢
