博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
整理关于java实现二维码的生成和解析代码供大家参考
阅读量:4293 次
发布时间:2019-05-27

本文共 2611 字,大约阅读时间需要 8 分钟。

淮安二傻子 2017-09-26 21:39

1zxing介绍

二维码的生成与解析。有多种途径。我们选择使用zxing,ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码。

2zxing下载

Jar下载路径:

https://codeload.github.com/zxing/zxing/zip/master

整理关于java实现二维码的生成和解析代码供大家参考

3在项目中应用

3.1首先在项目里面引入zxing jar

整理关于java实现二维码的生成和解析代码供大家参考

3.2新建TestZXing类,编写产生二维码代码

package test;

import java.io.File;

import java.io.IOException;

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.WriterException;

import com.google.zxing.client.j2se.MatrixToImageWriter;

import com.google.zxing.common.BitMatrix;

import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class TestZXing {

@SuppressWarnings({ "rawtypes", "unchecked" })

public static void main(String[] args) {

int width=300;

int height=300;

String format="png";

String contents="www.baidu.com";

HashMap map=new HashMap();

map.put(EncodeHintType.CHARACTER_SET, "utf-8");

map.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);

map.put(EncodeHintType.MARGIN, 0);

try {

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

Path file=new File("D:/img.png").toPath();

MatrixToImageWriter.writeToPath(bm, format, file);

} catch (WriterException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

3.3创建TestRead类,编写解析二维码代码,运行代码,打印测试结果

package test;

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 TestRead {

@SuppressWarnings({ "unchecked", "rawtypes" })

public static void main(String[] args) {

try {

MultiFormatReader reader=new MultiFormatReader();

File f=new File("D:/img.png");

BufferedImage image=ImageIO.read(f);

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

HashMap map =new HashMap();

map.put(EncodeHintType.CHARACTER_SET, "utf-8");

Result result = reader.decode(bb,map);

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();

}

}

}

整理关于java实现二维码的生成和解析代码供大家参考

请大家多多关注我的头条号,谢谢大家

转载地址:http://uyzws.baihongyu.com/

你可能感兴趣的文章
CentOS Docker 安装
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
Mysql出现Table 'performance_schema.session_status' doesn't exist
查看>>
MySQL innert join、left join、right join等理解
查看>>
vivado模块封装ip/edf
查看>>
sdc时序约束
查看>>
Xilinx Jtag Access/svf文件/BSCANE2
查看>>
NoC片上网络
查看>>
开源SoC整理
查看>>
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
已知子网掩码,确定ip地址范围
查看>>
判断时间或者数字是否连续
查看>>
docker-daemon.json各配置详解
查看>>
Mac 下docker路径 /var/lib/docker不存在问题
查看>>
Docker(一)使用阿里云容器镜像服务
查看>>
Docker(二) 基础命令
查看>>
Docker(三) 构建镜像
查看>>