ZXing 生成二维码 QRCode 和条码 CODE128

ZXing 生成二维码 QRCode 和条码 CODE128 和 ZXing 解析或读取 QR_Code 和 条码 CODE_128

2019年02月20日 01:15:24 ___NULL 阅读数 736更多

分类专栏: 所有 Java 算法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/joyous/article/details/87748303

二维码是国际标准,由日本某公司发明,并保留版权,免费让全世界使用,目前在中国金融支付领域大放异彩。

条码的联合发明人诺曼·约瑟夫·伍德兰德(Norman Joseph Woodland)于上世纪70年代发明了条形码,这一技术的发明几乎对全球商业活动产生了一场革命,并为消费者在超市的购物节约了大量时间。日前在自己位于新泽西的家中不幸去世,享年91岁。

二维码和条码的生成使用 zxing,zxing 是开源的条码及二维码生成框架,https://github.com/zxing/zxing/ 免费获取,目前版本 ver 3.3.3。

以下是测试生成的代码,内容将保存在 c:\temp\a.png 和 c:\temp\b.png 两个图片文件内。

 
  1.  
  2. /*

  3. * 2019-2-20

  4. * 测试 zxing 3.3.3 二维码和条码生成

  5. */

  6.  
  7. import java.io.BufferedOutputStream;

  8. import java.io.File;

  9. import java.io.FileOutputStream;

  10. import java.io.IOException;

  11. import java.util.Hashtable;

  12.  
  13. import com.google.zxing.BarcodeFormat;

  14. import com.google.zxing.EncodeHintType;

  15. import com.google.zxing.MultiFormatWriter;

  16. import com.google.zxing.WriterException;

  17. import com.google.zxing.client.j2se.MatrixToImageWriter;

  18. import com.google.zxing.common.BitMatrix;

  19. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

  20.  
  21. public class WriteQRCode

  22. {

  23.  
  24. public static void main(String[] args)

  25. {

  26. int width = 200;

  27. int height = 200;

  28.  
  29. // 写 二维码

  30. TestWriteRCode("c:/temp/a.png", BarcodeFormat.QR_CODE, "二维码文字信息, Hello world", width, height);

  31.  
  32. // 写条码

  33. height = 50;

  34. TestWriteRCode("c:/temp/b.png", BarcodeFormat.CODE_128, "ABC123456abc", width, height);

  35. }

  36.  
  37. /**

  38. * 将字符串信息写入 QRCode

  39. *

  40. * @param fileName

  41. * 指定文件名

  42. * @param strContents

  43. * 写入的内容

  44. * @param width

  45. * 尺寸宽

  46. * @param height

  47. * 尺寸高

  48. */

  49. public static void TestWriteRCode(String fileName, BarcodeFormat typeCode, String strContents, int width, int height)

  50. {

  51. Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();

  52. // 字符集

  53. hints.put(EncodeHintType.CHARACTER_SET, "GBK");// Constant.CHARACTER);

  54. // 容错质量

  55. hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);

  56.  
  57. try

  58. {

  59. // 尺寸

  60. BitMatrix bitMatrix = new MultiFormatWriter().encode(strContents, typeCode, width, height, hints);

  61. BufferedOutputStream buffer = null;

  62. buffer = new BufferedOutputStream(new FileOutputStream(fileName));

  63. // ok

  64. MatrixToImageWriter.writeToStream(bitMatrix, "png", new FileOutputStream(new File(fileName)));

  65. buffer.close();

  66. }

  67. catch (WriterException e)

  68. {

  69. e.printStackTrace();

  70. }

  71. catch (IOException e)

  72. {

  73. e.printStackTrace();

  74. }

  75.  
  76. }

  77. }

生成成功将得到两个 png 文件。

以下是读取 c:\temp\a.png 和 c:\temp\b.png 内的条码及二维码。

 
  1.  
  2. /*

  3. * 2019-2-20

  4. * 测试 zxing 3.3.3 二维码和条码读取

  5. */

  6.  
  7. import java.awt.image.BufferedImage;

  8. import java.io.File;

  9. import java.io.IOException;

  10. import java.util.ArrayList;

  11. import java.util.Arrays;

  12. import java.util.Collection;

  13. import java.util.EnumMap;

  14. import java.util.EnumSet;

  15. import java.util.Map;

  16.  
  17. import javax.imageio.ImageIO;

  18.  
  19. import com.google.zxing.BarcodeFormat;

  20. import com.google.zxing.BinaryBitmap;

  21. import com.google.zxing.ChecksumException;

  22. import com.google.zxing.DecodeHintType;

  23. import com.google.zxing.FormatException;

  24. import com.google.zxing.LuminanceSource;

  25. import com.google.zxing.MultiFormatReader;

  26. import com.google.zxing.NotFoundException;

  27. import com.google.zxing.Reader;

  28. import com.google.zxing.ReaderException;

  29. import com.google.zxing.Result;

  30. import com.google.zxing.client.j2se.BufferedImageLuminanceSource;

  31. import com.google.zxing.client.j2se.ImageReader;

  32. import com.google.zxing.common.GlobalHistogramBinarizer;

  33. import com.google.zxing.common.HybridBinarizer;

  34. import com.google.zxing.multi.GenericMultipleBarcodeReader;

  35. import com.google.zxing.multi.MultipleBarcodeReader;

  36.  
  37. public class ReadCode

  38. {

  39. private static final Map<DecodeHintType, Object> HINTS;

  40. private static final Map<DecodeHintType, Object> HINTS_PURE;

  41.  
  42. static

  43. {

  44. HINTS = new EnumMap<>(DecodeHintType.class);

  45. HINTS.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);

  46. HINTS.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.allOf(BarcodeFormat.class));

  47. HINTS_PURE = new EnumMap<>(HINTS);

  48. HINTS_PURE.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);

  49. }

  50.  
  51. public static void main(String[] args)

  52. {

  53. BufferedImage image = null;

  54. try

  55. {

  56. image = ImageIO.read(new File("c:/temp/a.jpg"));

  57. ProcessImage(image);

  58.  
  59. image = ImageIO.read(new File("c:/temp/b.png"));

  60. ProcessImage(image);

  61. }

  62. catch (IOException e)

  63. {

  64. }

  65.  
  66. }

  67.  
  68. private static void ProcessImage(BufferedImage image)

  69. {

  70. LuminanceSource source = new BufferedImageLuminanceSource(image);

  71. BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));

  72. Collection<Result> results = new ArrayList<>(1);

  73.  
  74. try

  75. {

  76.  
  77. Reader reader = new MultiFormatReader();

  78. ReaderException savedException = null;

  79. try

  80. {

  81. // Look for multiple barcodes

  82. MultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);

  83. Result[] theResults = multiReader.decodeMultiple(bitmap, HINTS);

  84. if (theResults != null)

  85. {

  86. results.addAll(Arrays.asList(theResults));

  87. }

  88. }

  89. catch (ReaderException re)

  90. {

  91. savedException = re;

  92. }

  93.  
  94. if (results.isEmpty())

  95. {

  96. try

  97. {

  98. // Look for pure barcode

  99. Result theResult = reader.decode(bitmap, HINTS_PURE);

  100. if (theResult != null)

  101. {

  102. results.add(theResult);

  103. }

  104. }

  105. catch (ReaderException re)

  106. {

  107. savedException = re;

  108. }

  109. }

  110.  
  111. if (results.isEmpty())

  112. {

  113. try

  114. {

  115. // Look for normal barcode in photo

  116. Result theResult = reader.decode(bitmap, HINTS);

  117. if (theResult != null)

  118. {

  119. results.add(theResult);

  120. }

  121. }

  122. catch (ReaderException re)

  123. {

  124. savedException = re;

  125. }

  126. }

  127.  
  128. if (results.isEmpty())

  129. {

  130. try

  131. {

  132. // Try again with other binarizer

  133. BinaryBitmap hybridBitmap = new BinaryBitmap(new HybridBinarizer(source));

  134. Result theResult = reader.decode(hybridBitmap, HINTS);

  135. if (theResult != null)

  136. {

  137. results.add(theResult);

  138. }

  139. }

  140. catch (ReaderException re)

  141. {

  142. savedException = re;

  143. }

  144. }

  145.  
  146. if (results.isEmpty())

  147. {

  148. try

  149. {

  150. throw savedException == null ? NotFoundException.getNotFoundInstance() : savedException;

  151. }

  152. catch (FormatException | ChecksumException e)

  153. {

  154. // log.info(e.toString());

  155. }

  156. catch (ReaderException e)

  157. { // Including NotFoundException

  158. // log.info(e.toString());

  159. }

  160. return;

  161. }

  162.  
  163. for (Result result : results)

  164. {

  165. System.out.println(result.getText());

  166. }

  167.  
  168. }

  169. catch (RuntimeException re)

  170. {

  171. // Call out unexpected errors in the log clearly

  172. // log.log(Level.WARNING, "Unexpected exception from library", re);

  173. }

  174. }

  175.  
  176. }

运行读取测试程序,将输出相应的内容。

这里顺便提一下,若二维码或者条码在一个含有表格、文字的富文本文档转换的图片中,也是可以被识别出来的,但会略微消耗一点时间。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值