1. import java.awt.image.BufferedImage;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.Hashtable;
  5. import java.util.Map;
  6.  
  7. import javax.imageio.ImageIO;
  8.  
  9. import com.google.zxing.BarcodeFormat;
  10. import com.google.zxing.BinaryBitmap;
  11. import com.google.zxing.DecodeHintType;
  12. import com.google.zxing.EncodeHintType;
  13. import com.google.zxing.LuminanceSource;
  14. import com.google.zxing.MultiFormatReader;
  15. import com.google.zxing.MultiFormatWriter;
  16. import com.google.zxing.ReaderException;
  17. import com.google.zxing.Result;
  18. import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
  19. import com.google.zxing.common.BitMatrix;
  20. import com.google.zxing.common.HybridBinarizer;
  21.  
  22. /**
  23. * 利用zxing开源工具生成二维码QRCode
  24. *
  25. * @date 2012-10-26
  26. * @author xhw
  27. *
  28. */
  29. public class QRCode {
  30. private static final int BLACK = 0xff000000;
  31. private static final int WHITE = 0xFFFFFFFF;
  32.  
  33. /**
  34. * @param args
  35. */
  36. public static void main(String[] args) {
  37.  
  38. /**
  39. * 在com.google.zxing.MultiFormatWriter类中,定义了一些我们不知道的码,二维码只是其中的一种<br>
  40. * public BitMatrix encode(String contents,
  41. BarcodeFormat format,
  42. int width, int height,
  43. Map<EncodeHintType,?> hints) throws WriterException {
  44. Writer writer;
  45. switch (format) {
  46. case EAN_8:
  47. writer = new EAN8Writer();
  48. break;
  49. case EAN_13:
  50. writer = new EAN13Writer();
  51. break;
  52. case UPC_A:
  53. writer = new UPCAWriter();
  54. break;
  55. case QR_CODE:
  56. writer = new QRCodeWriter();
  57. break;
  58. case CODE_39:
  59. writer = new Code39Writer();
  60. break;
  61. case CODE_128:
  62. writer = new Code128Writer();
  63. break;
  64. case ITF:
  65. writer = new ITFWriter();
  66. break;
  67. case PDF_417:
  68. writer = new PDF417Writer();
  69. break;
  70. case CODABAR:
  71. writer = new CodaBarWriter();
  72. break;
  73. default:
  74. throw new IllegalArgumentException("No encoder available for format " + format);
  75. }
  76. return writer.encode(contents, format, width, height, hints);
  77. }
  78.  
  79. */
  80. String filePostfix="png";
  81. File file = new File("C://test_QR_CODE."+filePostfix);
  82. QRCode.encode("http://www.cnblogs.com/sprinng", file,filePostfix, BarcodeFormat.QR_CODE, 500, 500, null);
  83. QRCode.decode(file);
  84. }
  85.  
  86. /**
  87. * 生成QRCode二维码<br>
  88. * 在编码时需要将com.google.zxing.qrcode.encoder.Encoder.java中的<br>
  89. * static final String DEFAULT_BYTE_MODE_ENCODING = "ISO8859-1";<br>
  90. * 修改为UTF-8,否则中文编译后解析不了<br>
  91. * @param contents 二维码的内容
  92. * @param file 二维码保存的路径,如:C://test_QR_CODE.png
  93. * @param filePostfix 生成二维码图片的格式:png,jpeg,gif等格式
  94. * @param format qrcode码的生成格式
  95. * @param width 图片宽度
  96. * @param height 图片高度
  97. * @param hints
  98. */
  99. public static void encode(String contents, File file,String filePostfix, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) {
  100. try {
  101. BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, format, width, height);
  102. writeToFile(bitMatrix, filePostfix, file);
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. }
  106. }
  107.  
  108. /**
  109. * 生成二维码图片<br>
  110. *
  111. * @param matrix
  112. * @param format
  113. * 图片格式
  114. * @param file
  115. * 生成二维码图片位置
  116. * @throws IOException
  117. */
  118. public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
  119. BufferedImage image = toBufferedImage(matrix);
  120. ImageIO.write(image, format, file);
  121. }
  122.  
  123. /**
  124. * 生成二维码内容<br>
  125. *
  126. * @param matrix
  127. * @return
  128. */
  129. public static BufferedImage toBufferedImage(BitMatrix matrix) {
  130. int width = matrix.getWidth();
  131. int height = matrix.getHeight();
  132. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  133. for (int x = 0; x < width; x++) {
  134. for (int y = 0; y < height; y++) {
  135. image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);
  136. }
  137. }
  138. return image;
  139. }
  140.  
  141. /**
  142. * 解析QRCode二维码
  143. */
  144. @SuppressWarnings("unchecked")
  145. public static void decode(File file) {
  146. try {
  147. BufferedImage image;
  148. try {
  149. image = ImageIO.read(file);
  150. if (image == null) {
  151. System.out.println("Could not decode image");
  152. }
  153. LuminanceSource source = new BufferedImageLuminanceSource(image);
  154. BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
  155. Result result;
  156. @SuppressWarnings("rawtypes")
  157. Hashtable hints = new Hashtable();
  158. //解码设置编码方式为:utf-8
  159. hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
  160. result = new MultiFormatReader().decode(bitmap, hints);
  161. String resultStr = result.getText();
  162. System.out.println("解析后内容:" + resultStr);
  163. } catch (IOException ioe) {
  164. System.out.println(ioe.toString());
  165. } catch (ReaderException re) {
  166. System.out.println(re.toString());
  167. }
  168. } catch (Exception ex) {
  169. System.out.println(ex.toString());
  170. }
  171. }
  172. }

  jar包下载

java二维码生成工具的更多相关文章

  1. Java 二维码生成工具类

    /** * 二维码 工具 * * @author Rubekid * */ public class QRcodeUtils { /** * 默认version */ public static fi ...

  2. Java二维码生成与解码工具Zxing使用

    Zxing是Google研发的一款非常好用的开放源代码的二维码生成工具,目前源码托管在github上,源码地址: https://github.com/zxing/zxing 可以看到Zxing库有很 ...

  3. java二维码生成-谷歌(Google.zxing)开源二维码生成学习及实例

    java二维码生成-谷歌(Google.zxing)开源二维码生成的实例及介绍   我们使用比特矩阵(位矩阵)的QR码编码在缓冲图片上画出二维码 实例有以下一个传入参数 OutputStream ou ...

  4. Java二维码生成与解码

      基于google zxing 的Java二维码生成与解码   一.添加Maven依赖(解码时需要上传二维码图片,所以需要依赖文件上传包) <!-- google二维码工具 --> &l ...

  5. 二维码生成工具类java版

    注意:这里我不提供所需jar包的路径,我会把所有引用的jar包显示出来,大家自行Google package com.net.util; import java.awt.BasicStroke; im ...

  6. 谷歌zxing 二维码生成工具

    一.加入maven依赖 <!-- 谷歌zxing 二维码 --> <dependency> <groupId>com.google.zxing</groupI ...

  7. java 二维码生成(可带图片)springboot版

    本文(2019年6月29日 飞快的蜗牛博客) 有时候,男人和女人是两个完全不同的世界,男人的玩笑和女人的玩笑也完全是两码事,爱的人完全不了解你,你也不要指望一个女人了解你,所以男的不是要求别人怎么样, ...

  8. vue项目条形码和二维码生成工具试用

    项目开发需要,优惠券分不同类型,简单的使用id生成条形码供店铺使用,麻烦点的需要多个字段的就需要使用二维码来展示了,对应的效果如下 条形码(一维码)使用工具code128 需引入code128.js ...

  9. java 二维码生成

    直接上代码: 二维码生成核心类: package com.bbkj.wechat.tool; import java.awt.image.BufferedImage; import java.io.F ...

随机推荐

  1. 使用Chrome快速实现数据的抓取(三)——JQuery

    使用Chrome抓取页面一个非常方便的地方就是它可以执行JS,也就是说我们可以通过JS函数获取我们想要的数据.一个非常强大易用的库就是Jquery,本文就简单的介绍一下使用Chrome获取数据时Jqu ...

  2. 【ButterKnife】 安卓程序猿的一大利器

    注:近期才看到的这个类库,来自于jakewharton大神的力作,安卓里面的视图注入库 另小弟水平有限,翻译的不好,还请多多指正 首先是地址(托管在github上):http://jakewharto ...

  3. Property Finder – a Cross-Platform Xamarin MonoTouch Mobile App

    Developers are now finding themselves having to author applications for a diverse range of mobile pl ...

  4. 聊聊JVM的年轻代(转)

    聊聊JVM的年轻代 本文转自http://ifeve.com/jvm-yong-generation/ 1.为什么会有年轻代 我们先来屡屡,为什么需要把堆分代?不分代不能完成他所做的事情么?其实不分代 ...

  5. win7无线网络共享

    一.最简单的方法: 1.使用360安全卫士 2.安装一个驱动人生 二.手工设置,参考:http://www.jb51.net/os/windows/63472.html

  6. js 设置焦点 判断控件是否获得焦点 判断哪个控件获得焦点

    设置焦点 <html> <head> <title>设置焦点</title> <mce:script language ="javasc ...

  7. jstl 处理字符串函数 substring spli等

    在jstl中的fn标签也是我们在网页设计中经常要用到的很关键的标签,在使用的时候要先加上头 <%@ taglib uri="http://java.sun.com/jsp/jstl/f ...

  8. [PHP] ubuntu16.04下 Phpstorm发布项目到apache

    reference to : http://blog.csdn.net/qq_23937195/article/details/72953308 在网上找的不靠谱,倒腾了大半天的,终于找到正确姿势QA ...

  9. Rotate List leetcode java

    题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example: Gi ...

  10. RS错误RSV-VAL-0032之项目未在布局中引用的3种解决办法

    如下图所示,我用RS新建了一个空白页面,拖入了一个列表,给该列表新建了一个条件样式 条件样式如下所示,表达式来自查询1 运行,报错如下图所示 原因就是条件样式使用到了查询1中的数据项1但是数据项1在报 ...