Example #1
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;
 }