继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Java技术实现解析读取二维码

霜花似雪
关注TA
已关注
手记 163
粉丝 1.5万
获赞 8507

此文章接上一篇:http://www.imooc.com/article/15617
Java实现读取解析二维码图片的内容两种实现方法:
方法一:

package com.imooc;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

//解析读取二维码图片信息
public class ReadQRCode {

    public static void main(String[] args) {
        try {
            MultiFormatReader formatReader = new MultiFormatReader();
            // 二维码图片位置
            File file = new File("f:/code2/Eo.png");
            // 读取图片
            BufferedImage image = ImageIO.read(file);
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));

            // 定义二维码的参数
            HashMap hints = new HashMap();
            //设置编码字符集
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

            //处理读取结果
            Result result = formatReader.decode(binaryBitmap, hints);

            System.out.println("解析结果:" + result.toString());
            System.out.println("二维码格式类型:" + result.getBarcodeFormat());
            System.out.println("二维码文本内容:" + result.getText());
        } catch (NotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

方法二:

package com.imooc;

import java.awt.image.BufferedImage;

import jp.sourceforge.qrcode.data.QRCodeImage;

public class MyQRCodeImage implements QRCodeImage {
    BufferedImage bufferedImage;

    public MyQRCodeImage(BufferedImage bufferedImage){
        this.bufferedImage=bufferedImage;
    }

    @Override
    public int getHeight() {
        return bufferedImage.getHeight();
    }

    @Override
    public int getPixel(int arg0, int arg1) {
        return bufferedImage.getRGB(arg0, arg1);
    }

    @Override
    public int getWidth() {
        return bufferedImage.getWidth();
    }

}
package com.imooc;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import jp.sourceforge.qrcode.QRCodeDecoder;

public class ReadQRcode1 {

    public static void main(String[] args) throws Exception {
        //指定文件路径
        File file=new File("E:/code/qrcode.png");
        //读取验证码图片
        BufferedImage bufferedImage=ImageIO.read(file);
        //调用方法
        QRCodeDecoder codeDecoder=new QRCodeDecoder();
        String result=new String(codeDecoder.decode(new MyQRCodeImage(bufferedImage)),"gb2312");

        System.out.println("二维码的内容为:"+result);

    }

}

项目目录结构:
图片描述

打开App,阅读手记
6人推荐
发表评论
随时随地看视频慕课网APP