Ejemplo n.º 1
0
 /**
  * 生成二维码(内嵌LOGO)
  *
  * @param content 内容
  * @param imgPath LOGO地址
  * @param destPath 存放目录
  * @param needCompress 是否压缩LOGO
  * @throws Exception
  */
 public static File encode(
     String content,
     String imgPath,
     String destPath,
     boolean needCompress,
     QRCodeConfig qrCodeConfig)
     throws Exception {
   BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress, qrCodeConfig);
   mkdirs(destPath);
   String fileName = null;
   if (qrCodeConfig != null && qrCodeConfig.getFileName() != null) {
     fileName = qrCodeConfig.getFileName() + ".png";
   } else {
     fileName = new Random().nextInt(99999999) + ".png";
   }
   File file = new File(destPath + File.separator + fileName);
   ImageIO.write(image, FORMAT_NAME, file);
   return file;
 }
Ejemplo n.º 2
0
 private static BufferedImage createImage(
     String content, String imgPath, boolean needCompress, QRCodeConfig qrCodeConfig)
     throws Exception {
   int codewidth = QRCODE_WIDTH;
   int codeheight = QRCODE_HEIGHT;
   if (qrCodeConfig != null) {
     if (qrCodeConfig.getWidth() > 0) {
       codewidth = qrCodeConfig.getWidth();
     }
     if (qrCodeConfig.getHeight() > 0) {
       codeheight = qrCodeConfig.getHeight();
     }
   }
   Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
   hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
   hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
   hints.put(EncodeHintType.MARGIN, 0);
   BitMatrix bitMatrix =
       new MultiFormatWriter()
           .encode(content, BarcodeFormat.QR_CODE, codewidth, codeheight, hints);
   bitMatrix = deleteWhite(bitMatrix);
   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 (imgPath == null || "".equals(imgPath)) {
     return image;
   }
   // 插入图片
   QRCodeUtil.insertImage(image, imgPath, needCompress);
   return image;
 }