Пример #1
0
  public BufferedImage getBarcode(
      String codeText, String codeType, Integer bcWidth, Integer bcHeight) {
    bcWidth = (bcWidth == 0) ? 150 : bcWidth;
    bcHeight = (bcHeight == 0) ? 20 : bcHeight;

    try {
      Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<>();
      hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

      switch (codeType) {
        case "QR_CODE":
          return getQRCode(codeText, bcWidth);
        case "CODE_39":
          Code39Writer codeWriter = new Code39Writer();
          byteMatrix =
              codeWriter.encode(codeText, BarcodeFormat.CODE_39, bcWidth, bcHeight, hintMap);
          return createBC();
        case "CODE_128":
          Code128Writer code128Writer = new Code128Writer();
          byteMatrix =
              code128Writer.encode(codeText, BarcodeFormat.CODE_128, bcWidth, bcHeight, hintMap);
          return createBC();
        case "EAN_13":
          EAN13Writer ean13Writer = new EAN13Writer();
          byteMatrix =
              ean13Writer.encode(codeText, BarcodeFormat.EAN_13, bcWidth, bcHeight, hintMap);
          return createBC();
        case "EAN_8":
          EAN8Writer ean8Writer = new EAN8Writer();
          byteMatrix = ean8Writer.encode(codeText, BarcodeFormat.EAN_8, bcWidth, bcHeight, hintMap);
          return createBC();
        case "CODABAR":
          CodaBarWriter CodaBarWriter = new CodaBarWriter();
          byteMatrix =
              CodaBarWriter.encode(codeText, BarcodeFormat.CODABAR, bcWidth, bcHeight, hintMap);
          return createBC();
        case "UPC_A":
          UPCAWriter UPCAWriter = new UPCAWriter();
          byteMatrix = UPCAWriter.encode(codeText, BarcodeFormat.UPC_A, bcWidth, bcHeight, hintMap);
          return createBC();
      }
    } catch (WriterException ex) {
      Logger.getLogger(BarCode.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
  }