Tutorial

Standalone Applications

The following shows one of the ways for you to use Barcode/Java in a desktop Java application.
Barcode barcode = new Barcode(Barcode.BCT_CODE39);
barcode.setData("12345678");

java.awt.Image img = barcode.makeSimpleImage(1, 35, true, 1, null);
graphics.drawImage(img, 5, 5, null);

barcode.saveSimpleImage(Barcode.BARCODE_PNG, 1, 35, true, 1, null, "c:\\tmp01.png");
Once you've created the barcode component, you can set the barcode data and (not shown above) other properties, like orientation, font size, color, etc. You then call makeSimpleImage() to generate an image. Now you can call drawImage of java.awt.Graphics to actually draw the image on a grapics object (note that you can also use another version of the Java Graphics drawImage() method that allows you to scale the image).

You can also save the image to a file by calling saveSimpleImage().

Alternatively, rather than generate an image first, you can also invoke the draw() method of the barcode object directly.

barcode.draw(graphics, rect, 0, Color.white);
Note that, this way, if you need rotation, you need to use Java Graphics2D to set up a transform, as the barcode draw() method does not do it internally, unlike makeSimpleImage() above.

Web Applications

BarCode/Java allows you to do dynamic barcode image generation using a servlet. Two graphics formats are supported: JPEG and PNG.

You can use dynamic barcode images in any HTML pages and this doesn't require any scripting. Simply embed, for example,

	<img src=".../BarcodeServlet?image=png&type=code39&data=12345678">
in your HTML file and, voila, you have a dynamically generated Code 39 barcode image.

Barcode Size and Printer Resolution

The minimum barcode width on the screen is given by the moduleCount property. The barcode width can only be a multiple (1, 2, etc.) of this minimum width. This multiple (barWidth) may be changed , e.g. <img src=.../BarcodeServlet?barwidth=2> will use a scale factor of 2.

While different bar widths are in proportion on the screen, the printed version may be slightly different. The higher the printer resolution, the better the result will be. If the printer resolution is low, you may need to use a scale factor of 2 or higher, so that the printed barcode is scannable. Note that this is different from applications using directly BarCode/ActiveX or DLL, in which case the barcode component has the opportunity to tune the barcode directly according to the destination printer resolution.

If you find the barcode size too big, you can specify a WIDTH attribute value for the <IMG> tag. This does not compromise the intrinsic barcode image quality as it comes down from the server; the image is simply scaled back at display time on the screen. For example,

	<img src=".../BarcodeServlet?image=png&type=code39&data=12345678" width="150">
forces the image to be at 150 screen pixels at display/print time, even though the underlying image has 188 pixels (moduleCount).

Threading Model

The Barcode/Java class library is thread-safe, in the sense that you can create multiple instances of the Barcode class from multiple threads; this somehow is equivalent to the notion of the apartment threading model in Microsoft COM. Each Barcode instance, however, is only safe when used within a single thread.

The choice of this model is to avoid unnecessary synchronization that would slow down the performance. It works perfectly in the Web environment, where for each servlet invocation, a new Barcode object is created, used to generate a barcode image, and then released.