原文: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 二维码解析生成工具类的更多相关文章

  1. ZXing二维码的生成和解析

    Zxing是Google提供的关于条码(一维码.二维码)的解析工具,提供了二维码的生成与解析的方法, 现在我简单介绍一下使用Java利用Zxing生成与解析二维码 注意: 二维码的生成需要借助辅助类( ...

  2. zxing二维码的生成与解码(C#)

    ZXing是一个开源Java类库用于解析多种格式的1D/2D条形码.目标是能够对QR编码.Data Matrix.UPC的1D条形码进行解码. 其提供了多种平台下的客户端包括:J2ME.J2SE和An ...

  3. Java中使用google.zxing快捷生成二维码(附工具类源码)

    移动互联网时代,基于手机端的各种活动扫码和收付款码层出不穷:那我们如何在Java中生成自己想要的二维码呢?下面就来讲讲在Java开发中使用 google.zxing 生成二维码. 一般情况下,Java ...

  4. Zxing二维码解析——图文转换

    一:文字转化为二维码图片. package com.xhm.tool; import java.util.Hashtable; import android.graphics.Bitmap; impo ...

  5. 谷歌zxing 二维码生成工具

    一.加入maven依赖 <!-- 谷歌zxing 二维码 --> <dependency> <groupId>com.google.zxing</groupI ...

  6. Android zxing 解析二维码,生成二维码极简demo

    zxing 官方的代码很多,看起来很费劲,此demo只抽取了有用的部分,实现了相机预览解码,解析本地二维码,生成二维码三个功能. 简化后的结构如下: 废话少说直接上代码: BaseDecodeHand ...

  7. Atitit zxing二维码qr码识别解析

    Atitit zxing二维码qr码识别解析 1.1. qr码识别解析 by zxing1 1.2. 解码lib:qrcode.jar  2 1.3. atitit.二维码生成总结java zxing ...

  8. Java 条形码 二维码 的生成与解析

    Barcode简介 Barcode是由一组按一定编码规则排列的条,空符号,用以表示一定的字符,数字及符号组成的,一种机器可读的数据表示方式. Barcode的形式多种多样,按照它们的外观分类: Lin ...

  9. .net core 的图片处理及二维码的生成及解析

    写代码这事,掐指算来已经十有余年. 从html到css到javascript到vbscript到c#,从兴趣到职业,生活总是失落与惊喜并存. 绝大部分时候,出发并不是因为知道该到哪里去,只是知道不能再 ...

随机推荐

  1. leetcode_919. Complete Binary Tree Inserter_完全二叉树插入

    https://leetcode.com/problems/complete-binary-tree-inserter/ 给出树节点的定义和完全二叉树插入器类的定义,为这个类补全功能.完全二叉树的定义 ...

  2. mysql 增删查改

    非关系型数据库关系型数据库Oracle mysql sqlserver db2 Postgresql Sqlite access sqlserver 微软db2 ibm================ ...

  3. HashMap Hashtable TreeMap LinkedHashMap 分析

    首先对hash的了解:就是关键字,和数据建立关系的映射. hash常用算法:假设我们中的字符有相应的内部编码,当然在实际过程中,我们不可能将所有的编码当做hash值. 平方取中法,将所得的内部编码平方 ...

  4. iview table 普通表格样式

    iview table 普通表格样式 https://run.iviewui.com/UvLFPMb0 <template> <table> <thead> < ...

  5. Unexpected Exception caught setting 'username' on 'class com.bj186.crm.web.action.UserAction: Error setting expression 'username' with value ['艾格尼丝', ]

    问题场景: 在使用表单向Action传递数据的时候, 遇到了这个问题, 导致了空指针异常. 问题描述: 10:14:56.622 [http-nio-8080-exec-45] ERROR com.o ...

  6. DirectX9:高级着色语言(HLSL)

    一.简介 高级着色语言(High)可以编写顶点着色器和像素着色器,取代固定功能流水线中的部分功能,在图形卡的GPU(Graphics Processing Unit,图形处理单元)中执行 注意:如果图 ...

  7. C语言之static用法

    1,static修饰全局变量 限定变量的作用域.被static修饰的全局变量存储域不变,依然存储在静态存储区,即bss段或data段.但作用域发生改变,被static修饰全局变量只能被本文件的函数访问 ...

  8. Spring Boot 返回Html界面

    @Controller public class HelloController { @RequestMapping("/") public String index(){ ret ...

  9. Uncaught ReferenceError: 板栗 is not defined at HTMLButtonElement.onclick (view:1)

    对JS传值一直以为都是随便传过去就行,直到今天遇到了中文传值的问题 中文传值不能够需要在调用的位置加 对于要传的值加单引号或者双引号 比如说下面这个样子,我这里还还记反斜杠注释 '<button ...

  10. ES6(字符串)

    ES6新增字符串特性 一.Unicode的表示法 当码值>2个字节(0xff) 即第一个数字未处理,不显示 处理这种超过2字节的情况,用{}包起来即可 二.API 1.ES5中 码值>2个 ...