1. 程式人生 > >java二維碼工具類:生成二維碼和解析二維碼

java二維碼工具類:生成二維碼和解析二維碼

利用google 的 zxing 生成和解析二維碼。

1、下載或maven加入依賴,我這選用的是3.3.0版本,

jar下載地址:

http://mvnrepository.com/artifact/com.google.zxing/core/3.3.0

http://mvnrepository.com/artifact/com.google.zxing/javase/3.3.0

下載core 和 javase 這兩個jar 包,匯入工程,即可。

如果用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>


工具類:

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Path;
import java.util.HashMap;

/**
 * 二維碼工具類
 * Created by Saindy on 2017-8-26.
 */
public class QrCodeUtils {

    static String QRCODE_IMG_PATH = "d:/opt/qrCode/"; // 存放二維碼的資料夾

    /**
     * 生成二維碼
     * @param content 要生成的二維碼內容
     * @param fileName 不帶副檔名的檔名
     * @return 返回以.png格式的檔案的絕對路徑
     */
    public static String createQrCode(String content, String fileName){
        String qrCodeFilePath = "";
        try {
            int qrCodeWidth = 300;
            int qrCodeHeight = 300;
            String qrCodeFormat = "png";
            HashMap<EncodeHintType, String> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, qrCodeWidth, qrCodeHeight, hints);

            BufferedImage image = new BufferedImage(qrCodeWidth, qrCodeHeight, BufferedImage.TYPE_INT_RGB);
            File qrCodeFile = new File(QRCODE_IMG_PATH + fileName +"." + qrCodeFormat);
            ImageIO.write(image, qrCodeFormat, qrCodeFile);
//            MatrixToImageWriter.writeToFile(bitMatrix, qrCodeFormat, qrCodeFile);
            Path path = qrCodeFile.toPath();
            MatrixToImageWriter.writeToPath(bitMatrix, qrCodeFormat, path);
            qrCodeFilePath = qrCodeFile.getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return qrCodeFilePath;
    }

    /**
     * 解析二維碼
     * @param filePath 要進行解析的二維碼圖片地址
     * @return 解析後得到的文字
     */

    public static String decodeQrCode(String filePath) {
        String retStr = "";
        if ("".equalsIgnoreCase(filePath) && filePath.length() == 0) {
            return "圖片路徑為空!";
        }
        try {
            BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filePath));
            LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap bitmap = new BinaryBitmap(binarizer);
            HashMap<DecodeHintType, Object> hintTypeObjectHashMap = new HashMap<>();
            hintTypeObjectHashMap.put(DecodeHintType.CHARACTER_SET, "UTF-8");
            Result result = new MultiFormatReader().decode(bitmap, hintTypeObjectHashMap);
            retStr = result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retStr;
    }

    public static void main(String[] args) {
        createQrCode("http://blog.csdn.net/Saindy5828", "1610432809");
        String str = decodeQrCode(QRCODE_IMG_PATH + "1610432809.png");
        System.out.println("解析二維碼得到的內容:"+str);
    }
}


生成的二維碼:

解析得到的字串:

解析二維碼得到的內容:http://blog.csdn.net/Saindy5828