admin
2020-02-28 1d9eef9a4ba7caebf73b483e0062f9259dbf22a0
utils/src/main/java/org/yeshi/utils/QRCodeUtil.java
@@ -88,6 +88,50 @@
      hints.put(EncodeHintType.MARGIN, 1);
      BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,
            hints);
      int width = bitMatrix.getWidth();
      int height = bitMatrix.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, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
         }
      }
      if (logoImg == null) {
         return image;
      }
      // 插入图片
      insertImage(image, logoImg, needCompress);
      return image;
   }
   /**
    * user: Rex date: 2016年12月29日 上午12:31:29
    *
    * @param content
    *            二维码内容
    * @param logoImgPath
    *            Logo
    * @param needCompress
    *            是否压缩Logo
    * @return 返回二维码图片
    * @throws WriterException
    * @throws IOException
    *             BufferedImage TODO 创建二维码图片
    */
   private BufferedImage createImageDeleteWhite(String content, InputStream logoImg, boolean needCompress)
         throws WriterException, IOException {
      Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
      hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
      hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
      hints.put(EncodeHintType.MARGIN, 1);
      BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,
            hints);
      //调用去除白边方法
        bitMatrix = deleteWhite(bitMatrix);
      int width = bitMatrix.getWidth();
      int height = bitMatrix.getHeight();
      BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
@@ -188,6 +232,14 @@
      return new ByteArrayInputStream(out.toByteArray());
   }
   public InputStream encodeDeleteWhite(String content) throws Exception {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      encodeDeleteWhite(content, null, out, false);
      return new ByteArrayInputStream(out.toByteArray());
   }
   /**
    * user: Rex date: 2016年12月29日 上午12:36:58
    * 
@@ -220,6 +272,21 @@
   public void encode(String content, InputStream logoImg, ByteArrayOutputStream output, boolean needCompress)
         throws Exception {
      BufferedImage image = createImage(content, logoImg, needCompress);
      ImageIO.write(image, FORMAT_NAME, output);
   }
   /**
    *
    * @param content
    * @param logoImg
    * @param output
    *            二进制输出流
    * @param needCompress
    * @throws Exception
    */
   public void encodeDeleteWhite(String content, InputStream logoImg, ByteArrayOutputStream output, boolean needCompress)
         throws Exception {
      BufferedImage image = createImageDeleteWhite(content, logoImg, needCompress);
      ImageIO.write(image, FORMAT_NAME, output);
   }
@@ -275,4 +342,19 @@
      return decode(new File(path));
   }
    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;
    }
}