智慧保安后台管理-外网项目备份
zhongrj
2023-09-17 8853292babb2ad94de4a3207966f1e83b767cd2d
src/main/java/org/springblade/common/utils/QRCodeUtil.java
@@ -23,6 +23,8 @@
import java.net.URL;
import java.util.HashMap;
import static com.google.zxing.client.j2se.MatrixToImageConfig.BLACK;
import static com.google.zxing.client.j2se.MatrixToImageConfig.WHITE;
import static org.bouncycastle.asn1.x500.style.RFC4519Style.cn;
/**
@@ -226,6 +228,9 @@
      hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
      BitMatrix bitMatrix =qrCodeWriter.encode(text,BarcodeFormat.QR_CODE, width, height,hints);
      //删除白边
//      BufferedImage bufferedImage = deleteWhite(bitMatrix);
      //BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
      ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
@@ -234,4 +239,53 @@
      return pngOutputStream.toByteArray();
   }
   /**
    * 去白边的话,调用这个方法
    * @param matrix
    * @return
    */
   private static BufferedImage  deleteWhite(BitMatrix matrix) {
      int[] rec = matrix.getEnclosingRectangle();
      int resWidth = rec[2] + 1;
      int resHeight = rec[3] + 1;
      BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
      resMatrix.clear();
      for (int i = 0; i < resWidth; i++) {
         for (int j = 0; j < resHeight; j++) {
            if (matrix.get(i + rec[0], j + rec[1]))
               resMatrix.set(i, j);
         }
      }
      int width = resMatrix.getWidth();
      int height = resMatrix.getHeight();
      BufferedImage image = new BufferedImage(width, height,
         BufferedImage.TYPE_INT_RGB);
      for (int x = 0; x < width; x++) {
         for (int y = 0; y < height; y++) {
            image.setRGB(x, y, resMatrix.get(x, y) ? BLACK
               : WHITE);
         }
      }
      return image;
   }
//   private static BitMatrix deleteWhite(BitMatrix matrix) {
//      int[] rec = matrix.getEnclosingRectangle();
//      int resWidth = rec[2] + 1;
//      int resHeight = rec[3] + 1;
//
//      BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
//      resMatrix.clear();
//      for (int i = 0; i < resWidth; i++) {
//         for (int j = 0; j < resHeight; j++) {
//            if (matrix.get(i + rec[0], j + rec[1]))
//               resMatrix.set(i, j);
//         }
//      }
//      return resMatrix;
//   }
}