条形码和二维码编码解码工具类源码
有一个好的工具,会让你的开发事半功倍。再将讲这个工具类之前,我先给小白补充一点条形码和二维码(以下基础知识选自,我本科阶段的一本教材:《物联网导论》(刘云浩 编著)。有对物联网感兴趣的,可以看看这本书),我们要内外兼修,你说是不是这么个理呢!
多行组成的条形码,不需要连接一个数据库,本身可存储大量数据,应用于:医院、驾驶证、物料管理、货物运输,当条形码受一定破坏时,错误纠正能使条形码能正确解码。二维码,是一个多行、连续
性、可变长、包含大量数据的符号标识。每个条形码有3 - 90行,每一行有一个起始部分、数据部分、终止部分。它的字符集包括所有128个字符,最大数据含量是1850个字符。
一维条形码只是在一个方向(一般是水平方向)表达信息,而在垂直方向则不表达任何信息,其一定的高度通常是为了便于阅读器的对准。
一维条形码的应用可以提高信息录入的速度,减少差错率,但是一维条形码也存在一些不足之处:
数据容量较小: 30个字符左右
只能包含字母和数字
条形码尺寸相对较大(空间利用率较低)
条形码遭到损坏后便不能阅读
在水平和垂直方向的二维空间存储信息的条形码, 称为二维条形码(dimensional bar code)
优势
从以上的介绍可以看出,与二维条形码相比一维条形码有着明显的优势,归纳起来主要有以下几个方面:
(一)数据容量更大
(二)超越了字母数字的限制
(三)条形码相对尺寸小
(四)具有抗损毁能力
优点
1.高密度编码,信息容量大:
可容纳多达1850个大写字母或2710个数字或1108个字节,或500多个汉字,比普通条码信息容量约高几十倍。
2.编码范围广:
该条码可以把图片、声音、文字、签字、指纹等可以数字化的信息进行编码,用条码表示出来;可以表示多种语言文字;可表示图像数据。
3.容错能力强,具有纠错功能:
这使得二维条码因穿孔、污损等引起局部损坏时,照样可以正确得到识读,损毁面积达50%仍可恢复信息。
4.译码可靠性高:
它比普通条码译码错误率百万分之二要低得多,误码率不超过千万分之一。
5.可引入加密措施:
保密性、防伪性好。
6.成本低,易制作,持久耐用。
7.条码符号形状、尺寸大小比例可变。
8.二维条码可以使用激光或CCD阅读器识读。
看到这里,接下来,我给大家讲解一下封装的条形码和二维码编码解码类。
1 import java.awt.image.BufferedImage; 2 import java.io.File; 3 import java.util.Hashtable; 4 5 import javax.imageio.ImageIO; 6 7 import com.google.zxing.BarcodeFormat; 8 import com.google.zxing.BinaryBitmap; 9 import com.google.zxing.DecodeHintType; 10 import com.google.zxing.EncodeHintType; 11 import com.google.zxing.LuminanceSource; 12 import com.google.zxing.MultiFormatReader; 13 import com.google.zxing.MultiFormatWriter; 14 import com.google.zxing.Result; 15 import com.google.zxing.client.j2se.BufferedImageLuminanceSource; 16 import com.google.zxing.client.j2se.MatrixToImageWriter; 17 import com.google.zxing.common.BitMatrix; 18 import com.google.zxing.common.HybridBinarizer; 19 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; 20 21 /** 22 * 条形码和二维码编码解码 23 */ 24 public class ZxingHandler { 25 26 /** 27 * 条形码编码 28 * 29 * @param contents 30 * @param width 31 * @param height 32 * @param imgPath 33 */ 34 public static void encode(String contents, int width, int height, String imgPath) { 35 int codeWidth = 3 + // start guard 36 (7 * 6) + // left bars 37 5 + // middle guard 38 (7 * 6) + // right bars 39 3; // end guard 40 codeWidth = Math.max(codeWidth, width); 41 try { 42 BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, 43 BarcodeFormat.EAN_13, codeWidth, height, null); 44 45 MatrixToImageWriter 46 .writeToFile(bitMatrix, "png", new File(imgPath)); 47 48 } catch (Exception e) { 49 e.printStackTrace(); 50 } 51 } 52 53 /** 54 * 条形码解码 55 * 56 * @param imgPath 57 * @return String 58 */ 59 public static String decode(String imgPath) { 60 BufferedImage image = null; 61 Result result = null; 62 try { 63 image = ImageIO.read(new File(imgPath)); 64 if (image == null) { 65 System.out.println("the decode image may be not exit."); 66 } 67 LuminanceSource source = new BufferedImageLuminanceSource(image); 68 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); 69 70 result = new MultiFormatReader().decode(bitmap, null); 71 return result.getText(); 72 } catch (Exception e) { 73 e.printStackTrace(); 74 } 75 return null; 76 } 77 78 /** 79 * 二维码编码 80 * 81 * @param contents 82 * @param width 83 * @param height 84 * @param imgPath 85 */ 86 public static void encode2(String contents, int width, int height, String imgPath) { 87 Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); 88 // 指定纠错等级 89 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); 90 // 指定编码格式 91 hints.put(EncodeHintType.CHARACTER_SET, "GBK"); 92 try { 93 BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, 94 BarcodeFormat.QR_CODE, width, height, hints); 95 96 MatrixToImageWriter 97 .writeToFile(bitMatrix, "png", new File(imgPath)); 98 99 } catch (Exception e) { 100 e.printStackTrace(); 101 } 102 } 103 104 /** 105 * 二维码解码 106 * 107 * @param imgPath 108 * @return String 109 */ 110 public static String decode2(String imgPath) { 111 BufferedImage image = null; 112 Result result = null; 113 try { 114 image = ImageIO.read(new File(imgPath)); 115 if (image == null) { 116 System.out.println("the decode image may be not exit."); 117 } 118 LuminanceSource source = new BufferedImageLuminanceSource(image); 119 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); 120 121 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(); 122 hints.put(DecodeHintType.CHARACTER_SET, "GBK"); 123 124 result = new MultiFormatReader().decode(bitmap, hints); 125 return result.getText(); 126 } catch (Exception e) { 127 e.printStackTrace(); 128 } 129 return null; 130 } 131 132 /** 133 * @param args 134 */ 135 public static void main(String[] args) { 136 137 // 条形码 138 String imgPath = "target\\zxing_EAN13.png"; 139 String contents = "6923450657713"; 140 int width = 105, height = 50; 141 142 ZxingHandler.encode(contents, width, height, imgPath); 143 System.out.println("finished zxing EAN-13 encode."); 144 145 String decodeContent = ZxingHandler.decode(imgPath); 146 System.out.println("解码内容如下:" + decodeContent); 147 System.out.println("finished zxing EAN-13 decode."); 148 149 // 二维码 150 String imgPath2 = "target\\zxing.png"; 151 String contents2 = "Hello Gem, welcome to Zxing!" 152 + "\nBlog [ http://jeeplus.iteye.com ]" 153 + "\nEMail [ jeeplus@163.com ]"; 154 int width2 = 300, height2 = 300; 155 156 ZxingHandler.encode2(contents2, width2, height2, imgPath2); 157 System.out.println("finished zxing encode."); 158 159 String decodeContent2 = ZxingHandler.decode2(imgPath2); 160 System.out.println("解码内容如下:" + decodeContent2); 161 System.out.println("finished zxing decode."); 162 163 } 164 165 }
转载请注明出处!
http://www.cnblogs.com/libingbin/
感谢您的阅读。如果文章对您有用,那么请轻轻点个赞,以资鼓励。
转载于:https://www.cnblogs.com/libingbin/p/6032973.html
条形码和二维码编码解码工具类源码相关推荐
- URL编码解码工具类
/****************************************************************************** * CREATETIME : 2016年 ...
- aes加密php源码,AES加解密类源码 · ThinkPHP5高阶实战教程 --诠释为API开发而生 · 看云...
# AES加解密类源码 > 根据网络整理 ~~~ /** * Created by PhpStorm. * Power by Mikkle * QQ:776329498 * Date: 2017 ...
- vue 二维数组_最近研究Vue源码时我发现的一些好玩函数
来源 | segmentfault.com/u/chinamasters 作者 | chinamasters 最近在深入研究vue源码,把学习过程中,看到的一些好玩的的函数方法收集起来做分享,希望对大 ...
- 语音识别之HTK入门(十)——HTK解码工具HVite源码分析
这一节讲的内容又是语音识别系统非常重要的一环--veterbi解码,前面我们经过了配置文件,处理音频数据,处理标注文本数据.通过Baum-Welch(前向-后向)算法评估模型参数等多个环节,目的都是为 ...
- python:实现9×9二维数组数独算法(附完整源码)
python:实现9×9二维数组数独算法 from __future__ import annotationsMatrix = list[list[int]]# assigning initial v ...
- 【图像分割】基于matlab灰狼算法二维oust图像分割【含Matlab源码 2574期】
⛄一. 灰狼算法简介 1 前言 灰狼优化算法(Grey Wolf Optimizer,GWO)由澳大利亚格里菲斯大学学者 Mirjalili 等人于2014年提出来的一种群智能优化算法.该算法受到了灰 ...
- 免费天气查询工具类源码,开箱即用,根据中国气象局API编写。高效稳定
文章目录 引言 相关依赖 WeatherUtil工具类代码 http工具类 测试 引言 使用Java语言,根据中国气象局API编写的查询天气工具类,代码引入就能用,代码中对异常做了处理,无论是否查询成 ...
- java工具类源码阅读,java学习日记第二天(实用的工具类和源码解析一Arrays)
本帖最后由 三木猿 于 2020-9-18 11:17 编辑 每日名言 学者须先立志.今日所以悠悠者,只是把学问不曾做一件事看,遇事则且胡乱恁地打过了,此只是志不立. --朱熹 工作中经常会用到一些工 ...
- 【图像分割】灰狼算法二维oust图像分割【含Matlab源码 2574期】
⛄一. 灰狼算法简介 1 前言 灰狼优化算法(Grey Wolf Optimizer,GWO)由澳大利亚格里菲斯大学学者 Mirjalili 等人于2014年提出来的一种群智能优化算法.该算法受到了灰 ...
最新文章
- libgdx 1.4.1公布
- PAT甲级1077 Kuchiguse:[C++题解]字符串、最长公共后缀
- 科学计算机怎么显示除尽的数字,如何快速判断一个数能被整除的方法(1-23之内)...
- 【MM模块】RFQ 采购询价单
- LeetCode 121. Best Time to Buy and Sell Stock
- Windows和Linux DNS Cache清理
- linux修音软件下载,修音软件下载 Auto Tune 8(修音工具) v8.1.2 免费安装版 下载-脚本之家...
- 软件测试计划的主要内容
- 速读-对抗攻击的弹性异构DNN加速器体系结构
- 常用通信光缆型号是如何命名的
- Linux|超好用!绘制流程图神器——PlantUML
- python调用sdk接口_基于聚合数据的短信API接口调用示例-Python版
- python串口收发
- 【历史上的今天】12 月 20 日:苹果收购 NeXT;苏联超级计算机先驱诞生;《绝地求生》发布
- 中转网关 (Transit Gateway) Connect连接类型集成FortiGate安全服务
- 一道经典的面试题:一只公鸡5块钱,一只母鸡3块钱,3只小鸡一块钱,一个农夫用100块钱买100只鸡(编写java程序)...
- java 判断图片格式_Java判断上传图片格式的实例代码
- Automated Phrase
- 安装 Cartographer问题记录汇总
- 寻址 far near