<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V1.1//EN" "document-v11.dtd">
<document>
  <header>
    <title>Embedding in a Java application (using the JavaBean API)</title>
    <authors>
      <person id="jmaerki" name="Jeremias Märki" email="jmaerki@users.sourceforge.net"/>
    </authors>
  </header>
  <body>
    <section>
      <title>Introduction</title>
      <p>
        Normally, you will configure the barcode generators using XML as described on
        the <link href="barcode-xml.html">Barcode XML page</link>. Some people prefer
        using JavaBeans instead of XML. Here's how to use that API.
      </p>
      <note>
        If you work with the Bean API you don't need avalon-framework.jar in your 
        classpath.
      </note>
    </section>
    <section>
      <title>Basic steps</title>
      <p>
        The steps necessary to create barcodes using JavaBeans is similar to the steps 
        using XML:
      </p>
      <ol>
        <li>Create a barcode bean</li>
        <li>Set the desired values to configure the barcode generator</li>
        <li>Create a CanvasProvider (depending on the output format)</li>
        <li>Finally generate the barcode</li>
      </ol>
    </section>
    <section>
      <title>Creating a barcode bean</title>
      <p>
        As the first step you have to instantiate a barcode bean.
      </p>
      <source><![CDATA[
Code39Bean bean = new Code39Bean();]]></source>
      <p>
        Here's a list of available bean classes:
      </p>
      <ul>
        <li>org.krysalis.barcode4j.impl.codabar.CodabarBean</li>
        <li>org.krysalis.barcode4j.impl.code128.Code128Bean</li>
        <li>org.krysalis.barcode4j.impl.code39.Code39Bean</li>
        <li>org.krysalis.barcode4j.impl.int2of5.Interleaved2Of5Bean</li>
        <li>org.krysalis.barcode4j.impl.postnet.POSTNETBean</li>
        <li>org.krysalis.barcode4j.impl.upcean.EAN13Bean</li>
        <li>org.krysalis.barcode4j.impl.upcean.EAN8Bean</li>
        <li>org.krysalis.barcode4j.impl.upcean.UPCABean</li>
        <li>org.krysalis.barcode4j.impl.upcean.UPCEBean</li>
      </ul>
    </section>
    <section>
      <title>Configuring the bean</title>
      <p>
        Each bean has specific getter and setter methods to control
        various aspects of the individual implementations. Please
        refer to the <fork href="ext:javadocs_2_0">JavaDocs</fork> for the
        available properties.
      </p>
      <p>An example:</p>
      <source><![CDATA[
bean.setChecksumMode(ChecksumMode.CP_CHECK);
bean.setWideFactor(3);]]></source>
    </section>
    <section>
      <title>Creating a CanvasProvider and generating the barcode</title>
      <p>
        From here it's the same steps as with XML configuration. Please refer to
        the <link href="embedding.html">primary embedding page</link>.
      </p>
    </section>
    <section>
      <title>A complete example</title>
      <source><![CDATA[
//Create the barcode bean
Code39Bean bean = new Code39Bean();

final int dpi = 150;

//Configure the barcode generator
bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar 
                                                 //width exactly one pixel
bean.setWideFactor(3);
bean.doQuietZone(false);

//Open output file
File outputFile = new File("out.png");
OutputStream out = new FileOutputStream(outputFile);
try {
    //Set up the canvas provider for monochrome PNG output 
    BitmapCanvasProvider canvas = new BitmapCanvasProvider(
            out, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);

    //Generate the barcode
    bean.generateBarcode(canvas, "123456");

    //Signal end of generation
    canvas.finish();
} finally {
    out.close();
}]]></source>
    </section>
  </body>
</document>
