1. package com.mohe.twocode;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics2D;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10.  
  11. import javax.imageio.ImageIO;
  12.  
  13. import jp.sourceforge.qrcode.QRCodeDecoder;
  14. import jp.sourceforge.qrcode.data.QRCodeImage;
  15. import jp.sourceforge.qrcode.exception.DecodingFailedException;
  16.  
  17. import com.swetake.util.Qrcode;
  18.  
  19. public class TwoDimensionCode implements QRCodeImage {
  20.  
  21. private BufferedImage bufImg = null;
  22.  
  23. public TwoDimensionCode() {
  24.  
  25. }
  26.  
  27. public TwoDimensionCode(BufferedImage bufImg) {
  28. this.bufImg = bufImg;
  29. }
  30.  
  31. @Override
  32. public int getHeight() {
  33. return bufImg.getHeight();
  34. }
  35.  
  36. @Override
  37. public int getPixel(int x, int y) {
  38. return bufImg.getRGB(x, y);
  39. }
  40.  
  41. @Override
  42. public int getWidth() {
  43. return bufImg.getWidth();
  44. }
  45.  
  46. /**
  47. * 生成二维码(QRCode)图片
  48. *
  49. * @param content
  50. * 存储内容
  51. * @param imgPath
  52. * 图片路径
  53. */
  54. public void encoderQRCode(String content, String imgPath) {
  55. this.encoderQRCode(content, imgPath, "png", 7);
  56. }
  57.  
  58. /**
  59. * 生成二维码(QRCode)图片
  60. *
  61. * @param content
  62. * 存储内容
  63. * @param output
  64. * 输出流
  65. */
  66. public void encoderQRCode(String content, OutputStream output) {
  67. this.encoderQRCode(content, output, "png", 7);
  68. }
  69.  
  70. /**
  71. * 生成二维码(QRCode)图片
  72. *
  73. * @param content
  74. * 存储内容
  75. * @param imgPath
  76. * 图片路径
  77. * @param imgType
  78. * 图片类型
  79. */
  80. public void encoderQRCode(String content, String imgPath, String imgType) {
  81. this.encoderQRCode(content, imgPath, imgType, 7);
  82. }
  83.  
  84. /**
  85. * 生成二维码(QRCode)图片
  86. *
  87. * @param content
  88. * 存储内容
  89. * @param output
  90. * 输出流
  91. * @param imgType
  92. * 图片类型
  93. */
  94. public void encoderQRCode(String content, OutputStream output, String imgType) {
  95. this.encoderQRCode(content, output, imgType, 7);
  96. }
  97.  
  98. /**
  99. * 生成二维码(QRCode)图片
  100. *
  101. * @param content
  102. * 存储内容
  103. * @param imgPath
  104. * 图片路径
  105. * @param imgType
  106. * 图片类型
  107. * @param size
  108. * 二维码尺寸
  109. */
  110. public void encoderQRCode(String content, String imgPath, String imgType, int size) {
  111. try {
  112. BufferedImage bufImg = this.qRCodeCommon(content, imgType, size);
  113.  
  114. File imgFile = new File(imgPath);
  115.  
  116. if (!imgFile.exists()) {
  117. imgFile.mkdirs();
  118. }
  119.  
  120. // 生成二维码QRCode图片
  121. ImageIO.write(bufImg, imgType, imgFile);
  122.  
  123. } catch (Exception e) {
  124. e.printStackTrace();
  125. }
  126. }
  127.  
  128. /**
  129. * 生成二维码(QRCode)图片
  130. *
  131. * @param content
  132. * 存储内容
  133. * @param output
  134. * 输出流
  135. * @param imgType
  136. * 图片类型
  137. * @param size
  138. * 二维码尺寸
  139. */
  140. public void encoderQRCode(String content, OutputStream output, String imgType, int size) {
  141. try {
  142. BufferedImage bufImg = this.qRCodeCommon(content, imgType, size);
  143. // 生成二维码QRCode图片
  144. ImageIO.write(bufImg, imgType, output);
  145. } catch (Exception e) {
  146. e.printStackTrace();
  147. }
  148. }
  149.  
  150. /**
  151. * 生成二维码(QRCode)图片的公共方法
  152. *
  153. * @param content
  154. * 存储内容
  155. * @param imgType
  156. * 图片类型
  157. * @param size
  158. * 二维码尺寸
  159. * @return
  160. */
  161. private BufferedImage qRCodeCommon(String content, String imgType, int size) {
  162. BufferedImage bufImg = null;
  163. try {
  164. Qrcode qrcodeHandler = new Qrcode();
  165. // 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
  166. qrcodeHandler.setQrcodeErrorCorrect('M');
  167. qrcodeHandler.setQrcodeEncodeMode('B');
  168. // 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大
  169. qrcodeHandler.setQrcodeVersion(size);
  170. // 获得内容的字节数组,设置编码格式
  171. byte[] contentBytes = content.getBytes("utf-8");
  172. // 图片尺寸
  173. int imgSize = 67 + 12 * (size - 1);
  174. bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);
  175. Graphics2D gs = bufImg.createGraphics();
  176. // 设置背景颜色
  177. gs.setBackground(Color.WHITE);
  178. gs.clearRect(0, 0, imgSize, imgSize);
  179.  
  180. // 设定图像颜色> BLACK
  181. gs.setColor(Color.BLACK);
  182. // 设置偏移量,不设置可能导致解析出错
  183. int pixoff = 2;
  184. // 输出内容> 二维码
  185. if (contentBytes.length > 0 && contentBytes.length < 800) {
  186. boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
  187. for (int i = 0; i < codeOut.length; i++) {
  188. for (int j = 0; j < codeOut.length; j++) {
  189. if (codeOut[j][i]) {
  190. gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
  191. }
  192. }
  193. }
  194. } else {
  195. throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 800].");
  196. }
  197. gs.dispose();
  198. bufImg.flush();
  199. } catch (Exception e) {
  200. e.printStackTrace();
  201. }
  202. return bufImg;
  203. }
  204.  
  205. /**
  206. * 解析二维码(QRCode)
  207. *
  208. * @param imgPath
  209. * 图片路径
  210. * @return
  211. */
  212. public String decoderQRCode(String imgPath) {
  213. // QRCode 二维码图片的文件
  214. File imageFile = new File(imgPath);
  215. BufferedImage bufImg = null;
  216. String content = null;
  217. try {
  218. bufImg = ImageIO.read(imageFile);
  219. QRCodeDecoder decoder = new QRCodeDecoder();
  220. content = new String(decoder.decode(new TwoDimensionCode(bufImg)), "utf-8");
  221. } catch (IOException e) {
  222. System.out.println("Error: " + e.getMessage());
  223. e.printStackTrace();
  224. } catch (DecodingFailedException dfe) {
  225. System.out.println("Error: " + dfe.getMessage());
  226. dfe.printStackTrace();
  227. }
  228. return content;
  229. }
  230.  
  231. /**
  232. * 解析二维码(QRCode)
  233. *
  234. * @param input
  235. * 输入流
  236. * @return
  237. */
  238. public String decoderQRCode(InputStream input) {
  239. BufferedImage bufImg = null;
  240. String content = null;
  241. try {
  242. bufImg = ImageIO.read(input);
  243. QRCodeDecoder decoder = new QRCodeDecoder();
  244. content = new String(decoder.decode(new TwoDimensionCode(bufImg)), "utf-8");
  245. } catch (IOException e) {
  246. System.out.println("Error: " + e.getMessage());
  247. e.printStackTrace();
  248. } catch (DecodingFailedException dfe) {
  249. System.out.println("Error: " + dfe.getMessage());
  250. dfe.printStackTrace();
  251. }
  252. return content;
  253. }
  254.  
  255. /**
  256. * 生成二维码
  257. *
  258. * @param filePath
  259. * @return
  260. */
  261. public static void encoder(String filePath, String deskCode) {
  262.  
  263. String imgPath = filePath + deskCode + ".png";
  264. TwoDimensionCode handler = new TwoDimensionCode();
  265. handler.encoderQRCode(deskCode, imgPath, "png");
  266. }
  267.  
  268. /**
  269. * 解析二维码
  270. *
  271. * @param filePath
  272. * @return
  273. */
  274. public static String decoder(String filePath, int deskType, String deskCode) {
  275.  
  276. String imgPath = filePath + deskCode + ".png";
  277. TwoDimensionCode handler = new TwoDimensionCode();
  278. return handler.decoderQRCode(imgPath);
  279. }
  280.  
  281. /**
  282. * 生成中间带LOG二维码(QRCode)图片
  283. *
  284. * @param content
  285. * 二维码图片的内容
  286. * @param imgPath
  287. * 生成二维码图片完整的路径
  288. * @param ccbpath
  289. * 二维码图片中间的logo路径
  290. */
  291. public static int createQRCode(String content, String imgPath, String ccbPath) {
  292.  
  293. try {
  294. Qrcode qrcodeHandler = new Qrcode();
  295. qrcodeHandler.setQrcodeErrorCorrect('M');
  296. qrcodeHandler.setQrcodeEncodeMode('B');
  297. qrcodeHandler.setQrcodeVersion(7);
  298.  
  299. // System.out.println(content);
  300. byte[] contentBytes = content.getBytes("utf-8");
  301. BufferedImage bufImg = new BufferedImage(140, 140, BufferedImage.TYPE_INT_RGB);
  302. Graphics2D gs = bufImg.createGraphics();
  303.  
  304. gs.setBackground(Color.WHITE);
  305. gs.clearRect(0, 0, 140, 140);
  306.  
  307. // 设定图像颜色 > BLACK
  308. // gs.setColor(new Color(255, 200, 0));
  309. gs.setColor(Color.BLACK);
  310.  
  311. // 设置偏移量 不设置可能导致解析出错
  312. int pixoff = 2;
  313.  
  314. // 输出内容 > 二维码
  315. if (contentBytes.length > 0 && contentBytes.length < 120) {
  316. boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
  317. for (int i = 0; i < codeOut.length; i++) {
  318. for (int j = 0; j < codeOut.length; j++) {
  319. if (codeOut[j][i]) {
  320. gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
  321. }
  322. }
  323. }
  324. } else {
  325. System.err.println("QRCode content bytes length = " + contentBytes.length + " not in [ 0,120 ]. ");
  326. return -1;
  327. }
  328. // Image img = ImageIO.read(new File(ccbPath));// 实例化一个Image对象。
  329. // gs.drawImage(img, 47, 54, null);
  330. // gs.setColor(Color.BLUE);
  331. // gs.setFont(new Font("微软雅黑", Font.BOLD, 18));
  332. // gs.drawString("交享越", 50, 70);
  333. // gs.dispose();
  334. bufImg.flush();
  335.  
  336. // 生成二维码QRCode图片
  337. File imgFile = new File(imgPath);
  338. ImageIO.write(bufImg, "png", imgFile);
  339.  
  340. } catch (Exception e) {
  341. e.printStackTrace();
  342. return -100;
  343. }
  344.  
  345. return 0;
  346. }
  347.  
  348. public static void main(String[] args) {
  349. // String imgPath = "F:/TDDOWNLOAD/Michael_QRCode.png";
  350. // String encoderContent = "123131";
  351. // TwoDimensionCode handler = new TwoDimensionCode();
  352. // handler.encoderQRCode(encoderContent, imgPath, "png");
  353. // // try {
  354. // // OutputStream output = new FileOutputStream(imgPath);
  355. // // handler.encoderQRCode(content, output);
  356. // // } catch (Exception e) {
  357. // // e.printStackTrace();
  358. // // }
  359. // System.out.println("========encoder success");
  360. //
  361. // String decoderContent = handler.decoderQRCode(imgPath);
  362. // System.out.println("解析结果如下:");
  363. // System.out.println(decoderContent);
  364. // System.out.println("========decoder success!!!");
  365.  
  366. // TwoDimensionCode
  367. // .createQRCode(
  368. // "http://jxy.misuland.com//jxy_manager//downLoadAction!downLoad.action?path=jxy_android.apk",
  369. // "F://jxy_android.png",
  370. // "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\image\\white.jpg");
  371. TwoDimensionCode.createQRCode("http://localhost:8080//jxy_manager//downLoadAction!downLoad.action?path=jxy_android.apk", "F://jxy_android.png", "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\image\\white.jpg");
  372. TwoDimensionCode.createQRCode("http://localhost:8080//jxy_manager//downLoadAction!downLoad.action?path=jxy_ios.apk", "F://jxy_ios.png", "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\image\\white.jpg");
  373. }
  374. }
  375.  
  376. 所需jar包可以在这个地址下载http://download.csdn.net/detail/daixinmei/5974269

JAVA生成解析二维码的更多相关文章

  1. java生成/解析二维码

    package a; import java.awt.BasicStroke; import java.awt.Graphics; import java.awt.Graphics2D; import ...

  2. 使用zxing生成解析二维码

    1. 前言 随着移动互联网的发展,我们经常在火车票.汽车票.快餐店.电影院.团购网站以及移动支付等各个场景下见到二维码的应用,可见二维码以经渗透到人们生活的各个方面.条码.二维码以及RFID被人们应用 ...

  3. Java使用ZXing生成/解析二维码图片

    ZXing是一种开源的多格式1D/2D条形码图像处理库,在Java中的实现.重点是在手机上使用内置摄像头来扫描和解码设备上的条码,而不与服务器通信.然而,该项目也可以用于对桌面和服务器上的条形码进行编 ...

  4. java代码解析二维码

    java代码解析二维码一般步骤 本文采用的是google的zxing技术进行解析二维码技术,解析二维码的一般步骤如下: 一.下载zxing-core的jar包: 二.创建一个BufferedImage ...

  5. Java生成艺术二维码也可以很简单

    原文点击: Quick-Media Java生成艺术二维码也可以很简单 现在二维码可以说非常常见了,当然我们见得多的一般是白底黑块,有的再中间加一个 logo,或者将二维码嵌在一张特定的背景中(比如微 ...

  6. Java生成微信二维码及logo二维码

    依赖jar包 二维码的实现有多种方法,比如 Google 的 zxing 和日本公司的 QrCode,本文以 QrCode 为例. QrCode.jar:https://pan.baidu.com/s ...

  7. Java 生成在线二维码 以Base64返回前端、或者写入到本地磁盘

    思路 现阶段遇到这样一个问题,在原有的产品上加入线下优惠券模式,用户领取优惠券以后,获取到一个唯一的ID作为领取凭证,但是在线下用扫码枪进行扫码的时候,总不能让人手动输入吧 于是乎就想出了一个办法,后 ...

  8. java 生成/解读 二维码

    package com.rails.util; import com.swetake.util.Qrcode; import jp.sourceforge.qrcode.QRCodeDecoder; ...

  9. APS.NET MVC4生成解析二维码简单Demo

    一.视图 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewpor ...

随机推荐

  1. Yii2 init 与 beforeAction 区别

    1.执行顺序 init > beforeAction 2.调用子函数时,两个函数都不会再次执行 3.返回值 init返回false继续执行,beforeAction停止执行 4.执行EXIT,全 ...

  2. 二、python的逻辑运算与数据类型

    .python的逻辑运算符 数学运算符 加:+   减:-  乘:*  除:/  取余:% 关系运算符 等于: ==  不等于: != 小于:< 大于:>     大于等于: >=  ...

  3. 在windows上安装和启动Elasticseach、Kibana

    写在前面的话:读书破万卷,编码如有神-------------------------------------------------------------------- 参考内容: <Ela ...

  4. [转]应用服务器ApacheSocketUnixthread

    HttpClient容易忽视的细节——连接关闭 博客分类: java   HttpClient client = new HttpClient(); HttpMethod method = new G ...

  5. PID控制原理和算法

    闭环控制是根据控制对象输出反馈来进行校正的控制方式,它是在测量出实际与计划发生偏差时,按定额或标准来进行纠正的.比如控制一个电机的转速,就得有一个测量转速的传感器,并将结果反馈到控制路线上.提到闭环控 ...

  6. 设计模式 - 观察者模式(Observer Pattern) Java内置 用法

    观察者模式(Observer Pattern) Java内置 用法 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26601659 ...

  7. 开源Word读写组件DocX介绍与入门

    来源:http://i.cnblogs.com/EditPosts.aspx?opt=1 读写Offic格式的文档,大家多少都有用到,可能方法也很多,组件有很多.这里不去讨论其他方法的优劣,只是向大家 ...

  8. crtmpserver配置文件详解

    Configuration file配置文件 The configuration file is actually a lua script which must contain an object ...

  9. Semaphore控制同时访问的线程个数countdownlatch等待多个线程执行完本身线程再执行

    Semaphore控制同时访问的线程个数countdownlatch等待多个线程执行完本身线程再执行 Semaphore控制同时访问的线程个数countdownlatch等待多个线程执行完本身线程再执 ...

  10. Executing a system tool

    Executing a system tool The following code example shows the execution of the Buffer tool from the A ...