众所周知,现在生活中二维码已经是无处不见。走在街道上,随处可见广告标语旁有二维码,手机上QQ,微信加个好友都能通过二维码的方式,我不知道是什么时候兴起的二维码浪潮,但是我知道,这在我小时候可是见不到的。小小的一个方形图片,却能存储着大量的信息,是不是不可思议。当然路边的二维码希望大家还是不要贪小便宜,随便扫二维码很可能给你手机内置了病毒,导致个人信息的泄露,切记切记!

话不多说,直接上代码。

生成带logo的二维码代码:

  1. 1 package com.lsd.barcode;
  2. 2
  3. 3 import java.awt.BasicStroke;
  4. 4 import java.awt.Graphics;
  5. 5 import java.awt.Graphics2D;
  6. 6 import java.awt.Image;
  7. 7 import java.awt.Shape;
  8. 8 import java.awt.geom.RoundRectangle2D;
  9. 9 import java.awt.image.BufferedImage;
  10. 10 import java.io.File;
  11. 11 import java.io.OutputStream;
  12. 12 import java.util.Hashtable;
  13. 13 import java.util.Random;
  14. 14 import javax.imageio.ImageIO;
  15. 15 import com.google.zxing.BarcodeFormat;
  16. 16 import com.google.zxing.BinaryBitmap;
  17. 17 import com.google.zxing.DecodeHintType;
  18. 18 import com.google.zxing.EncodeHintType;
  19. 19 import com.google.zxing.MultiFormatReader;
  20. 20 import com.google.zxing.MultiFormatWriter;
  21. 21 import com.google.zxing.Result;
  22. 22 import com.google.zxing.common.BitMatrix;
  23. 23 import com.google.zxing.common.HybridBinarizer;
  24. 24 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  25. 25
  26. 26 /**
  27. 27 * 二维码工具类
  28. 28 *
  29. 29 */
  30. 30 public class QRCodeUtil {
  31. 31
  32. 32 private static final String CHARSET = "utf-8";
  33. 33 private static final String FORMAT_NAME = "JPG";
  34. 34 // 二维码尺寸
  35. 35 private static final int QRCODE_SIZE = 300;
  36. 36 // LOGO宽度
  37. 37 private static final int WIDTH = 60;
  38. 38 // LOGO高度
  39. 39 private static final int HEIGHT = 60;
  40. 40
  41. 41 private static BufferedImage createImage(String content, String imgPath,
  42. 42 boolean needCompress) throws Exception {
  43. 43 Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
  44. 44 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
  45. 45 hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
  46. 46 hints.put(EncodeHintType.MARGIN, 1);
  47. 47 BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
  48. 48 BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
  49. 49 int width = bitMatrix.getWidth();
  50. 50 int height = bitMatrix.getHeight();
  51. 51 BufferedImage image = new BufferedImage(width, height,
  52. 52 BufferedImage.TYPE_INT_RGB);
  53. 53 for (int x = 0; x < width; x++) {
  54. 54 for (int y = 0; y < height; y++) {
  55. 55 image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000
  56. 56 : 0xFFFFFFFF);
  57. 57 }
  58. 58 }
  59. 59 if (imgPath == null || "".equals(imgPath)) {
  60. 60 return image;
  61. 61 }
  62. 62 // 插入图片
  63. 63 QRCodeUtil.insertImage(image, imgPath, needCompress);
  64. 64 return image;
  65. 65 }
  66. 66
  67. 67 /**
  68. 68 * 插入LOGO
  69. 69 *
  70. 70 * @param source
  71. 71 * 二维码图片
  72. 72 * @param imgPath
  73. 73 * LOGO图片地址
  74. 74 * @param needCompress
  75. 75 * 是否压缩
  76. 76 * @throws Exception
  77. 77 */
  78. 78 private static void insertImage(BufferedImage source, String imgPath,
  79. 79 boolean needCompress) throws Exception {
  80. 80 File file = new File(imgPath);
  81. 81 if (!file.exists()) {
  82. 82 System.err.println(""+imgPath+" 该文件不存在!");
  83. 83 return;
  84. 84 }
  85. 85 Image src = ImageIO.read(new File(imgPath));
  86. 86 int width = src.getWidth(null);
  87. 87 int height = src.getHeight(null);
  88. 88 if (needCompress) { // 压缩LOGO
  89. 89 if (width > WIDTH) {
  90. 90 width = WIDTH;
  91. 91 }
  92. 92 if (height > HEIGHT) {
  93. 93 height = HEIGHT;
  94. 94 }
  95. 95 Image image = src.getScaledInstance(width, height,
  96. 96 Image.SCALE_SMOOTH);
  97. 97 BufferedImage tag = new BufferedImage(width, height,
  98. 98 BufferedImage.TYPE_INT_RGB);
  99. 99 Graphics g = tag.getGraphics();
  100. 100 g.drawImage(image, 0, 0, null); // 绘制缩小后的图
  101. 101 g.dispose();
  102. 102 src = image;
  103. 103 }
  104. 104 // 插入LOGO
  105. 105 Graphics2D graph = source.createGraphics();
  106. 106 int x = (QRCODE_SIZE - width) / 2;
  107. 107 int y = (QRCODE_SIZE - height) / 2;
  108. 108 graph.drawImage(src, x, y, width, height, null);
  109. 109 Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
  110. 110 graph.setStroke(new BasicStroke(3f));
  111. 111 graph.draw(shape);
  112. 112 graph.dispose();
  113. 113 }
  114. 114
  115. 115 /**
  116. 116 * 生成二维码(内嵌LOGO)
  117. 117 *
  118. 118 * @param content
  119. 119 * 内容
  120. 120 * @param imgPath
  121. 121 * LOGO地址
  122. 122 * @param destPath
  123. 123 * 存放目录
  124. 124 * @param needCompress
  125. 125 * 是否压缩LOGO
  126. 126 * @throws Exception
  127. 127 */
  128. 128 public static void encode(String content, String imgPath, String destPath,
  129. 129 boolean needCompress) throws Exception {
  130. 130 BufferedImage image = QRCodeUtil.createImage(content, imgPath,
  131. 131 needCompress);
  132. 132 mkdirs(destPath);
  133. 133 String file = new Random().nextInt(99999999)+".jpg";
  134. 134 ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));
  135. 135 }
  136. 136
  137. 137 /**
  138. 138 * 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
  139. 139 * @param destPath 存放目录
  140. 140 */
  141. 141 public static void mkdirs(String destPath) {
  142. 142 File file =new File(destPath);
  143. 143 //当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
  144. 144 if (!file.exists() && !file.isDirectory()) {
  145. 145 file.mkdirs();
  146. 146 }
  147. 147 }
  148. 148
  149. 149 /**
  150. 150 * 生成二维码(内嵌LOGO)
  151. 151 *
  152. 152 * @param content
  153. 153 * 内容
  154. 154 * @param imgPath
  155. 155 * LOGO地址
  156. 156 * @param destPath
  157. 157 * 存储地址
  158. 158 * @throws Exception
  159. 159 */
  160. 160 public static void encode(String content, String imgPath, String destPath)
  161. 161 throws Exception {
  162. 162 QRCodeUtil.encode(content, imgPath, destPath, false);
  163. 163 }
  164. 164
  165. 165 /**
  166. 166 * 生成二维码
  167. 167 *
  168. 168 * @param content
  169. 169 * 内容
  170. 170 * @param destPath
  171. 171 * 存储地址
  172. 172 * @param needCompress
  173. 173 * 是否压缩LOGO
  174. 174 * @throws Exception
  175. 175 */
  176. 176 public static void encode(String content, String destPath,
  177. 177 boolean needCompress) throws Exception {
  178. 178 QRCodeUtil.encode(content, null, destPath, needCompress);
  179. 179 }
  180. 180
  181. 181 /**
  182. 182 * 生成二维码
  183. 183 *
  184. 184 * @param content
  185. 185 * 内容
  186. 186 * @param destPath
  187. 187 * 存储地址
  188. 188 * @throws Exception
  189. 189 */
  190. 190 public static void encode(String content, String destPath) throws Exception {
  191. 191 QRCodeUtil.encode(content, null, destPath, false);
  192. 192 }
  193. 193
  194. 194 /**
  195. 195 * 生成二维码(内嵌LOGO)
  196. 196 *
  197. 197 * @param content
  198. 198 * 内容
  199. 199 * @param imgPath
  200. 200 * LOGO地址
  201. 201 * @param output
  202. 202 * 输出流
  203. 203 * @param needCompress
  204. 204 * 是否压缩LOGO
  205. 205 * @throws Exception
  206. 206 */
  207. 207 public static void encode(String content, String imgPath,
  208. 208 OutputStream output, boolean needCompress) throws Exception {
  209. 209 BufferedImage image = QRCodeUtil.createImage(content, imgPath,
  210. 210 needCompress);
  211. 211 ImageIO.write(image, FORMAT_NAME, output);
  212. 212 }
  213. 213
  214. 214 /**
  215. 215 * 生成二维码
  216. 216 *
  217. 217 * @param content
  218. 218 * 内容
  219. 219 * @param output
  220. 220 * 输出流
  221. 221 * @throws Exception
  222. 222 */
  223. 223 public static void encode(String content, OutputStream output)
  224. 224 throws Exception {
  225. 225 QRCodeUtil.encode(content, null, output, false);
  226. 226 }
  227. 227
  228. 228 /**
  229. 229 * 解析二维码
  230. 230 *
  231. 231 * @param file
  232. 232 * 二维码图片
  233. 233 * @return
  234. 234 * @throws Exception
  235. 235 */
  236. 236 public static String decode(File file) throws Exception {
  237. 237 BufferedImage image;
  238. 238 image = ImageIO.read(file);
  239. 239 if (image == null) {
  240. 240 return null;
  241. 241 }
  242. 242 BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(
  243. 243 image);
  244. 244 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
  245. 245 Result result;
  246. 246 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
  247. 247 hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
  248. 248 result = new MultiFormatReader().decode(bitmap, hints);
  249. 249 String resultStr = result.getText();
  250. 250 return resultStr;
  251. 251 }
  252. 252
  253. 253 /**
  254. 254 * 解析二维码
  255. 255 *
  256. 256 * @param path
  257. 257 * 二维码图片地址
  258. 258 * @return
  259. 259 * @throws Exception
  260. 260 */
  261. 261 public static String decode(String path) throws Exception {
  262. 262 return QRCodeUtil.decode(new File(path));
  263. 263 }
  264. 264
  265. 265 public static void createCode(String text,String picturepath,String location) throws Exception {
  266. 266 QRCodeUtil.encode(text, picturepath, location, true);
  267. 267 }
  268. 268 }
  1. package com.lsd.barcode;
  2.  
  3. import java.awt.Graphics2D;
  4. import java.awt.geom.AffineTransform;
  5. import java.awt.image.BufferedImage;
  6. import com.google.zxing.LuminanceSource;
  7.  
  8. public class BufferedImageLuminanceSource extends LuminanceSource {
  9. private final BufferedImage image;
  10. private final int left;
  11. private final int top;
  12.  
  13. public BufferedImageLuminanceSource(BufferedImage image) {
  14. this(image, 0, 0, image.getWidth(), image.getHeight());
  15. }
  16.  
  17. public BufferedImageLuminanceSource(BufferedImage image, int left,
  18. int top, int width, int height) {
  19. super(width, height);
  20.  
  21. int sourceWidth = image.getWidth();
  22. int sourceHeight = image.getHeight();
  23. if (left + width > sourceWidth || top + height > sourceHeight) {
  24. throw new IllegalArgumentException(
  25. "Crop rectangle does not fit within image data.");
  26. }
  27.  
  28. for (int y = top; y < top + height; y++) {
  29. for (int x = left; x < left + width; x++) {
  30. if ((image.getRGB(x, y) & 0xFF000000) == 0) {
  31. image.setRGB(x, y, 0xFFFFFFFF); // = white
  32. }
  33. }
  34. }
  35.  
  36. this.image = new BufferedImage(sourceWidth, sourceHeight,
  37. BufferedImage.TYPE_BYTE_GRAY);
  38. this.image.getGraphics().drawImage(image, 0, 0, null);
  39. this.left = left;
  40. this.top = top;
  41. }
  42.  
  43. public byte[] getRow(int y, byte[] row) {
  44. if (y < 0 || y >= getHeight()) {
  45. throw new IllegalArgumentException(
  46. "Requested row is outside the image: " + y);
  47. }
  48. int width = getWidth();
  49. if (row == null || row.length < width) {
  50. row = new byte[width];
  51. }
  52. image.getRaster().getDataElements(left, top + y, width, 1, row);
  53. return row;
  54. }
  55.  
  56. public byte[] getMatrix() {
  57. int width = getWidth();
  58. int height = getHeight();
  59. int area = width * height;
  60. byte[] matrix = new byte[area];
  61. image.getRaster().getDataElements(left, top, width, height, matrix);
  62. return matrix;
  63. }
  64.  
  65. public boolean isCropSupported() {
  66. return true;
  67. }
  68.  
  69. public LuminanceSource crop(int left, int top, int width, int height) {
  70. return new BufferedImageLuminanceSource(image, this.left + left,
  71. this.top + top, width, height);
  72. }
  73.  
  74. public boolean isRotateSupported() {
  75. return true;
  76. }
  77.  
  78. public LuminanceSource rotateCounterClockwise() {
  79. int sourceWidth = image.getWidth();
  80. int sourceHeight = image.getHeight();
  81. AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0,
  82. 0.0, 0.0, sourceWidth);
  83. BufferedImage rotatedImage = new BufferedImage(sourceHeight,
  84. sourceWidth, BufferedImage.TYPE_BYTE_GRAY);
  85. Graphics2D g = rotatedImage.createGraphics();
  86. g.drawImage(image, transform, null);
  87. g.dispose();
  88. int width = getWidth();
  89. return new BufferedImageLuminanceSource(rotatedImage, top,
  90. sourceWidth - (left + width), getHeight(), width);
  91. }
  92. }

分别保存在两个java类文件中,然后打成jar包,直接调用包中的createCode方法即可,参数别分为二维码内容,图片路径,存放地址,图片是否需要压缩。

Java语言实现二维码的生成的更多相关文章

  1. java 二维码原理以及用java实现的二维码的生成、解码(转)

    http://blog.csdn.net/songylwq/article/details/8643948 http://sjsky.iteye.com/blog/1136934 http://bbs ...

  2. Java 条形码 二维码 的生成与解析

    Barcode简介 Barcode是由一组按一定编码规则排列的条,空符号,用以表示一定的字符,数字及符号组成的,一种机器可读的数据表示方式. Barcode的形式多种多样,按照它们的外观分类: Lin ...

  3. java zxing实现二维码生成和解析zxing实现二维码生成和解析

    原文:https://www.cnblogs.com/zhangzhen894095789/p/6623041.html zxing实现二维码生成和解析   二维码 zxing   二维码的生成与解析 ...

  4. 关于java的二维码的生成与解析

    本文说的是通过zxing实现二维码的生成与解析,看着很简单,直接上代码 import java.io.File; import java.io.IOException; import java.nio ...

  5. Android zxing 解析二维码,生成二维码极简demo

    zxing 官方的代码很多,看起来很费劲,此demo只抽取了有用的部分,实现了相机预览解码,解析本地二维码,生成二维码三个功能. 简化后的结构如下: 废话少说直接上代码: BaseDecodeHand ...

  6. ZXing二维码的生成和解析

    Zxing是Google提供的关于条码(一维码.二维码)的解析工具,提供了二维码的生成与解析的方法, 现在我简单介绍一下使用Java利用Zxing生成与解析二维码 注意: 二维码的生成需要借助辅助类( ...

  7. Android实例-实现扫描二维码并生成二维码(XE8+小米5)

    相关资料: 第三方资料太大没法写在博文上,请下载CSDN的程序包. 程序包下载: http://download.csdn.net/detail/zhujianqiangqq/9657186 注意事项 ...

  8. zxing二维码的生成与解码(C#)

    ZXing是一个开源Java类库用于解析多种格式的1D/2D条形码.目标是能够对QR编码.Data Matrix.UPC的1D条形码进行解码. 其提供了多种平台下的客户端包括:J2ME.J2SE和An ...

  9. java画海报二维码

    package cn.com.yitong.ares.qrcode; import java.awt.BasicStroke;import java.awt.Color;import java.awt ...

随机推荐

  1. 通过Python将监控数据由influxdb写入到MySQL

    一.项目背景 我们知道InfluxDB是最受欢迎的时序数据库(TSDB).InfluxDB具有 持续高并发写入.无更新:数据压缩存储:低查询延时 的特点.从下面这个权威的统计图中,就可以看出Influ ...

  2. 2020BUAA-团队介绍-采访

    团队作业-团队介绍和采访 项目 内容 课程:北航2020软件工程 博客园班级地址 作业要求 团队作业-团队介绍和采访 团队介绍 姓名 有图有真相 个人介绍 刘y 精通(没那么熟悉)c++和python ...

  3. 7. IDEA概述和安装

    1.1IDEA概述 IDEA全称InteliJ IDEA,是用于Java语言开发的继承环境,它是业界公认的目前用于Java程序开发的最好工具 集成环境:把代码编写,编译,执行,调试等多种功能综合到一起 ...

  4. 白日梦的MySQL专题(第33篇):各种登陆MySQL的骚操作

    阅读原文 系列文章公众号首发,点击阅读原文 前置知识 我们想登陆到mysql中前提是肯定需要一个用户名和密码:比如 mysql -uroot -proot 在mysql中用户的信息会存放在 mysql ...

  5. 关于文字内容过长,导致文本内容超出html 标签宽度的解决方法之自动换行

    在标签的style 属性中设置 word-break style="word-break:break-all;" 这样就可以实现换行 上截图没设置之前 设置之后 完美解决!!!!! ...

  6. Linux基本原则

    Bash特性 Shell shell(外壳),广义的shell可以理解为是用户的工作环境,在windows看来桌面就是一个shell,在linux看来终端就是shell 常见的shell有两种,一种是 ...

  7. openstack创建vlan网络并配置网络设备

    1.在管理员-->网络-->创建网络. 2.填写网络信息,这里要划分新的VLAN,注意在物理网络中填写的事VLAN,段ID指的是vlan的id 3.创建的网络. 4.创建子网,在里面修改子 ...

  8. linux进阶之网络技术管理

    本节内容 1.      网络七层模型 物理层 数据链路层 网络层 传输层 会话层 表示层 应用层 2.  TCP/UDP (1)TCP面向连接,可靠传输,消耗系统资源比较多,传输速度较慢,但是数据传 ...

  9. MyBatis 开启 Log4j 日志调试信息开关

    Log4j 是什么 Log4j 是由 Apache 提供的开源日志框架,用于帮助用户处理日志信息. Log4j 能将日志信息分级打印和存储,而且提供了日志不同的存储方式,我们可以将日志发送到控制台,或 ...

  10. 常用Python第三方库简介

    如果说强大的标准库奠定了Python发展的基石,丰富的第三方库则是python不断发展的保证,随着python的发展一些稳定的第三库被加入到了标准库里面,这里有6000多个第三方库的介绍 下表中加粗并 ...