Google开源工具Zxing 之 解析二维码

1,项目地址:https://github.com/zxing/zxing

2,引入依赖(对应的链接可以下载jar包)

复制代码
    <!--二维码 操作依赖-->
        <!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.3</version>
        </dependency>
复制代码

3,效果

 

 4,util

复制代码
package com.mangoubiubiu.util;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.multi.qrcode.QRCodeMultiReader;
import sun.misc.BASE64Decoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
/**
 * 作用:二维码识别(图片)
 **/
public class QRCodeTools {
    /**
     * 解析二维码解析,此方法是解析Base64格式二维码图片
     * baseStr:base64字符串,data:image/png;base64开头的
     */
    public static String deEncodeByBase64(String baseStr) {
        String content = null;
        BufferedImage image;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] b=null;
        try {
            int i = baseStr.indexOf("data:image/png;base64,");
            baseStr = baseStr.substring(i+"data:image/png;base64,".length());//去掉base64图片的data:image/png;base64,部分才能转换为byte[]

            b = decoder.decodeBuffer(baseStr);//baseStr转byte[]
            ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(b);//byte[] 转BufferedImage
            image = ImageIO.read(byteArrayInputStream);
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
            Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
            Result result = new MultiFormatReader().decode(binaryBitmap, hints);//解码
            System.out.println("图片中内容:  ");
            System.out.println("content: " + result.getText());
            content = result.getText();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotFoundException e) {
            e.printStackTrace();
        }
        return content;
    }
    /**
     * 解析二维码,此方法解析一个路径的二维码图片
     * path:图片路径
     */
    public static String deEncodeByPath(String path) {
        String content = null;
        BufferedImage image;
        try {
            image = ImageIO.read(new File(path));
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
            Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
            Result result = new MultiFormatReader().decode(binaryBitmap, hints);//解码

            System.out.println("图片中内容:  ");
            System.out.println("content: " + result.getText());
            content = result.getText();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotFoundException e) {
            e.printStackTrace();
        }
        return content;
    }

    /**
     * 多张二维码取最后一个
     * @param
     * @return
     * @throws Exception
     */
    public static String decode(String path) throws Exception {
        BufferedImage image;
        image = ImageIO.read(new File(path));
        if (image == null) {
            return null;
        }
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Result result=null;
        Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
        //   hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
        //优化精度
        hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
        //复杂模式,开启PURE_BARCODE模式
        hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
        try{
            result = new MultiFormatReader().decode(bitmap, hints);
        }catch(Exception e){
            e.printStackTrace();
            QRCodeMultiReader qc = new QRCodeMultiReader();//一张图片有多张二维码取最后一个
            Result[] r = qc.decodeMultiple(bitmap, hints);
            if(r!=null && r.length>0){
                String resultStrTemp = r[r.length-1].getText();
                System.out.println("图片信9息:"+resultStrTemp);
                return resultStrTemp;
            }
        }
        String resultStr = result.getText();
     //   System.out.println("图片信息:"+resultStr);
        return resultStr;
    }



    /**
     * 识别多个二维码
     * @param
     * @return
     * @throws Exception
     */
    public static String redeMoreImg(String path) throws Exception {
        BufferedImage image;
        image = ImageIO.read(new File(path));
        if (image == null) {
            return null;
        }
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Result result=null;
        Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
        //   hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
        //优化精度
        hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
        //复杂模式,开启PURE_BARCODE模式
        hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);

            QRCodeMultiReader qc = new QRCodeMultiReader();//一张图片有多张二维码取最后一个

            Result[] r = qc.decodeMultiple(bitmap, hints);


             StringBuilder msg=new StringBuilder();

        if(r!=null && r.length>0){
                for(int i=0;i<r.length;i++){
                    if(i!=r.length-1) {
                        msg.append(r[i].getText()).append("_");
                    }else{
                        msg.append(r[i].getText());
                    }
                }
            }
        return msg.toString();
    }



}
复制代码

 

本文作者:KwFruit

本文链接:https://www.cnblogs.com/mangoubiubiu/p/13557298.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   KwFruit  阅读(611)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起