You are on page 1of 5

Home Products Developers For use with Downloads Sales Support Services Contact us

Quick_links... Search site Trial products Online tutorials Products Barcodes for Office Barcode creators Barcode fonts Developer tools Label printing Labelling tools Windows Phone Resources New to barcodes? How To ... Support centre Tutorial centre Which product For users Common problems What's New? Newsletter Online registration Contact us

Powered by a UK2 VPS

Barcodes in Java
dBarcode Java Classes are platform-independent Java classes that allow barcode images to be created within a developer's own Java desktop application, applet or servlet. Each product includes the class file, a JavaBean archive, and samples with source code illustrating a desktop application, an applet and a servlet. The classes are designed to work with, and the sample source code is provided for the Java 2 SDK J2SE 1.5 or later, and the J2EE SDK 1.5 (for the servlet). Products are suitable for use with BEA WebLogic Server, IBM WebSphere and Sun Java System Application Server.

Setting and retrieving property values


The dBarcode Java classes may be operated entirely by setting or retrieving barcode Property values programmatically. Any property may be set using a method of the form setProperty(value), eg. The Data property may be set using Barcode.setData(12345); Similarly the current value of any property may be obtained using a getProperty() method, eg. String ss=Barcode.getData(); These examples require that that value used to set a property or the variable used to receive a property must be of the correct type (as listed in the Properties reference).

For applets and servlets it is usually desirable to set all parameters using strings. So all properties may also be set using the setParam(PropertyName,Value) function where both the PropertyName and the Value are Strings, e.g. Barcode.setParam(DATA,123456); Using the former method the CodeType property may be set using Barcode.setCodeType(2); Or Barcode.setParam(CODETYPE,2); In these examples the property names are shown in upper case, but the Property name parameter in the setParam() method is actually case-insensitive.

Displaying a barcode on a form


The dBarcodeXX classes extend the Canvas, and so can be placed on other visible elements, such as a frame. For example, the following simple program creates a frame containing a label and a barcode, and displays the barcode using the data 123456. Because the barcode has been added to a visible element it automatically receives Paint messages, and displays itself. Public class test implements Printable{ JFrame pane; JLabel dlabel; dBarcodeU barcode=new dBarcodeU(); public testU(){ pane= new JFrame(" Frame title - Example"); ............ // place a barcode on the form pane.getContentPane().add(barcode); bcard.setLocation(100,100); ................ // draw the barcode doBarcode(); } public void doBarcode(){ // this function sets the barcode properties and then draws it Font dFont=new Font(Arial, Font.PLAIN, 10); bcard.setFont(dFont); bcard.setCodeType(0); bcard.setData(123456); bcard.setUnits(mm); bcard.setdWidth(40.0);

bcard.setdHeight(20.0); bcard.setShowText(1); bcard.setIndicators(1); bcard.setScreenScale(96); }

Printing a barcode image


Barcode images may be drawn on any graphics device, one of the most important of which is the printer. The drawBarcode() method causes the barcode to be draw on the graphics surface (g2 in the example below), positioned at the left and top coordinates (in points) specified by the 2nd and 3rd parameters. For printing the remaining parameters may be 0. In fact the 4th parameter specifies the required resolution (in dpi) of the image but for a printer this information is generally provided thought the graphic surface properties and need not be specified in the method call. public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex >= 1) { return Printable.NO_SUCH_PAGE; } Graphics2D g2=(Graphics2D)g; barcode.drawBarcode(g2,72.0,120.0,0,0); g2.setFont(new Font("Arial",Font.BOLD,12)); g2.setColor(java.awt.Color.BLACK); g2.drawString("dBarcode for Java",72,100); return Printable.PAGE_EXISTS; }

Saving a graphic file


Barcode images may be saved to file using the saveGraphicToFile() method, with the first parameter as the target filename (illustrated below for a Windows file system). The graphic image types supported depend on the platform on which the Java VM is running, but generally include EPS, GIF, JPEG and PNG formats. public void savebuttonActionPerformed(java.awt.event.ActionEvent evt) { // Save to file String status; doBarcode(); try { if (bcard.saveGraphicToFile(c:\test.png,png,300,0)<0) { status="Unable to create graphics file"; } else status=OK;

} catch (IOException e) { } }

Applet development
The sample applet included with the distribution is very bare bones, and the source code is provided simply as a starting point for developers who wish to make a more advanced applet. Examination of the source code of dbcappXX.java in the build directory will illustrate how easy this is. The init() function is the ideal place to set default barcode parameters. A developer-created applet.class together with the dBarcodeXX.class may be formed into a jar archive for easy deployment using a command of the form Jar cfm myapplet.jar myapplet.class com.dlsoft.dBarcodeXX.dBarcodeXX.class

Servlet development
Servlets run on a server and return html and/or graphic code to a web browser in response to a request from the browser. The simple servlet (dbcservXX.java) accepts data from a browser either through the POST method (as when a submit button is pressed on a browser-displayed form and the browser POSTs the forms parameters back to the server), or through the GET method - when a URL linked from the browser page contains additional parameters, such as: <IMG src=http://localhost/servlet/dbcserv128?CODETYPE=0&DATA=12345678> In this case the servlet returns a image a Portable Network Graphic (PNG) image using the code in the sample servlet. See samples.

Home | About | Contact | FAQs | Information Links | News | Pictures | Products | Services | Site Map | Support Copyright 2010 DL Technology Ltd. All Rights Reserved.
Site operated by DL Technology Ltd, a company registered in England, number 3615271

You might also like