基於ZXing的二維碼生成工具類

package com.study.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @date 2020/5/8
 */
public class ORCodeUtils {

    private static final int width = 300;// 默認二維碼寬度
    private static final int height = 300;// 默認二維碼高度
    private static final String format = "png";// 默認二維碼文件格式
    private static final Map<EncodeHintType, Object> hints = new HashMap<>();// 二維碼參數

    static {
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 字符編碼
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 容錯等級 L、M、Q、H 其中 L 爲最低, H 爲最高
        hints.put(EncodeHintType.MARGIN, 2);// 二維碼與圖片邊距
    }
    /**
     * 返回一個 BufferedImage 對象
     * @param content 二維碼內容
     * @param width   寬
     * @param height  高
     */
    public static BufferedImage toBufferedImage(String content, int width, int height) throws WriterException, IOException {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        return MatrixToImageWriter.toBufferedImage(bitMatrix);
    }
    /**
     * 將二維碼圖片輸出到一個流中
     * @param content 二維碼內容
     * @param stream  輸出流
     * @param width   寬
     * @param height  高
     */
    public static void writeToStream(String content, OutputStream stream, int width, int height) throws WriterException, IOException {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        MatrixToImageWriter.writeToStream(bitMatrix, format, stream);
    }
    /**
     * 生成二維碼圖片文件
     * @param content 二維碼內容
     * @param path    文件保存路徑
     * @param width   寬
     * @param height  高
     */
    public static void createQRCode(String content, String path, int width, int height) throws WriterException, IOException {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        //toPath() 方法由 jdk1.7 及以上提供
        MatrixToImageWriter.writeToPath(bitMatrix, format, new File(path).toPath());
    }

 /**
     *  解析二維碼內容
     * @param file 二維碼
     * @return 二維碼包含的信息
     * @throws Exception
     */
    public static String parseQRCode(File file) throws Exception {
        BufferedImage image;
        image = ImageIO.read(file);
        if (image == null) {
            return null;
        }
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Map<DecodeHintType, Object> hints = new HashMap<>(2);
        hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
        Result result = new MultiFormatReader().decode(bitmap, hints);
        return result.getText();
    }
}

maven依賴:

  <!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.4.0</version>
        </dependency>

 

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