This technical tip shows how to create a sample mobile application in Netbeans IDE to generate a barcode image. We will provide a textbox for getting the codetext value, generate a Pdf417 2D barcode image and display it on the mobile screen.
Open Netbeans IDE and create a new mobile device application. Please refer to Setting the Environment for preparing the development environment and adding reference to Aspose.BarCode.J2ME.jar package. We will use this textbox to get the codetext from screen for generating the barcode.
For rendering the barcode image on screen, we will use a custom item. For this purpose, we will create a new class which will extend from “CustomItem” class. This class will add an image on the screen area and render the barcode image to the display. Public methods will be added in this class to generate and render the barcode image from the application. Below is the complete code for this class. Add this class to your sample project, by copying the code.
[Java]
package hello;
import com.aspose.j2me.barcode.generation.BarCodeBuilder;
import com.aspose.j2me.barcode.generation.Symbology;
import com.aspose.j2me.barcode.generation.GraphicsUnit;
import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
* Sample class for using Aspose.BarCode for J2ME
* Shows how to create a CustomItem to paint barcodes.
*/
public class SampleBarCodeItem extends CustomItem {
protected SampleBarCodeItem(String s) {
super(s);
}
protected int getMinContentWidth() {
return 200;
}
protected int getMinContentHeight() {
return 200;
}
protected int getPrefContentWidth(int i) {
return i;
}
protected int getPrefContentHeight(int i) {
return i;
}
// default codetext
private String _codeText = "12345";
// symbology
private long _symbology = Symbology.CODE128;
private Image _img = null;
// set codetext
public void setCodeText(String code)
{
this._codeText = code;
}
// set symbology
public void setSymbology(long sym)
{
this._symbology = sym;
}
// instance of BarCodeBuilder class
private BarCodeBuilder barcodeBuilder;
// paint method for displaying the barcode image
protected void paint(Graphics g, int w, int h) {
// initialize with default values, if null
if(barcodeBuilder == null)
{
barcodeBuilder = new BarCodeBuilder(_symbology,_codeText);
}
else
{
barcodeBuilder.setSymbology(_symbology);
barcodeBuilder.setCodeText(_codeText);
}
// set properties
barcodeBuilder.setGraphicsUnit(GraphicsUnit.PIXEL);
barcodeBuilder.setXDimension(2);
barcodeBuilder.setBarHeight(50);
barcodeBuilder.setWideNarrowRatio(2);
barcodeBuilder.setBorderVisible(false);
barcodeBuilder.setCodeTextVisible(true);
// create an image
if(_img == null)
{
_img = Image.createImage(w,h);
}
// get Graphics
Graphics g1 = _img.getGraphics();
g1.setColor(0xFFFFFFFF);
g1.fillRect(0,0,w,h);
// render the barcode image on the display area
barcodeBuilder.render(g1);
g.drawImage(_img,0,0,Graphics.TOP | Graphics.LEFT);
}
// call this method to draw barcode image
public void repaintBarCode() {
this.repaint();
}
// get barcode image
public Image getImage() {
return this._img;
}
}
private SampleBarCodeItem bc;
public void startApp()
{
if (midletPaused)
{
resumeMIDlet();
}
else
{
initialize();
startMIDlet();
// initialize the custom item
bc = new SampleBarCodeItem("Aspose-BarCode Generation ");
this.form.append(bc);
bc.setPreferredSize(300, 200);
}
midletPaused = false;
}
// method to generate the barcode and render on device screen
public void generateBarcode()
{
bc.setCodeText(txtCodetext.getString());
bc.setSymbology(Symbology.PDF417);
bc.repaintBarCode();
}
More about Aspose.BarCode for Java
Contact Information
Aspose Pty Ltd
Suite 163, 79 Longueville Road
Lane Cove, NSW, 2066
Australia
Aspose – Your File Format Experts
sales@aspose.com
Phone: 888.277.6734
Fax: 866.810.94651