QRCodeDialog.java 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.mes.component;
  2. import com.google.zxing.BarcodeFormat;
  3. import com.google.zxing.EncodeHintType;
  4. import com.google.zxing.WriterException;
  5. import com.google.zxing.common.BitMatrix;
  6. import com.google.zxing.qrcode.QRCodeWriter;
  7. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  8. import javax.swing.*;
  9. import java.awt.*;
  10. import java.awt.image.BufferedImage;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. public class QRCodeDialog extends JDialog {
  14. private static final int QR_CODE_SIZE = 400; // 二维码尺寸
  15. public QRCodeDialog(JFrame owner, String title, String content){
  16. super(owner, title, true);
  17. init(content, owner);
  18. }
  19. private void init(String content, JFrame owner){
  20. Container container = this.getContentPane();
  21. container.setLayout(new BorderLayout());
  22. container.setBackground(Color.WHITE);
  23. // 生成二维码图片
  24. BufferedImage qrImage = generateQRCode(content);
  25. if(qrImage != null){
  26. // 显示二维码
  27. JLabel imageLabel = new JLabel(new ImageIcon(qrImage));
  28. imageLabel.setHorizontalAlignment(SwingConstants.CENTER);
  29. container.add(imageLabel, BorderLayout.CENTER);
  30. // 显示文本内容
  31. JLabel textLabel = new JLabel(content);
  32. textLabel.setHorizontalAlignment(SwingConstants.CENTER);
  33. textLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  34. textLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  35. container.add(textLabel, BorderLayout.SOUTH);
  36. }else{
  37. // 如果生成失败,显示错误信息
  38. JLabel errorLabel = new JLabel("二维码生成失败");
  39. errorLabel.setHorizontalAlignment(SwingConstants.CENTER);
  40. errorLabel.setForeground(Color.RED);
  41. errorLabel.setFont(new Font("微软雅黑", Font.PLAIN, 24));
  42. container.add(errorLabel, BorderLayout.CENTER);
  43. }
  44. this.setSize(QR_CODE_SIZE + 50, QR_CODE_SIZE + 100);
  45. this.setLocationRelativeTo(owner);
  46. this.setResizable(false);
  47. this.setVisible(true);
  48. }
  49. /**
  50. * 生成二维码图片
  51. * @param content 二维码内容
  52. * @return BufferedImage
  53. */
  54. private BufferedImage generateQRCode(String content) {
  55. try {
  56. // 设置二维码参数
  57. Map<EncodeHintType, Object> hints = new HashMap<>();
  58. hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 容错级别
  59. hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 字符编码
  60. hints.put(EncodeHintType.MARGIN, 1); // 边距
  61. // 生成二维码矩阵
  62. QRCodeWriter qrCodeWriter = new QRCodeWriter();
  63. BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, QR_CODE_SIZE, QR_CODE_SIZE, hints);
  64. // 转换为图片
  65. BufferedImage image = new BufferedImage(QR_CODE_SIZE, QR_CODE_SIZE, BufferedImage.TYPE_INT_RGB);
  66. for (int x = 0; x < QR_CODE_SIZE; x++) {
  67. for (int y = 0; y < QR_CODE_SIZE; y++) {
  68. image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
  69. }
  70. }
  71. return image;
  72. } catch (WriterException e) {
  73. e.printStackTrace();
  74. return null;
  75. }
  76. }
  77. }