java后台生成二維碼


需求:生成的二維碼可以在微信、支付寶、等手機掃碼設備可識別內容!

1.導入maven

<dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.0</version>
 </dependency>

2.1:先舉例生成二維碼保存本地:

/**
* 存放本地固定路徑
*/
private static final String QR_CODE_IMAGE_PATH = "F:\\kedaSoft\\appinterface\\WebRoot\\ycs.png";
private static void generateQRCodeImage(String text, Integer width, Integer 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("測試,保存在本地的二維碼", 350, 350, QR_CODE_IMAGE_PATH);
        } catch (WriterException e) {
            System.out.println("寫入異常 :: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("io異常 IOException :: " + e.getMessage());
        }

    }

如圖所示:找到保存的路徑查看:

2.2:舉例生成二維碼不保存本地使用web瀏覽器訪問:

    /**
     *
     * @param text 掃碼顯示內容
     * @param width 二維碼長度
     * @param height 二維碼高度
     * @return 返回byte數組
     * @throws WriterException 寫入異常
     * @throws IOException io異常
     */
    public static byte[] getQRCodeImage(String text, Integer width, Integer height) throws WriterException, IOException{
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            // 解決中文亂碼
            HashMap<EncodeHintType, Object> hints = new HashMap<>(16);
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            BitMatrix encode = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            MatrixToImageWriter.writeToStream(encode,"PNG",byteArrayOutputStream);
            return byteArrayOutputStream.toByteArray();
    }
    // 這里我是用的jfinal框架,如你用的是spring:加上RequestMaping(“/qrCode”)即可。

public void qrCode(){ HttpServletResponse response = this.getResponse(); //二維碼內的信息 String info = "二維碼內的信息"; try { byte[] qrCode = QrCodeGenerator.getQRCodeImage(info, 360, 360); OutputStream outputStream = response.getOutputStream(); outputStream.write(qrCode); response.setCharacterEncoding("utf-8"); response.setContentType("image/png"); outputStream.flush(); } catch (WriterException e) { System.out.println("寫入異常 :: " + e.getMessage()); } catch (IOException e) { System.out.println("io異常 IOException :: " + e.getMessage()); } }

 

 3.:全部放到一個工具類:

package com.wom.utils;

/**
 * @author ycs
 * @date 2022/2/14
 */
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

public class QrCodeGenerator {
    /**
     * 存放本地固定路徑
     */
    private static final String QR_CODE_IMAGE_PATH = "F:\\kedaSoft\\appinterface\\WebRoot\\ycs.png";

    private static void generateQRCodeImage(String text, Integer width, Integer 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);

    }

    /**
     *
     * @param text 掃碼顯示內容
     * @param width 二維碼長度
     * @param height 二維碼高度
     * @return 返回byte數組
     * @throws WriterException 寫入異常
     * @throws IOException io異常
     */
    public static byte[] getQRCodeImage(String text, Integer width, Integer height) throws WriterException, IOException{
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            // 解決中文亂碼
            HashMap<EncodeHintType, Object> hints = new HashMap<>(16);
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            BitMatrix encode = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            MatrixToImageWriter.writeToStream(encode,"PNG",byteArrayOutputStream);
            return byteArrayOutputStream.toByteArray();
    }

    public static void main(String[] args) {
        try {
            generateQRCodeImage("測試,保存在本地的二維碼", 350, 350, QR_CODE_IMAGE_PATH);
        } catch (WriterException e) {
            System.out.println("寫入異常 :: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("io異常 IOException :: " + e.getMessage());
        }

    }


}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM