代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
二维码解析生成工具类
作者:
金龙
/ 发布于
2016/2/17
/
757
ZXing二维码解析生成类,主要完善了一下解析功能(官网演示项目代码整理),针对简单解析时有可能报NotFoundException异常(解析失败),但是在官网(http://zxing.org/w/decode.jspx)却能正常的解析。
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(); } } }
试试其它关键字
二维码
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
金龙
贡献的其它代码
(
8
)
.
判断手机横屏和竖屏方向
.
JSON按key进行排序
.
随意两个数的乘积
.
Jquery-与其他Javascript类库冲突解决方案
.
XtraTabControl右键添加关闭当前页、关闭其他页、全部
.
图片转字符
.
二维码解析生成工具类
.
验证sql语句是否正确
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3