原文: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. SAP成都研究院姚瑶:软件质量保证工作的变迁

    大家好,我是来自SAP成都研究院Revenue Cloud 团队的质量工程师 , yoyo.很高兴可以和大家分享我个人的工作体会.每个团队都有QE(Quality Engineer), 相信大家对QE ...

  2. 闲着蛋疼没事干,写个Mac端的Kcptun Client管理器

    原理: 执行一行脚本 输入服务器地址,端口,密码等做了图形化编辑 可以控制Kcptun是否正在运行 App已上传github https://github.com/nicky2k8/KcptunCli ...

  3. 骑芯供应链(W 笔试)

    单选题 1.调用本身构造方法 答案:this(X) 2.若y=3,a=2,b=4,运行y+=a++/--b,求y的值 答案: 3.不能控制servlet生命周期的方法是? 选项:init().serv ...

  4. DROP DOMAIN - 删除一个用户定义的域

    SYNOPSIS DROP DOMAIN name [, ...] [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP DOMAIN 将从系统表中删除一个用户域. 只 ...

  5. this.$emit('on-select-change' emit里面不能写大写字母

    this.$emit('on-select-change' emit里面不能写大写字母 刚试了下 也能写大写 但是 两边就都写一样就完了,就都写成带-的就完了

  6. sh脚本写法

    1.shell注释符号: 1. 单行注释: “#” 2. 多行注释: : << ! 语句1 语句2 语句3 语句4 ! http://blog.csdn.net/lansesl2008/a ...

  7. C++虚析构函数的使用

    如果,你设计的程序里,释放对象实例的时候,有“使用某个基类的指针,来释放它指向的派生类的实例”这种用法出现的话,那么,这个基类的destructor就应该设计成virtual的. 如果,基类不是vir ...

  8. 【转】解决WPF图片模糊最佳方法(绑定PixelWidth与PixelHeight)

    解决WPF图片模糊最佳方法(绑定PixelWidth与PixelHeight) 转载自:http://www.360doc.com/content/13/1126/09/10504424_332211 ...

  9. vue 中scroll事件不触发问题

    在vue项目中需要监听滚动条滚动的位置,结果写了scroll监听事件就是不生效,最后查资料发现是页面有样式设置了over-flow:scroll,去掉之后完美解决.(页面样式中存在over-flow: ...

  10. 模拟--P1328 生活大爆炸版石头剪刀布 题解

    P1328 生活大爆炸版石头剪刀布 这也是打表么?? #include <iostream> using namespace std; static const auto y = []() ...