个人随笔
目录
工具类:Java版本二维码生成以及解码
2020-09-11 23:05:15

有时候我们经常会想要实现二维码批量生成的功能,这里提供一个二维码生成以及解析的工具类,包括可以上传logo,代码如下:

  1. /**
  2. * 二维码工具类
  3. * @date 20198年10月31日
  4. * @version 1.0
  5. */
  6. public class QRCodeUtils {
  7. // 二维码大小
  8. private static final int QRCODE_SIZE = 300;
  9. // 二维码中间logo的尺寸
  10. private static final int WIDTH = 60;
  11. private static final int HEIGHT = 60;
  12. public static void encode(String content, String logoPath, String imgPath) throws Exception {
  13. encode(content, logoPath, imgPath, QRCODE_SIZE, 1);
  14. }
  15. public static void encode(String content, String logoPath, String imgPath, int size, int margin) throws Exception {
  16. BufferedImage image = createImage(content, logoPath, size, margin);
  17. ImageIO.write(image, "png", new File(imgPath));
  18. }
  19. public static void encode(String content, String logoPath, OutputStream output) throws Exception {
  20. encode(content, logoPath, output, QRCODE_SIZE, 1);
  21. }
  22. public static void encode(String content, String logoPath, OutputStream output, int size, int margin) throws Exception {
  23. BufferedImage image = createImage(content, logoPath, size, margin);
  24. ImageIO.write(image, "png", output);
  25. }
  26. private static BufferedImage createImage(String content, String imgPath, int size, int margin) throws Exception {
  27. Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
  28. hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
  29. hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
  30. hints.put(EncodeHintType.MARGIN, margin);
  31. BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, hints);
  32. int[] rec = bitMatrix.getEnclosingRectangle();
  33. int resWidth = rec[2] + 1;
  34. int resHeight = rec[3] + 1;
  35. BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
  36. resMatrix.clear();
  37. for (int i = 0; i < resWidth; i++) {
  38. for (int j = 0; j < resHeight; j++) {
  39. if (bitMatrix.get(i + rec[0], j + rec[1])) {
  40. resMatrix.set(i, j);
  41. }
  42. }
  43. }
  44. int width = bitMatrix.getWidth();
  45. int height = bitMatrix.getHeight();
  46. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  47. for (int x = 0; x < width; x++) {
  48. for (int y = 0; y < height; y++) {
  49. image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
  50. }
  51. }
  52. if (imgPath == null || "".equals(imgPath)) {
  53. return image;
  54. }
  55. // 插入图片
  56. insertImage(image, imgPath, size);
  57. return image;
  58. }
  59. private static void insertImage(BufferedImage source, String imgPath, int size) throws Exception {
  60. File file = new File(imgPath);
  61. if (!file.exists()) {
  62. System.out.println(""+imgPath+" 该文件不存在!");
  63. return;
  64. }
  65. Image src = ImageIO.read(new File(imgPath));
  66. int width = src.getWidth(null);
  67. int height = src.getHeight(null);
  68. // 压缩LOGO
  69. if (width > WIDTH) {
  70. width = WIDTH;
  71. }
  72. if (height > HEIGHT) {
  73. height = HEIGHT;
  74. }
  75. Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
  76. BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  77. Graphics g = tag.getGraphics();
  78. g.drawImage(image, 0, 0, null); // 绘制缩小后的图
  79. g.dispose();
  80. src = image;
  81. // 插入LOGO
  82. Graphics2D graph = source.createGraphics();
  83. int x = (size - width) / 2;
  84. int y = (size - height) / 2;
  85. graph.drawImage(src, x, y, width, height, null);
  86. Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
  87. graph.setStroke(new BasicStroke(3f));
  88. graph.draw(shape);
  89. graph.dispose();
  90. }
  91. public static String decode(File file) throws Exception {
  92. BufferedImage image;
  93. image = ImageIO.read(file);
  94. if (image == null) {
  95. return null;
  96. }
  97. BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
  98. BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
  99. Result result;
  100. Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
  101. hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
  102. result = new MultiFormatReader().decode(bitmap, hints);
  103. return result != null ? result.getText() : null;
  104. }
  105. public static void main(String[] args) throws Exception {
  106. QRCodeUtils.encode("https://www.suibibk.com/blog/579412311547052032/582955445098905600/639208466002477056", "e:/logo.jpg", "e:/qrcode.png");
  107. // 解析二维码存储的内容并打印
  108. System.out.println(decode(new File("e:/qrcode.png")));
  109. }
  110. }

若不需要logo就不需要传递

 510

啊!这个可能是世界上最丑的留言输入框功能~


当然,也是最丑的留言列表

有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2