http://www.ostools.net/qr看到了一个生成二维码的工具,于是就产生了一个想法:

为什么自己不做一个二维码的生成和解析工具呢?花了一个多钟的时间,嘿嘿,就做出来啦...

先来看看效果图吧:

CODE_QR:

 CODE_128:        

PDF_417:

二维码的意思是:

下面是操作步骤:

一:下载zxing的压缩包:

可以到这里下载:http://code.google.com/p/zxing/downloads/list

ZXing-2.1.zip:http://code.google.com/p/zxing/downloads/detail?name=ZXing-2.1.zip&can=2&q=

得到:

zxing-2.1\core\core.jar

zxing-2.1\javase\javase.jar

二:把他添加进入你的项目的里面:

/QRCodes/src/com/b510/qrcode/QRCode.java

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

是不是很简单.....

源码下载:http://files.cnblogs.com/hongten/QRCodes.rar

java 二维码的更多相关文章

  1. Atitit java 二维码识别 图片识别

    Atitit java 二维码识别 图片识别 1.1. 解码11.2. 首先,我们先说一下二维码一共有40个尺寸.官方叫版本Version.11.3. 二维码的样例:21.4. 定位图案21.5. 数 ...

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

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

  3. Java二维码生成与解码

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

  4. [转]java二维码生成与解析代码实现

    转载地址:点击打开链接 二维码,是一种采用黑白相间的平面几何图形通过相应的编码算法来记录文字.图片.网址等信息的条码图片.如下图 二维码的特点: 1.  高密度编码,信息容量大 可容纳多达1850个大 ...

  5. java二维码生成与解析代码实现

    TwoDimensionCode类:二维码操作核心类 package qrcode; import java.awt.Color; import java.awt.Graphics2D; import ...

  6. Java二维码登录流程实现(包含短地址生成,含部分代码)

    近年来,二维码的使用越来越风生水起,笔者最近手头也遇到了一个需要使用二维码扫码登录网站的活,所以研究了一下这一套机制,并用代码实现了整个流程,接下来就和大家聊聊二维码登录及的那些事儿. 二维码原理 二 ...

  7. java二维码开发

    之前就写过很多关于二维码的东西,一直没有时间整理一下,所以呢今天就先来介绍一下如何利用java开发二维码.生成二维码有很多jar包可以实现,例如Zxing,QRcode,前者是谷歌的,后者日本的,这里 ...

  8. java二维码生成

    import java.io.File; import java.nio.file.Path; import java.util.HashMap; import com.google.zxing.Ba ...

  9. java二维码生成代码

    QRCodeUtil.encode(text, "D:/004.jpg", "D:", true, "exp");// 这个方法的第一个参数 ...

随机推荐

  1. 洛谷 U4704 函数

    设gcd(a,b)为a和b的最大公约数,xor(a,b)为a异或b的结果. 题目描述 kkk总是把gcd写成xor.今天数学考试恰好出到了gcd(a,b)=?这样的题目,但是kkk全部理解成了xor( ...

  2. PAT (Advanced Level) 1045. Favorite Color Stripe (30)

    最长公共子序列变形. #include<iostream> #include<cstring> #include<cmath> #include<algori ...

  3. 谈谈java的BlockingQueue

    http://www.cnblogs.com/archy_yu/archive/2013/04/19/3018479.html 博客园 首页 新随笔 联系 管理 随笔- 92  文章- 0  评论- ...

  4. POP音原因

    一,通话时调节音量,有POP音. POP音产生原因在于,音量变化太大导致有POP音,需要以淡入淡出的方式调节音量.请申请MOLY00108114 & MOLY00108143这两个Modem ...

  5. [iOS]C语言技术视频-10-指针变量

    下载地址: 链接: http://pan.baidu.com/s/1jGjbaXg 密码: u2t9

  6. openstack controller ha测试环境搭建记录(一)——操作系统准备

    为了初步了解openstack controller ha的工作原理,搭建测试环境进行学习. 在学习该方面知识时,当前采用的操作系统版本是centos 7.1 x64.首先在ESXi中建立2台用于测试 ...

  7. OC与Swift创建pod

    Cocoa pods 是iOS最常用的类库管理工具   OC的使用   删除源   sudo gem sources -r https://rubygems.org/ 添加源(使用淘宝的镜像,记住要用 ...

  8. java中关于编码的问题(字符转换流及字符缓冲流 )

    上次我们使用的是字节流,还有一种方式就是字符流,上次说过如何分辨使用哪种流,如果记事本可以读懂则使用字符流,否则使用字节流.使用字符流就需要牵扯到编码的问题,下面给出一种转化流的格式. OutputS ...

  9. html5新增标签集锦

    <keygen></keygen><meter low="69" high="80" max="100" op ...

  10. HTML 5终于定稿,八年后我们再一次谈谈怎么改变世界

    我们第一次谈论 HTML5 要改变世界大概是因为乔布斯,他坚持在 iOS 上不兼容 Flash,在 Adobe 统治多媒体开发的那个年代,这需要付出极大的勇气.这么多年过去了,虽然所有人都在谈论 HT ...