最近琢磨了一下二维码、一维码的编码、解码方法,感觉google的zxing用起来还是比较方便。
  本人原创,欢迎转载,转载请标注原文地址:http://wallimn.iteye.com/blog/2071020 一、工具类
Java代码 收藏代码
package com.exam.services.qrcode; import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer; import javax.imageio.ImageIO; import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.awt.image.BufferedImage; /**
* 使用ZXing2.3,生成条码的辅助类。可以编码、解码。编码使用code包,解码需要javase包。
*
* <br/>
* <br/>
* 作者:wallimn<br/>
* 联系:54871876@qq.com,http://wallimn.iteye.com<br/>
* 时间:2014年5月25日  下午10:33:05<br/>
*/
public final class MatrixUtil { private static final String CHARSET = "utf-8";
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF; /**
* 禁止生成实例,生成实例也没有意义。
*/
private MatrixUtil() {
} /**
* 生成矩阵,是一个简单的函数,参数固定,更多的是使用示范。
*
* <br/>
* <br/>
* 作者:wallimn<br/>
* 时间:2014年5月25日  下午10:41:12<br/>
* 联系:54871876@qq.com<br/>
*
* @param text
* @return
*/
public static BitMatrix toQRCodeMatrix(String text, Integer width,
Integer height) {
if (width == null || width < 300) {
width = 300;
} if (height == null || height < 300) {
height = 300;
}
// 二维码的图片格式
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
// 内容所使用编码
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
BitMatrix bitMatrix = null;
try {
bitMatrix = new MultiFormatWriter().encode(text,
BarcodeFormat.QR_CODE, width, height, hints);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 生成二维码
// File outputFile = new File("d:"+File.separator+"new.gif");
// MatrixUtil.writeToFile(bitMatrix, format, outputFile);
return bitMatrix;
} /**
* 将指定的字符串生成二维码图片。简单的使用示例。
*
* <br/>
* <br/>
* 作者:wallimn<br/>
* 时间:2014年5月25日  下午10:44:52<br/>
* 联系:54871876@qq.com<br/>
*
* @param text
* @param file
* @param format
* @return
*/
public boolean toQrcodeFile(String text, File file, String format) {
BitMatrix matrix = toQRCodeMatrix(text, null, null);
if (matrix != null) {
try {
writeToFile(matrix, format, file);
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
} /**
* 根据点矩阵生成黑白图。 作者:wallimn<br/>
* 时间:2014年5月25日  下午10:26:22<br/>
* 联系:54871876@qq.com<br/>
*/
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
} /**
* 将字符串编成一维条码的矩阵
*
* <br/>
* <br/>
* 作者:wallimn<br/>
* 时间:2014年5月25日  下午10:56:34<br/>
* 联系:54871876@qq.com<br/>
*
* @param str
* @param width
* @param height
* @return
*/
public static BitMatrix toBarCodeMatrix(String str, Integer width,
Integer height) { if (width == null || width < 200) {
width = 200;
} if (height == null || height < 50) {
height = 50;
} try {
// 文字编码
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, CHARSET); BitMatrix bitMatrix = new MultiFormatWriter().encode(str,
BarcodeFormat.CODE_128, width, height, hints); return bitMatrix;
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* 根据矩阵、图片格式,生成文件。 作者:wallimn<br/>
* 时间:2014年5月25日  下午10:26:43<br/>
* 联系:54871876@qq.com<br/>
*/
public static void writeToFile(BitMatrix matrix, String format, File file)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format "
+ format + " to " + file);
}
} /**
* 将矩阵写入到输出流中。 作者:wallimn<br/>
* 时间:2014年5月25日  下午10:27:58<br/>
* 联系:54871876@qq.com<br/>
*/
public static void writeToStream(BitMatrix matrix, String format,
OutputStream stream) throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format "
+ format);
}
} /**
* 解码,需要javase包。
*
* <br/>
* <br/>
* 作者:wallimn<br/>
* 时间:2014年5月25日  下午11:06:07<br/>
* 联系:54871876@qq.com<br/>
*
* @param file
* @return
*/
public static String decode(File file) { BufferedImage image;
try {
if (file == null || file.exists() == false) {
throw new Exception(" File not found:" + file.getPath());
} image = ImageIO.read(file); LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result; // 解码设置编码方式为:utf-8,
Hashtable hints = new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET); result = new MultiFormatReader().decode(bitmap, hints); return result.getText(); } catch (Exception e) {
e.printStackTrace();
} return null;
}
} 二、使用示例
Java代码 收藏代码
package com.exam.services.qrcode; import java.io.File; public class Test { /**
* 测试函数。简单地将指定的字符串生成二维码图片。
*
* <br/><br/>
* 作者:wallimn<br/>
* 时间:2014年5月25日  下午10:30:00<br/>
* 联系:54871876@qq.com<br/>
*/
public static void main(String[] args) throws Exception {
String text = "http://wallimn.itey.com";
String result;
String format = "gif";
//生成二维码
File outputFile = new File("d:"+File.separator+"rqcode.gif");
MatrixUtil.writeToFile(MatrixUtil.toQRCodeMatrix(text, null, null), format, outputFile);
result = MatrixUtil.decode(outputFile);
System.out.println(result); outputFile = new File("d:"+File.separator+"barcode.gif");
MatrixUtil.writeToFile(MatrixUtil.toBarCodeMatrix(text, null, null), format, outputFile); result = MatrixUtil.decode(outputFile);
System.out.println(result);
} }

  

  最近琢磨了一下二维码、一维码的编码、解码方法,感觉google的zxing用起来还是比较方便。 
  本人原创,欢迎转载,转载请标注原文地址:http://wallimn.iteye.com/blog/2071020

一、工具类

  1. package com.exam.services.qrcode;
  2. import com.google.zxing.BarcodeFormat;
  3. import com.google.zxing.BinaryBitmap;
  4. import com.google.zxing.DecodeHintType;
  5. import com.google.zxing.EncodeHintType;
  6. import com.google.zxing.LuminanceSource;
  7. import com.google.zxing.MultiFormatReader;
  8. import com.google.zxing.MultiFormatWriter;
  9. import com.google.zxing.Result;
  10. import com.google.zxing.WriterException;
  11. import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
  12. import com.google.zxing.common.BitMatrix;
  13. import com.google.zxing.common.HybridBinarizer;
  14. import javax.imageio.ImageIO;
  15. import java.io.File;
  16. import java.io.OutputStream;
  17. import java.io.IOException;
  18. import java.util.Hashtable;
  19. import java.awt.image.BufferedImage;
  20. /**
  21. * 使用ZXing2.3,生成条码的辅助类。可以编码、解码。编码使用code包,解码需要javase包。
  22. *
  23. * <br/>
  24. * <br/>
  25. * 作者:wallimn<br/>
  26. * 联系:54871876@qq.com,http://wallimn.iteye.com<br/>
  27. * 时间:2014年5月25日  下午10:33:05<br/>
  28. */
  29. public final class MatrixUtil {
  30. private static final String CHARSET = "utf-8";
  31. private static final int BLACK = 0xFF000000;
  32. private static final int WHITE = 0xFFFFFFFF;
  33. /**
  34. * 禁止生成实例,生成实例也没有意义。
  35. */
  36. private MatrixUtil() {
  37. }
  38. /**
  39. * 生成矩阵,是一个简单的函数,参数固定,更多的是使用示范。
  40. *
  41. * <br/>
  42. * <br/>
  43. * 作者:wallimn<br/>
  44. * 时间:2014年5月25日  下午10:41:12<br/>
  45. * 联系:54871876@qq.com<br/>
  46. *
  47. * @param text
  48. * @return
  49. */
  50. public static BitMatrix toQRCodeMatrix(String text, Integer width,
  51. Integer height) {
  52. if (width == null || width < 300) {
  53. width = 300;
  54. }
  55. if (height == null || height < 300) {
  56. height = 300;
  57. }
  58. // 二维码的图片格式
  59. Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
  60. // 内容所使用编码
  61. hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
  62. BitMatrix bitMatrix = null;
  63. try {
  64. bitMatrix = new MultiFormatWriter().encode(text,
  65. BarcodeFormat.QR_CODE, width, height, hints);
  66. } catch (WriterException e) {
  67. // TODO Auto-generated catch block
  68. e.printStackTrace();
  69. }
  70. // 生成二维码
  71. // File outputFile = new File("d:"+File.separator+"new.gif");
  72. // MatrixUtil.writeToFile(bitMatrix, format, outputFile);
  73. return bitMatrix;
  74. }
  75. /**
  76. * 将指定的字符串生成二维码图片。简单的使用示例。
  77. *
  78. * <br/>
  79. * <br/>
  80. * 作者:wallimn<br/>
  81. * 时间:2014年5月25日  下午10:44:52<br/>
  82. * 联系:54871876@qq.com<br/>
  83. *
  84. * @param text
  85. * @param file
  86. * @param format
  87. * @return
  88. */
  89. public boolean toQrcodeFile(String text, File file, String format) {
  90. BitMatrix matrix = toQRCodeMatrix(text, null, null);
  91. if (matrix != null) {
  92. try {
  93. writeToFile(matrix, format, file);
  94. return true;
  95. } catch (IOException e) {
  96. // TODO Auto-generated catch block
  97. e.printStackTrace();
  98. }
  99. }
  100. return false;
  101. }
  102. /**
  103. * 根据点矩阵生成黑白图。 作者:wallimn<br/>
  104. * 时间:2014年5月25日  下午10:26:22<br/>
  105. * 联系:54871876@qq.com<br/>
  106. */
  107. public static BufferedImage toBufferedImage(BitMatrix matrix) {
  108. int width = matrix.getWidth();
  109. int height = matrix.getHeight();
  110. BufferedImage image = new BufferedImage(width, height,
  111. BufferedImage.TYPE_INT_RGB);
  112. for (int x = 0; x < width; x++) {
  113. for (int y = 0; y < height; y++) {
  114. image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
  115. }
  116. }
  117. return image;
  118. }
  119. /**
  120. * 将字符串编成一维条码的矩阵
  121. *
  122. * <br/>
  123. * <br/>
  124. * 作者:wallimn<br/>
  125. * 时间:2014年5月25日  下午10:56:34<br/>
  126. * 联系:54871876@qq.com<br/>
  127. *
  128. * @param str
  129. * @param width
  130. * @param height
  131. * @return
  132. */
  133. public static BitMatrix toBarCodeMatrix(String str, Integer width,
  134. Integer height) {
  135. if (width == null || width < 200) {
  136. width = 200;
  137. }
  138. if (height == null || height < 50) {
  139. height = 50;
  140. }
  141. try {
  142. // 文字编码
  143. Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
  144. hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
  145. BitMatrix bitMatrix = new MultiFormatWriter().encode(str,
  146. BarcodeFormat.CODE_128, width, height, hints);
  147. return bitMatrix;
  148. } catch (Exception e) {
  149. e.printStackTrace();
  150. }
  151. return null;
  152. }
  153. /**
  154. * 根据矩阵、图片格式,生成文件。 作者:wallimn<br/>
  155. * 时间:2014年5月25日  下午10:26:43<br/>
  156. * 联系:54871876@qq.com<br/>
  157. */
  158. public static void writeToFile(BitMatrix matrix, String format, File file)
  159. throws IOException {
  160. BufferedImage image = toBufferedImage(matrix);
  161. if (!ImageIO.write(image, format, file)) {
  162. throw new IOException("Could not write an image of format "
  163. + format + " to " + file);
  164. }
  165. }
  166. /**
  167. * 将矩阵写入到输出流中。 作者:wallimn<br/>
  168. * 时间:2014年5月25日  下午10:27:58<br/>
  169. * 联系:54871876@qq.com<br/>
  170. */
  171. public static void writeToStream(BitMatrix matrix, String format,
  172. OutputStream stream) throws IOException {
  173. BufferedImage image = toBufferedImage(matrix);
  174. if (!ImageIO.write(image, format, stream)) {
  175. throw new IOException("Could not write an image of format "
  176. + format);
  177. }
  178. }
  179. /**
  180. * 解码,需要javase包。
  181. *
  182. * <br/>
  183. * <br/>
  184. * 作者:wallimn<br/>
  185. * 时间:2014年5月25日  下午11:06:07<br/>
  186. * 联系:54871876@qq.com<br/>
  187. *
  188. * @param file
  189. * @return
  190. */
  191. public static String decode(File file) {
  192. BufferedImage image;
  193. try {
  194. if (file == null || file.exists() == false) {
  195. throw new Exception(" File not found:" + file.getPath());
  196. }
  197. image = ImageIO.read(file);
  198. LuminanceSource source = new BufferedImageLuminanceSource(image);
  199. BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
  200. Result result;
  201. // 解码设置编码方式为:utf-8,
  202. Hashtable hints = new Hashtable();
  203. hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
  204. result = new MultiFormatReader().decode(bitmap, hints);
  205. return result.getText();
  206. } catch (Exception e) {
  207. e.printStackTrace();
  208. }
  209. return null;
  210. }
  211. }

二、使用示例

  1. package com.exam.services.qrcode;
  2. import java.io.File;
  3. public class Test {
  4. /**
  5. * 测试函数。简单地将指定的字符串生成二维码图片。
  6. *
  7. * <br/><br/>
  8. * 作者:wallimn<br/>
  9. * 时间:2014年5月25日  下午10:30:00<br/>
  10. * 联系:54871876@qq.com<br/>
  11. */
  12. public static void main(String[] args) throws Exception {
  13. String text = "http://wallimn.itey.com";
  14. String result;
  15. String format = "gif";
  16. //生成二维码
  17. File outputFile = new File("d:"+File.separator+"rqcode.gif");
  18. MatrixUtil.writeToFile(MatrixUtil.toQRCodeMatrix(text, null, null), format, outputFile);
  19. result = MatrixUtil.decode(outputFile);
  20. System.out.println(result);
  21. outputFile = new File("d:"+File.separator+"barcode.gif");
  22. MatrixUtil.writeToFile(MatrixUtil.toBarCodeMatrix(text, null, null), format, outputFile);
  23. result = MatrixUtil.decode(outputFile);
  24. System.out.println(result);
  25. }
  26. }

java利用zxing编码解码一维码与二维码的更多相关文章

  1. C# 利用ZXing.Net来生成条形码和二维码

    本文是利用ZXing.Net在WinForm中生成条形码,二维码的小例子,仅供学习分享使用,如有不足之处,还请指正. 什么是ZXing.Net? ZXing是一个开放源码的,用Java实现的多种格式的 ...

  2. ZXing拍摄代码扫描之后以区分一维码、二维码、其他代码

    我怎么有二维码没有联系,最近遇到一个问题,,如何推断条码扫描到一维代码或者二维代码,辛苦了一个下午下班后自己,加上网上跟踪信息. 总结出两种方式能够解决该问题(推荐採用另外一种方式): 1.改动源代码 ...

  3. Halcon一维码和二维码的解码步骤和技巧——第11讲

    针对Halcon中一维码和二维码的解码,我分别写了两篇文章,参见: <Halcon的一维条码解码步骤和解码技巧>:https://www.cnblogs.com/xh6300/p/1048 ...

  4. 【转】 Android 基于google Zxing实现对手机中的二维码进行扫描--不错

    原文网址:http://blog.csdn.net/xiaanming/article/details/14450809 转载请注明出处:http://blog.csdn.net/xiaanming/ ...

  5. Android 基于google Zxing实现对手机中的二维码进行扫描

    转载请注明出处:http://blog.csdn.net/xiaanming/article/details/14450809 有时候我们有这样子的需求,需要扫描手机中有二维码的的图片,所以今天实现的 ...

  6. .NET使用ZXing.NET生成中间带图片的二维码

    很久之前就有写这样的代码了,只是一直没记录下来,偶然想写成博客. 把之前的代码封装成函数,以方便理解以及调用. 基于开源的 ZXing.NET 组件,代码如下: 先添加对ZXing.NET的引用,然后 ...

  7. C#使用zxing,zbar,thoughtworkQRcode解析二维码,附源代码

    最近做项目需要解析二维码图片,找了一大圈,发现没有人去整理下开源的几个库案例,花了点时间 做了zxing,zbar和thoughtworkqrcode解析二维码案例,希望大家有帮助. zxing是谷歌 ...

  8. 使用qrcode输入信息生成二维码包含二维码说明信息,点击转化为图片并下载

    说明:输入汉字和数字都可以识别并展示 <body> <h2 id="h2">二维码生成</h2> <br> <span id= ...

  9. asp.net生成店铺推广二维码,二维码中间加logo(源码)

    二维条码比一维条码记载数据量更多,二维码条码是一种高密度.高信息含量的便携式数据文件,是实现证件及卡片等大容量.高可靠性信息自动存储.携带并可用机器自动识读的理想手段.而且可以记载更复杂的数据,比如图 ...

随机推荐

  1. 一般处理程序使用Session的方法

    1 引用这个命名空间 using System.Web.SessionState; 2 实现这两个接口中的任何一个 IReadOnlySessionState   //此接口只能使用session,无 ...

  2. Java基础——常用类(Date、File)以及包装类

    本文要点: 基本数据类型的包装类 字符串相关类: 不可变字符序列:String 可变字符序列:StringBuffer.StringBuilder 时间处理相关类: Date DateFormat.S ...

  3. iOS符号表

    https://docs.bugtags.com/zh/symbols/ios/find.html 发红包的限制 1.发送频率规则 ◆ 每分钟发送红包数量不得超过1800个: ◆ 同一个商户号,每分钟 ...

  4. my ambition

    学好java基础,不用学swing.学完java之后学my circle,之后学jsp,装好linux系统,天天打代码,在当寒假之前一定要学完并熟练运用java.java scrip,straw pu ...

  5. 记32位程序(使用3gb用户虚拟内存)使用D3DX9导致的一个崩溃的问题

    为了增加32位程序的用户虚拟内存的使用量,我们使用了/LARGEADDRESSAWARE编译选项来使32位程序可能使用到3gb的内存,能否使用到3gb内存也跟平台.系统和设置有关系,现摘抄部分作为参考 ...

  6. mysql -prompt选项

    使用-pormpt修改提示符.可以在登录时或者在登录后使用prompt选项来修改提示符 (1)使用mysql命令行参数修改提示符 # mysql -u root -p Enter password: ...

  7. 常用<meta>标签

    页面关键词 <meta name="keywords" content="your tags" /> 页面描述 <meta name=&quo ...

  8. windows远程控制ubuntu尝试--未成功

    按照百度知道上的步骤一步一步操作,下载xrdp,一切很顺利. 直至出现了,如下的語言: connecting to sesman ip 尝试找到原因第一个原因 第二次的分析解释 至今还没找出原因

  9. 数据库调优过程(二):找到IO不存在问题,而是sqlserver单表写入IO瓶颈

    物理机上测试IO是否为瓶颈: 使用一个死循环insert into测试数据库最大写入速度: use [iTest]; declare @index int; ; begin ; INSERT into ...

  10. 解决多线程调用sql存储过程问题

    场景: 我们程序现在改成多线程了,我现在需要把临时表中的数据给插入到TABLE_M中,但这时候可能其他的线程也在插入,我就不能用之前我们的方案了(select max(oid) from Tuning ...