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


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页