二维码介绍

二维条形码最早发明于日本,它是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,在代码编制上巧妙地利用构成计算机内部逻辑基础的“0”、“1”比特流的概念,使用若干个与二进制相对应的几何形体来表示文字数值信息,通过图象输入设备或光电扫描设备自动识读以实现信息自动处理。
http://zsrimg.ikafan.com/file_images/article/201709/2017910143617990.jpg?2017810143632
如下为java生成二维码工具类,可以选择生成文件,或者直接在页面生成,话不多说了,来一起看看详细的示例代码吧。

示例代码
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.util.HashMap;
import java.util.Map;

  1. import javax.imageio.ImageIO;
  2. import javax.servlet.http.HttpServletResponse;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.google.zxing.BarcodeFormat;
  7. import com.google.zxing.Binarizer;
  8. import com.google.zxing.BinaryBitmap;
  9. import com.google.zxing.DecodeHintType;
  10. import com.google.zxing.EncodeHintType;
  11. import com.google.zxing.LuminanceSource;
  12. import com.google.zxing.MultiFormatReader;
  13. import com.google.zxing.MultiFormatWriter;
  14. import com.google.zxing.NotFoundException;
  15. import com.google.zxing.Result;
  16. import com.google.zxing.WriterException;
  17. import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
  18. import com.google.zxing.client.j2se.MatrixToImageWriter;
  19. import com.google.zxing.common.BitMatrix;
  20. import com.google.zxing.common.HybridBinarizer;
  21. /**
  22. *
  23. * 二维码工具类
  24. * @see [相关类/方法]
  25. * @since [产品/模块版本]
  26. */
  27. public class QRCodeUtil
  28. {
  29. private static final Logger log = LoggerFactory.getLogger(QRCodeUtil.class);
  30. /**
  31. *
  32. * 生成二维码文件测试
  33. * @param filePath 文件路径
  34. * @param fileName 文件名
  35. * @param number 编号
  36. * @param phone 手机号
  37. * @see [类、类#方法、类#成员]
  38. */
  39. public static void generatEncodeTest(String filePath, String fileName, String number, String phone)
  40. {
  41. int width = 200; // 图像宽度
  42. int height = 200; // 图像高度
  43. String format = "png";// 图像类型
  44. JSONObject json = new JSONObject();
  45. json.put("number",number);
  46. json.put("phone", phone);
  47. String content = json.toJSONString();// 内容
  48. Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
  49. hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
  50. try
  51. {
  52. BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
  53. String path = FileSystems.getDefault().getPath(filePath, fileName).toString();
  54. File file = new File(path);
  55. MatrixToImageWriter.writeToFile(bitMatrix, format, file);// 输出图像
  56. System.out.println("二维码输出成功");
  57. System.out.println("图片地址:" + filePath + fileName);
  58. System.out.println("---------------------------");
  59. }
  60. catch (WriterException e)
  61. {
  62. e.printStackTrace();
  63. System.out.println("二维码输出异常");
  64. }
  65. catch (IOException e)
  66. {
  67. e.printStackTrace();
  68. System.out.println("二维码输出异常");
  69. }
  70. }
  71. /**
  72. *
  73. * 解析二维码内容测试
  74. * @param filePath 二维码绝对路径
  75. * @see [类、类#方法、类#成员]
  76. */
  77. public static void parseDecodeTest(String filePath)
  78. {
  79. BufferedImage image;
  80. try
  81. {
  82. image = ImageIO.read(new File(filePath));
  83. LuminanceSource source = new BufferedImageLuminanceSource(image);
  84. Binarizer binarizer = new HybridBinarizer(source);
  85. BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
  86. Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
  87. hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
  88. Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码
  89. JSONObject content = JSONObject.parseObject(result.getText());
  90. StringBuffer sb = new StringBuffer();
  91. sb.append("编号:")
  92. .append(content.getString("number"))
  93. .append("\r\n")
  94. .append("手机号码:")
  95. .append(content.getString("phone"));
  96. String returnText = sb.toString();
  97. System.out.println(returnText);
  98. }
  99. catch (IOException e)
  100. {
  101. e.printStackTrace();
  102. }
  103. catch (NotFoundException e)
  104. {
  105. e.printStackTrace();
  106. }
  107. }
  108. /**
  109. *
  110. * 生成二维码输出流
  111. * 在jsp页面中直接展示时使用
  112. * 无须保存 即生成即展示
  113. * @param response
  114. * @param number 编号
  115. * @param phone 手机号
  116. * @see [类、类#方法、类#成员]
  117. */
  118. public static void generatEncode(HttpServletResponse response, String number, String phone)
  119. {
  120. JSONObject json = new JSONObject();
  121. json.put("number",number);
  122. json.put("phone", phone);
  123. String content = json.toJSONString();// 内容
  124. int width = 200; // 图像宽度
  125. int height = 200; // 图像高度
  126. String format = "png";// 图像类型
  127. Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
  128. hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
  129. try
  130. {
  131. BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
  132. MatrixToImageWriter.writeToStream(bitMatrix, format, response.getOutputStream());// 输出图像
  133. log.info("二维码输出成功");
  134. }
  135. catch (WriterException e)
  136. {
  137. e.printStackTrace();
  138. log.error("二维码输出异常");
  139. }
  140. catch (IOException e)
  141. {
  142. e.printStackTrace();
  143. log.error("二维码输出异常");
  144. }
  145. }
  146. public static void main(String[] args)
  147. {
  148. generatEncodeTest("D:\\","zxing.png","001","13019931996");
  149. parseDecodeTest("D:\\zxing.png");
  150. }
  151. }

总结

更多相关文章

  1. Android中Log的输出
  2. Android中如何查看内存(下)
  3. 变量与函数的声明
  4. 使用PHPExcel导出excell,出现身份证用科学技术法,不能正常显示身份
  5. Laravel中如何轻松容易的输出完整的SQL语句
  6. Android(安卓)时间格式转换
  7. android 文件上传
  8. Vue常用术语,样式绑定与事件绑定,列表渲染
  9. Android(安卓)S5PV210 fimc驱动分析 - fimc_regs.c

随机推荐

  1. android MediaScanner 扫出来的ID3 MP3文
  2. osmdroid 6.0.1基础测试
  3. TextView在单击时发送电子邮件
  4. Android学习笔记--《第一行代码Android》
  5. android-eclipse-phonegap 2..9以下(包含
  6. 如何将文件路径从.java类文件传递到本机j
  7. Android开发之线程与线程池
  8. Android时间日期类小结
  9. Android studio Run 出现 红叉 AndroidRu
  10. 截击后的截击似乎不奏效。