【Java应用】二维码的生成和读取

一、简述

      我们知道二维码包含很多种类型,比如Data Matrix,Vericode,QR Code等等,而我们日常生活中所见到的最多的应该是QR Code。

      下面就用Java来生成QR Code和读取QR Code做一个简要介绍。

二、生成包含指定内容的QR Code

      在这里我们要借用Google公司名叫zxing的jar包。它是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库。这个包广泛支持常见的各种码格式,包括这里要说的QR码。当然,除此之外,还有其他一些常用的开源包,读者可根据自己的兴趣选择实现。

      创建工程后,首先要做的应该是把zxing包导入项目中,然后开始编码。

package com.BebDong.zxing;   //自定义包名

import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;

import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

//生成二维码
public class CreateQRCode 
{

    public static void main(String[] args) 
    {
        //二维码像素
        int width=300;
        int height=300;

        String contents="CSDN是一个技术交流的好地方";   //内容
        String format="png";   //图片格式

        //二维码相关参数
        HashMap hints=new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");                      //编码格式
        hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);     //纠错等级
        hints.put(EncodeHintType.MARGIN,2);    //边距,即空白处距离

        //生成QR码
        try 
        {
            BitMatrix bitMatrix;
            bitMatrix=new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);  //这里指定生成QR码

            //生成二维码到指定路径
            Path file=new File("E:/ProgrammingWorkspace/Eclipse/Java/QR_Code/QRCode.png").toPath();   //自定义路径
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }

    }

}

三、读取指定QR码内容

package com.BebDong.zxing;    //自定义包名

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 multiFormatReader=new MultiFormatReader();

            //指定二维码路径
            File file=new File("E:/ProgrammingWorkspace/Eclipse/Java/QR_Code/QRCode.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=multiFormatReader.decode(binaryBitmap,hints);

            //输出解析结果
            System.out.println("解析结果:"+result.toString());
            System.out.println("二维码格式:"+result.getBarcodeFormat());
            System.out.println("二维码文本内容:"+result.getText());

        } 
        catch (NotFoundException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }

    }

}

四、简单测试

利用上述给出代码,做一个简单测试,得到如下结果:

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值