Java:Java编程实现导出二维码

举报
一个处女座的程序猿 发表于 2021/03/27 00:13:38 2021/03/27
【摘要】 Java:Java编程实现导出二维码         目录 输出结果 代码设计           输出结果 更新……     代码设计 public class QRCodeUtil { private static final String CHARSET = "utf-8"; private static final String ...

Java:Java编程实现导出二维码

 

 

 

 

目录

输出结果

代码设计


 

 

 

 

 

输出结果

更新……

 

 

代码设计


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

文章来源: yunyaniu.blog.csdn.net,作者:一个处女座的程序猿,版权归原作者所有,如需转载,请联系作者。

原文链接:yunyaniu.blog.csdn.net/article/details/90037208

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。