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.
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.
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
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.
<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).
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.