欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

二维码(第一弹:使用ZXing方式实现二维码),zxing方式

来源: javaer 分享于  点击 19715 次 点评:60

二维码(第一弹:使用ZXing方式实现二维码),zxing方式


准备工作

可以到https://github.com/zxing 下载完整的project源码,目前最新的版本是3.3.0。然后可以把源码里面的相关的类拷到自己的项目里面直接用,也可以把源码里面的core包和javase包拎出来打成jar包来使用。
不过这里我推荐使用Maven来管理,可以到http://mvnrepository.org/ 下载相关的依赖:
搜索ZXing,找到ZXing Core,我们使用目前最新的版本3.3.0,依赖如下:

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

然后找到ZXing Java SE Extensions,同样使用目前最新的版本3.3.0,依赖如下:

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

代码示例

我们新建一个工具类QRCodeUtil.java,用来生成和解析二维码。我们写一个encode方法来生成二维码,再写一个decode方法来解析二维码,最后用main方法测试一下。代码写的不是很严谨,仅做示例。代码如下:

package com.demo.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
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 com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QRCodeUtil {

    public static void encode() throws Exception {
        // 二维码图片的宽度
        int width = 300;
        // 二维码图片的高度
        int height = 300;
        // 二维码图片的格式
        String format = "png";
        // 二维码的内容
        String contents = "www.baidu.com";

        // 二维码的参数
        HashMap<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        // 字符集
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 纠错等级
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN, 2);

        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);

        // 二维码图片的生成路径
        Path path = new File("D:/img.png").toPath();

        MatrixToImageWriter.writeToPath(bitMatrix, format, path);
    }

    public static void decode() throws Exception {
        // 二维码的参数
        HashMap<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
        // 字符集
        hints.put(DecodeHintType.CHARACTER_SET, "utf-8");

        // 获取二维码图片文件
        File file = new File("D:/img.png");
        BufferedImage image = ImageIO.read(file);
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));

        Result result = new MultiFormatReader().decode(binaryBitmap, hints);
        System.out.println(result.getBarcodeFormat());
        System.out.println(result.getText());
    }

    public static void main(String[] args) throws Exception {
        QRCodeUtil.encode();
        QRCodeUtil.decode();
    }

}

相关文章

    暂无相关文章

用户点评