原创

JAVA利用google的zxing解析二维码QRCODE

1.导入jar包,如果是非maven工程就去mvnrepository.com搜索zxing,下载本jar包即可

<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>


2.写上下面代码即可,可创建一个main方法来测试,下面标识出来了对应的包

import com.google.zxing.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class GenerateQRCode {

    public static void main(String[] args) {
        readQRCode();
    }

    public static void readQRCode(){
        MultiFormatReader formatReader = new MultiFormatReader();

        File file = new File("c:/code/img.png");
        try {
            Map map = new HashMap();
            map.put(EncodeHintType.CHARACTER_SET,"utf-8");
            BufferedImage image = ImageIO.read(file);

            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));

            Result decode = formatReader.decode(binaryBitmap,map);
            System.err.println(decode.toString());
            System.err.println(decode.getBarcodeFormat());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

运行main方法即可运行!

正文到此结束
本文目录