Java生成二維碼圖片

maven配置jar包前端

<dependency>
	<groupId>com.google.zxing</groupId>
	<artifactId>core</artifactId>
	<version>3.3.3</version>
</dependency>
<dependency>
	<groupId>com.google.zxing</groupId>
	<artifactId>javase</artifactId>
	<version>3.3.3</version>
</dependency>
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.11</version>
</dependency>

CodeUtil.javajava

package com.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 org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 二維碼工具類
 */
public class CodeUtil {

    private static final Logger log = LoggerFactory.getLogger(CNCBPayUtil.class);

    //二維碼寬度
    private static Integer wc = 80;

    //二維碼高度
    private static Integer hc = 80;

    //圖片類型
    private static String imgType = "png";

    /**
     * 設置參數
     * @param w 寬度
     * @param h 高度
     * @param imageType 圖片類型
     */
    public static void setParam(int w, int h, String imageType) {
        wc = w;
        hc = h;
        imgType = imageType;
    }

    /**
     * 圖片base64編碼 前端根據base64編碼直接顯示
     * @param code
     * @return
     */
    public static String imgBase64(String code) {

        String base64 = Base64.encodeBase64String(codeChangeBytes(code));

        if (null != base64) {
            base64 = "data:image/" + imgType + ";base64," + base64;
        }

        return base64;
    }

    /**
     * 將二維碼裝換成字節數組
     * @param code
     * @return
     */
    @SuppressWarnings("unchecked")
    public static byte[] codeChangeBytes(String code) {
        byte[] bytes = null;

        try {

            BitMatrix bitMatrix = getBitMatrix(code);

            ByteArrayOutputStream out = new ByteArrayOutputStream();

            MatrixToImageWriter.writeToStream(bitMatrix, imgType, out);

            bytes = out.toByteArray();

        } catch (IOException e) {
            log.error("二維碼轉換成字節數組失敗: code ---> " + code);
        }

        return bytes;
    }

    /**
     * 生成二維碼
     * @param code
     * @return
     */
    public static BitMatrix getBitMatrix(String code) {

        BitMatrix bitMatrix = null;

        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();

        Map hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        try {
            //BarcodeFormat.QR_CODE表明是生成二維碼仍是條形碼或者其它碼
            bitMatrix = multiFormatWriter.encode(code, BarcodeFormat.QR_CODE, hc, wc, hints);
            bitMatrix = deleteWhite(bitMatrix);
        } catch (WriterException e) {
            log.error("生成二維碼失敗: code ---> " + code);
        }

        return bitMatrix;
    }

    /**
     * 去除二維碼白邊
     * @param matrix
     * @return
     */
    public static BitMatrix deleteWhite(BitMatrix matrix) {
        int[] rec = matrix.getEnclosingRectangle();
        int resWidth = rec[2] + 1;
        int resHeight = rec[3] + 1;
        BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
        resMatrix.clear();

        for(int i = 0; i < resWidth; ++i) {
            for(int j = 0; j < resHeight; ++j) {
                if (matrix.get(i + rec[0], j + rec[1])) {
                    resMatrix.set(i, j);
                }
            }
        }

        return resMatrix;
    }

}
相關文章
相關標籤/搜索