ZXing 二维码解析生成工具类
原文:http://www.open-open.com/code/view/1455848023292
import com.google.zxing.*;
import com.google.zxing.Reader;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.multi.GenericMultipleBarcodeReader;
import com.google.zxing.multi.MultipleBarcodeReader; import javax.imageio.ImageIO;
import java.io.*;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.*; /**
* 二维码生成工具类
*
* @author KisChang
* @version 1.0
* @date 2015年12月03日
* @since 1.0
*/
public class ZXingUtils { public static enum ImageType {
JPEG("jpeg"),PNG("png"),GIF("gif");
private String value; ImageType(String value) {
this.value = value;
} public String getValue() {
return value;
}
} /**编码*/
public static class Encode { private static Map<EncodeHintType, Object> HINTS;
static {
HINTS = new EnumMap<EncodeHintType,Object>(EncodeHintType.class);
HINTS.put(EncodeHintType.CHARACTER_SET, "UTF-8");
}
/**
* 生成二维码
* @param widthAndHeight 高宽
* @param content 二维码内容
* @param os 输出流
*/
public static void buildQRCode(int widthAndHeight, String content, OutputStream os, ImageType imageType) throws WriterException, IOException {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight, HINTS);// 生成矩阵
MatrixToImageWriter.writeToStream(bitMatrix, imageType.getValue(), os);
} public static void buildQRCode(String content, OutputStream os, ImageType imageType) throws WriterException, IOException {
buildQRCode(200, content, os, imageType);
} /**
* 生成二维码
* @param widthAndHeight 高宽
* @param content 二维码内容
* @param filePath 输出目录
* @param fileName 输出文件名
* @param imageType 输出文件类型
*/
public static void buildQRCode(int widthAndHeight, String content, String filePath, String fileName, ImageType imageType) throws WriterException, IOException {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight, HINTS);
Path path = FileSystems.getDefault().getPath(filePath, fileName);
MatrixToImageWriter.writeToPath(bitMatrix, imageType.getValue(), path);// 输出图像
} public static void buildQRCode(String content, String filePath, String fileName, ImageType imageType) throws WriterException, IOException {
buildQRCode(200, content,filePath,fileName,imageType);
}
} /**解码*/
public static class Decode { private static final Map<DecodeHintType,Object> HINTS;
private static final Map<DecodeHintType,Object> HINTS_PURE;
static {
HINTS = new EnumMap<DecodeHintType,Object>(DecodeHintType.class);
HINTS.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
HINTS.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.allOf(BarcodeFormat.class));
HINTS_PURE = new EnumMap<DecodeHintType,Object>(HINTS);
HINTS_PURE.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
} /**
* 解析二维码
*/
public static Collection<Result> readQRCode(File qrCode) throws ReaderException, IOException {
FileInputStream inputStream = new FileInputStream(qrCode);
return readQRCode(inputStream);
} public static Collection<Result> readQRCode(InputStream inputStream) throws ReaderException, IOException {
LuminanceSource source = new BufferedImageLuminanceSource(ImageIO.read(inputStream));
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source)); Collection<Result> results = new ArrayList<Result>(1);
ReaderException savedException = null;
Reader reader = new MultiFormatReader();
try {
//寻找多个条码
MultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
Result[] theResults = multiReader.decodeMultiple(binaryBitmap, HINTS);
if (theResults != null) {
results.addAll(Arrays.asList(theResults));
}
} catch (ReaderException re) {
savedException = re;
} if (results.isEmpty()) {
try {
//寻找纯条码
Result theResult = reader.decode(binaryBitmap, HINTS_PURE);
if (theResult != null) {
results.add(theResult);
}
} catch (ReaderException re) {
savedException = re;
}
} if (results.isEmpty()) {
try {
//寻找图片中的正常条码
Result theResult = reader.decode(binaryBitmap, HINTS);
if (theResult != null) {
results.add(theResult);
}
} catch (ReaderException re) {
savedException = re;
}
} if (results.isEmpty()) {
try {
//再次尝试其他特殊处理
BinaryBitmap hybridBitmap = new BinaryBitmap(new HybridBinarizer(source));
Result theResult = reader.decode(hybridBitmap, HINTS);
if (theResult != null) {
results.add(theResult);
}
} catch (ReaderException re) {
savedException = re;
}
}
if (results.isEmpty()){
throw savedException;
}else {
return results;
}
} public static Result readQRCodeResult(File qrCode) throws ReaderException, IOException {
FileInputStream inputStream = new FileInputStream(qrCode);
return readQRCodeResult(inputStream);
}
public static Result readQRCodeResult(InputStream inputStream) throws ReaderException, IOException {
Collection<Result> results = readQRCode(inputStream);
if (!results.isEmpty()){
//寻找结果集中非空的结果
for (Result result : results){
if (result != null){
return result;
}
}
}
throw NotFoundException.getNotFoundInstance();
}
}
}
ZXing 二维码解析生成工具类的更多相关文章
- ZXing二维码的生成和解析
Zxing是Google提供的关于条码(一维码.二维码)的解析工具,提供了二维码的生成与解析的方法, 现在我简单介绍一下使用Java利用Zxing生成与解析二维码 注意: 二维码的生成需要借助辅助类( ...
- zxing二维码的生成与解码(C#)
ZXing是一个开源Java类库用于解析多种格式的1D/2D条形码.目标是能够对QR编码.Data Matrix.UPC的1D条形码进行解码. 其提供了多种平台下的客户端包括:J2ME.J2SE和An ...
- Java中使用google.zxing快捷生成二维码(附工具类源码)
移动互联网时代,基于手机端的各种活动扫码和收付款码层出不穷:那我们如何在Java中生成自己想要的二维码呢?下面就来讲讲在Java开发中使用 google.zxing 生成二维码. 一般情况下,Java ...
- Zxing二维码解析——图文转换
一:文字转化为二维码图片. package com.xhm.tool; import java.util.Hashtable; import android.graphics.Bitmap; impo ...
- 谷歌zxing 二维码生成工具
一.加入maven依赖 <!-- 谷歌zxing 二维码 --> <dependency> <groupId>com.google.zxing</groupI ...
- Android zxing 解析二维码,生成二维码极简demo
zxing 官方的代码很多,看起来很费劲,此demo只抽取了有用的部分,实现了相机预览解码,解析本地二维码,生成二维码三个功能. 简化后的结构如下: 废话少说直接上代码: BaseDecodeHand ...
- Atitit zxing二维码qr码识别解析
Atitit zxing二维码qr码识别解析 1.1. qr码识别解析 by zxing1 1.2. 解码lib:qrcode.jar 2 1.3. atitit.二维码生成总结java zxing ...
- Java 条形码 二维码 的生成与解析
Barcode简介 Barcode是由一组按一定编码规则排列的条,空符号,用以表示一定的字符,数字及符号组成的,一种机器可读的数据表示方式. Barcode的形式多种多样,按照它们的外观分类: Lin ...
- .net core 的图片处理及二维码的生成及解析
写代码这事,掐指算来已经十有余年. 从html到css到javascript到vbscript到c#,从兴趣到职业,生活总是失落与惊喜并存. 绝大部分时候,出发并不是因为知道该到哪里去,只是知道不能再 ...
随机推荐
- mysql 函数tree状
// 子节点的查询 CREATE DEFINER = `root`@`%` FUNCTION `getDeptChildList`(rootId BIGINT) RETURNS longtext DE ...
- windows安装tensorflow的一个教训
今天没什么课,然后就准备安装tensorflow. 看了一下教程,就去做了. 然后就犯了错误.网上的教程还是有一些差异的,而我又比较大意,没有很注意到CUDA,cudnn的版本要求,也过于高估自己cp ...
- python基础一 day3 列表方法
ls=['a','b','c','d','a','b','c','d']lst=['e','f','g','h']# 增加# ls.append('a') 将元素a添加至列表ls的尾部# ls.ext ...
- 解决margin塌陷问题
父元素添加: border: 1px solid transparent; 或者 over-flow:hidden;
- No-7.系统信息相关命令
系统信息相关命令 本节内容主要是为了方便通过远程终端维护服务器时,查看服务器上当前 系统日期和时间 / 磁盘空间占用情况 / 程序执行情况 本小结学习的终端命令基本都是查询命令,通过这些命令对系统资源 ...
- Nginx Location和Rewrite总结
Nginx 版本:nginx/1.10.3 (Ubuntu) Location 部分: 第一步:创建Nginx 虚拟主机 Nginx 安装成功安装并且可以运行之后,在 /etc/nginx 目录下创建 ...
- Python中的函数(5)
一.向函数中传递任意数量的实参 有时候,你预先不知道函数需要接受多少个实参,Python中函数可以收集任意数量的实参. 栗子:来看一个打印好友列表功能的函数,它需要接收任意数量的好友名.如下: def ...
- Python之 七级字典查询
# -*- coding:utf- -*- # 作业要求: # 打印直辖市,省,市,县,区,街道五级菜单: # 可以一层一层地进入到所有层 # 可以退出到上一层 # 可随时退出程序 mapChina ...
- 《算法导论》 — Chapter 10 基本数据结构
序 在本章中,要讨论如何通过使用了指针的简单数据结构表示动态集合.有很多的复杂的数据结构可以用指针来构造,本章介绍几种基本数据结构,包括栈.队列.链表,以及有根树. GitHub 第十章 程序实现代码 ...
- laravel count distinct
$result->count(\DB::raw("distinct(material_id)"));