注意:这里我不提供所需jar包的路径,我会把所有引用的jar包显示出来,大家自行Google

  1. package com.net.util;
  2.  
  3. import java.awt.BasicStroke;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Image;
  7. import java.awt.Shape;
  8. import java.awt.geom.RoundRectangle2D;
  9. import java.awt.image.BufferedImage;
  10. import java.io.File;
  11. import java.io.OutputStream;
  12. import java.util.Hashtable;
  13. import java.util.Random;
  14.  
  15. import javax.imageio.ImageIO;
  16.  
  17. import com.google.zxing.BarcodeFormat;
  18. import com.google.zxing.BinaryBitmap;
  19. import com.google.zxing.DecodeHintType;
  20. import com.google.zxing.EncodeHintType;
  21. import com.google.zxing.MultiFormatReader;
  22. import com.google.zxing.MultiFormatWriter;
  23. import com.google.zxing.Result;
  24. import com.google.zxing.common.BitMatrix;
  25. import com.google.zxing.common.HybridBinarizer;
  26. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  27. /********************************************************************************
  28. * Description: 二维码制作工具类
  29. * @author zhangdi
  30. * @version 1.0
  31. */
  32. public class QRCodeUtil {
  33. private static final String CHARSET = "utf-8";
  34. private static final String FORMAT_NAME = "JPG";
  35. // 二维码尺寸
  36. private static final int QRCODE_SIZE = 300;
  37. // LOGO宽度
  38. private static final int WIDTH = 60;
  39. // LOGO高度
  40. private static final int HEIGHT = 60;
  41.  
  42. /**
  43. * 生成二维码
  44. * @param content 源内容
  45. * @param imgPath 生成二维码保存的路径
  46. * @param needCompress 是否要压缩
  47. * @return 返回二维码图片
  48. * @throws Exception
  49. */
  50. private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception {
  51. Hashtable hints = new Hashtable();
  52. hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
  53. hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
  54. hints.put(EncodeHintType.MARGIN, 1);
  55. BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,
  56. hints);
  57. int width = bitMatrix.getWidth();
  58. int height = bitMatrix.getHeight();
  59. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  60. for (int x = 0; x < width; x++) {
  61. for (int y = 0; y < height; y++) {
  62. image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
  63. }
  64. }
  65. if (imgPath == null || "".equals(imgPath)) {
  66. return image;
  67. }
  68. // 插入图片
  69. QRCodeUtil.insertImage(image, imgPath, needCompress);
  70. return image;
  71. }
  72.  
  73. /**
  74. * 在生成的二维码中插入图片
  75. * @param source
  76. * @param imgPath
  77. * @param needCompress
  78. * @throws Exception
  79. */
  80. private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception {
  81. File file = new File(imgPath);
  82. if (!file.exists()) {
  83. System.err.println("" + imgPath + " 该文件不存在!");
  84. return;
  85. }
  86. Image src = ImageIO.read(new File(imgPath));
  87. int width = src.getWidth(null);
  88. int height = src.getHeight(null);
  89. if (needCompress) { // 压缩LOGO
  90. if (width > WIDTH) {
  91. width = WIDTH;
  92. }
  93. if (height > HEIGHT) {
  94. height = HEIGHT;
  95. }
  96. Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
  97. BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  98. Graphics g = tag.getGraphics();
  99. g.drawImage(image, 0, 0, null); // 绘制缩小后的图
  100. g.dispose();
  101. src = image;
  102. }
  103. // 插入LOGO
  104. Graphics2D graph = source.createGraphics();
  105. int x = (QRCODE_SIZE - width) / 2;
  106. int y = (QRCODE_SIZE - height) / 2;
  107. graph.drawImage(src, x, y, width, height, null);
  108. Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
  109. graph.setStroke(new BasicStroke(3f));
  110. graph.draw(shape);
  111. graph.dispose();
  112. }
  113.  
  114. /**
  115. * 生成带logo二维码,并保存到磁盘
  116. * @param content
  117. * @param imgPath logo图片
  118. * @param destPath
  119. * @param needCompress
  120. * @throws Exception
  121. */
  122. public static String encode(String content, String imgPath, String destPath, boolean needCompress) throws Exception {
  123. BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);
  124. mkdirs(destPath);
  125. String file = new Random().nextInt(99999999) + ".jpg";//生成随机文件名
  126. ImageIO.write(image, FORMAT_NAME, new File(destPath + "/" + file));
  127. return file;
  128. }
  129.  
  130. public static void mkdirs(String destPath) {
  131. File file = new File(destPath);
  132. // 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir。(mkdir如果父目录不存在则会抛出异常)
  133. if (!file.exists() && !file.isDirectory()) {
  134. file.mkdirs();
  135. }
  136. }
  137.  
  138. public static void encode(String content, String imgPath, String destPath) throws Exception {
  139. QRCodeUtil.encode(content, imgPath, destPath, false);
  140. }
  141.  
  142. public static void encode(String content, String destPath, boolean needCompress) throws Exception {
  143. QRCodeUtil.encode(content, null, destPath, needCompress);
  144. }
  145.  
  146. public static void encode(String content, String destPath) throws Exception {
  147. QRCodeUtil.encode(content, null, destPath, false);
  148. }
  149.  
  150. public static void encode(String content, String imgPath, OutputStream output, boolean needCompress)
  151. throws Exception {
  152. BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);
  153. ImageIO.write(image, FORMAT_NAME, output);
  154. }
  155.  
  156. public static void encode(String content, OutputStream output) throws Exception {
  157. QRCodeUtil.encode(content, null, output, false);
  158. }
  159.  
  160. /**
  161. * 从二维码中,解析数据
  162. * @param file 二维码图片文件
  163. * @return 返回从二维码中解析到的数据值
  164. * @throws Exception
  165. */
  166. public static String decode(File file) throws Exception {
  167. BufferedImage image;
  168. image = ImageIO.read(file);
  169. if (image == null) {
  170. return null;
  171. }
  172. BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
  173. BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
  174. Result result;
  175. Hashtable hints = new Hashtable();
  176. hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
  177. result = new MultiFormatReader().decode(bitmap, hints);
  178. String resultStr = result.getText();
  179. return resultStr;
  180. }
  181.  
  182. public static String decode(String path) throws Exception {
  183. return QRCodeUtil.decode(new File(path));
  184. }
  185. // try {
  186. ////String text = "http://weifeng.nethangzhou.com/wf/weixin/getUserInfoCode";
  187. //String text = "http://www.lechuan100.com/plugin.php?id=haven_article&mod=my&aid=21";
  188. ////生成带logo 的二维码
  189. ////QRCodeUtil.encode(text, "d:/730.png", "d:/wfqrcode", true);
  190. ////生成不带logo 的二维码
  191. //QRCodeUtil.encode(text,"","d:/wfqrcode",true);
  192. //
  193. ////指定二维码图片,解析返回数据
  194. ////System.out.println(QRCodeUtil.decode("D:/WPS/75040887.jpg"));
  195. //} catch (Exception e) {
  196. //e.printStackTrace();
  197. //}
  198. }

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

  1. Java 二维码生成工具类

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

  2. 你不可错过的二维码生成与解析-java后台与前端js都有

    1.二维码分类   二维条码也有许多不同的码制,就码制的编码原理而言,通常分为三种类型. 线性堆叠式二维码 编码原理: 建立在一维条码基础之上,按需要堆积成两行或多行. 图示: 矩阵式二维码 最常用编 ...

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

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

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

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

  5. java二维码生成工具

    import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.ut ...

  6. iOS 原生实现生成二维码(CoreImage)工具类,不依赖第三方库,可自定义背景颜色,添加logo(Swift 4.0)

    import Foundation import CoreImage import UIKit extension UIColor { var coreImageColor: CIColor { re ...

  7. 开发ASP.NET MVC 开发名片二维码生成工具 (原创)

    在网上找了很多,都只能生成网址,不能生成名片二维码,于是自己动手. 第一步,写视图界面,主要代码如下: <script type="text/javascript"> ...

  8. 二维码生成工具——QRCode

    下载QRCode的源代码:https://github.com/davidshimjs/qrcodejs 引入项目中:<script type="text/javascript&quo ...

  9. 把url链接转换成二维码的工具类

    import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io ...

随机推荐

  1. AtCoder Beginner Contest 132

    目录 Contest Info Solutions A. Fifty-Fifty B. Ordinary Number C. Divide the Problems D. Blue and Red B ...

  2. 异步时钟FIFO(一)

    FIFO一般用于通过两个不同时钟域的数据传输.一个水池有进和出两个通道,由于进出口水流不一致所以需要水池加以缓冲.堆栈也是相当于水池的作用.如果输入端不是连续的数据流,可以通过堆栈来调节使数据以稳定的 ...

  3. Selenium结合BeautifulSoup4编写简单爬虫

    在学会了抓包,接口请求(如requests库)和Selenium的一些操作方法后,基本上就可以编写爬虫,爬取绝大多数网站的内容. 在爬虫领域,Selenium永远是最后一道防线.从本质上来说,访问网页 ...

  4. UOJ #164 [清华集训2015]V (线段树)

    题目链接 http://uoj.ac/problem/164 题解 神仙线段树题. 首先赋值操作可以等价于减掉正无穷再加上\(x\). 假设某个位置从前到后的操作序列是: \(x_1,x_2,..., ...

  5. Linux设备驱动程序 之 get_free_page

    get_free_page 如果模块需要分配大块的内存,使用面向页的分配会有很多优点: 分配页面可使用下面的函数: unsigned long get_zeroed_page(gfp_t gfp_ma ...

  6. ArcGIS Python 唯一值专题

    import arcpy mxd = arcpy.mapping.MapDocument("current") lyr = arcpy.mapping.ListLayers(mxd ...

  7. [GIT]比较不同分支的差异

        比如我们有 2 个分支:master, dev,现在想查看这两个 branch 的区别,有以下几种方式: undefined 1.查看 dev 有,而 master 中没有的: 1.查看 de ...

  8. ForkJoinPool 源码分析

    ForkJoinPool ForkJoinPool 是一个运行 ForkJoinTask 任务.支持工作窃取和并行计算的线程池 核心参数+创建实例 // 工作者线程驻留任务队列索引位 static f ...

  9. linux:解决SSH连接Linux超时自动断开

    用SSH登录到Linux的时候,由于默认的连接超时时间很短,经常断开! 1.修改文件 # vi /etc/ssh/sshd_config 2.重启sshd服务 # /etc/init.d/sshd r ...

  10. mongodb download

    https://www.mongodb.org/dl/win32/x86_64-2008plus-ssl