/**
  * Prepares a BufferedImage to paint to.
  *
  * @param dim the barcode dimensions
  * @param orientation the barcode orientation (0, 90, 180, 270)
  * @param resolution the desired image resolution (dots per inch)
  * @param imageType the desired image type (Values: BufferedImage.TYPE_*)
  * @return the requested BufferedImage
  */
 public static BufferedImage prepareImage(
     BarcodeDimension dim, int orientation, int resolution, int imageType) {
   int bmw = UnitConv.mm2px(dim.getWidthPlusQuiet(orientation), resolution);
   int bmh = UnitConv.mm2px(dim.getHeightPlusQuiet(orientation), resolution);
   BufferedImage bi = new BufferedImage(bmw, bmh, imageType);
   return bi;
 }
Ejemplo n.º 2
0
 public void initCode39Bean() {
   code39Bean = new Code39Bean();
   final int dpi = barcodeDpi;
   // Configure the barcode generator
   code39Bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); // makes the
   // narrow bar
   // width exactly one pixel
   code39Bean.setBarHeight(barHeight);
   code39Bean.setWideFactor(wideFactor);
   code39Bean.doQuietZone(false);
 }
  public static BarcodeGenerator createBarcode4J(
      final String type, final boolean showText, final boolean checksum, final Number barHeight) {
    if (BARCODE_DATAMATRIX.equals(type)) {
      final DataMatrix dataMatrix = new DataMatrix();
      return dataMatrix;
    }

    if (BARCODE_EAN8.equals(type)) {
      final EAN8 dataMatrix = new EAN8();
      if (showText == false) {
        dataMatrix.getUPCEANBean().setMsgPosition(HumanReadablePlacement.HRP_NONE);
      }
      if (barHeight != null) {
        dataMatrix.getUPCEANBean().setBarHeight(UnitConv.pt2mm(barHeight.doubleValue()));
      }
      dataMatrix
          .getUPCEANBean()
          .setChecksumMode(checksum ? ChecksumMode.CP_AUTO : ChecksumMode.CP_IGNORE);
      return dataMatrix;
    }

    if (BARCODE_EAN128.equals(type)) {
      final EAN128 dataMatrix = new EAN128();
      if (showText == false) {
        dataMatrix.getEAN128Bean().setMsgPosition(HumanReadablePlacement.HRP_NONE);
      }
      if (barHeight != null) {
        dataMatrix.getEAN128Bean().setBarHeight(UnitConv.pt2mm(barHeight.doubleValue()));
      }
      dataMatrix
          .getEAN128Bean()
          .setChecksumMode(checksum ? ChecksumMode.CP_AUTO : ChecksumMode.CP_IGNORE);
      return dataMatrix;
    }

    if (BARCODE_UPCE.equals(type)) {
      final UPCE dataMatrix = new UPCE();
      if (showText == false) {
        dataMatrix.getUPCEANBean().setMsgPosition(HumanReadablePlacement.HRP_NONE);
      }
      if (barHeight != null) {
        dataMatrix.getUPCEANBean().setBarHeight(UnitConv.pt2mm(barHeight.doubleValue()));
      }
      dataMatrix
          .getUPCEANBean()
          .setChecksumMode(checksum ? ChecksumMode.CP_AUTO : ChecksumMode.CP_IGNORE);
      return dataMatrix;
    }

    if (BARCODE_ROYALMAIL.equals(type)) {
      final RoyalMailCBC dataMatrix = new RoyalMailCBC();
      if (showText == false) {
        dataMatrix.getRoyalMailCBCBean().setMsgPosition(HumanReadablePlacement.HRP_NONE);
      }
      if (barHeight != null) {
        dataMatrix.getRoyalMailCBCBean().setBarHeight(UnitConv.pt2mm(barHeight.doubleValue()));
      }
      dataMatrix
          .getRoyalMailCBCBean()
          .setChecksumMode(checksum ? ChecksumMode.CP_AUTO : ChecksumMode.CP_IGNORE);
      return dataMatrix;
    }

    if (BARCODE_USPSINTELLIGENTMAIL.equals(type)) {
      final USPSIntelligentMail dataMatrix = new USPSIntelligentMail();
      if (showText == false) {
        dataMatrix.getUSPSIntelligentMailBean().setMsgPosition(HumanReadablePlacement.HRP_NONE);
      }
      if (barHeight != null) {
        dataMatrix
            .getUSPSIntelligentMailBean()
            .setBarHeight(UnitConv.pt2mm(barHeight.doubleValue()));
      }
      dataMatrix
          .getUSPSIntelligentMailBean()
          .setChecksumMode(checksum ? ChecksumMode.CP_AUTO : ChecksumMode.CP_IGNORE);
      return dataMatrix;
    }
    return null;
  }
Ejemplo n.º 4
0
/**
 * This class is an implementation of DataMatrix (ISO 16022:2000(E)).
 *
 * @version $Id: DataMatrixBean.java,v 1.7 2008/09/22 08:59:07 jmaerki Exp $
 */
public class DataMatrixBean extends AbstractBarcodeBean {

  /** The default module width (dot size) for DataMatrix. */
  protected static final double DEFAULT_MODULE_WIDTH = UnitConv.in2mm(1.0 / 72); // 1px at 72dpi

  /**
   * The requested shape. May be <code>FORCE_NONE</code>, <code>FORCE_SQUARE</code> or <code>
   * FORCE_RECTANGLE</code>.
   */
  private SymbolShapeHint shape;

  /** Optional: the minimum size of the symbol. */
  private Dimension minSize;
  /** Optional: the maximum size of the symbol. */
  private Dimension maxSize;

  /** Create a new instance. */
  public DataMatrixBean() {
    this.height = 0.0; // not used by DataMatrix
    this.moduleWidth = DEFAULT_MODULE_WIDTH;
    setQuietZone(1 * moduleWidth);
    this.shape = SymbolShapeHint.FORCE_NONE;
  }

  /**
   * Sets the requested shape for the generated barcodes.
   *
   * @param shape requested shape. May be <code>SymbolShapeHint.FORCE_NONE</code>, <code>
   *     SymbolShapeHint.FORCE_SQUARE</code> or <code>SymbolShapeHint.FORCE_RECTANGLE</code>.
   */
  public void setShape(SymbolShapeHint shape) {
    this.shape = shape;
  }

  /**
   * Gets the requested shape for the generated barcodes.
   *
   * @return the requested shape (one of SymbolShapeHint.*).
   */
  public SymbolShapeHint getShape() {
    return shape;
  }

  /**
   * Sets the minimum symbol size that is to be produced.
   *
   * @param minSize the minimum size (in pixels), or null for no constraint
   */
  public void setMinSize(Dimension minSize) {
    this.minSize = new Dimension(minSize);
  }

  /**
   * Returns the minimum symbol size that is to be produced. If the method returns null, there's no
   * constraint on the symbol size.
   *
   * @return the minimum symbol size (in pixels), or null if there's no size constraint
   */
  public Dimension getMinSize() {
    if (this.minSize != null) {
      return new Dimension(this.minSize);
    } else {
      return null;
    }
  }

  /**
   * Sets the maximum symbol size that is to be produced.
   *
   * @param maxSize the maximum size (in pixels), or null for no constraint
   */
  public void setMaxSize(Dimension maxSize) {
    this.maxSize = new Dimension(maxSize);
  }

  /**
   * Returns the maximum symbol size that is to be produced. If the method returns null, there's no
   * constraint on the symbol size.
   *
   * @return the maximum symbol size (in pixels), or null if there's no size constraint
   */
  public Dimension getMaxSize() {
    if (this.maxSize != null) {
      return new Dimension(this.maxSize);
    } else {
      return null;
    }
  }

  /** {@inheritDoc} */
  public void generateBarcode(CanvasProvider canvas, String msg) {
    if ((msg == null) || (msg.length() == 0)) {
      throw new NullPointerException("Parameter msg must not be empty");
    }

    TwoDimBarcodeLogicHandler handler =
        new DefaultTwoDimCanvasLogicHandler(this, new Canvas(canvas));

    DataMatrixLogicImpl impl = new DataMatrixLogicImpl();
    impl.generateBarcodeLogic(handler, msg, getShape(), getMinSize(), getMaxSize());
  }

  /** {@inheritDoc} */
  public BarcodeDimension calcDimensions(String msg) {
    String encoded;
    try {
      encoded = DataMatrixHighLevelEncoder.encodeHighLevel(msg, shape, getMinSize(), getMaxSize());
    } catch (IOException e) {
      throw new IllegalArgumentException("Cannot fetch data: " + e.getLocalizedMessage());
    }
    DataMatrixSymbolInfo symbolInfo = DataMatrixSymbolInfo.lookup(encoded.length(), shape);

    double width = symbolInfo.getSymbolWidth() * getModuleWidth();
    double height = symbolInfo.getSymbolHeight() * getBarHeight();
    double qzh = (hasQuietZone() ? getQuietZone() : 0);
    double qzv = (hasQuietZone() ? getVerticalQuietZone() : 0);
    return new BarcodeDimension(width, height, width + (2 * qzh), height + (2 * qzv), qzh, qzv);
  }

  /** {@inheritDoc} */
  public double getVerticalQuietZone() {
    return getQuietZone();
  }

  /** {@inheritDoc} */
  public double getBarWidth(int width) {
    return moduleWidth;
  }

  /** {@inheritDoc} */
  public double getBarHeight() {
    return moduleWidth;
  }
}