Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Al Hobbs
  • salvin francis

Sample Mobile Device App. to Generate & Display Barcode Image

Ranch Hand
Posts: 714
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


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



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







        // set properties







        // create an image

        if(_img == null)


            _img = Image.createImage(w,h);


        // get Graphics

        Graphics g1 = _img.getGraphics();



        // render the barcode image on the display area


        g.drawImage(_img,0,0,Graphics.TOP | Graphics.LEFT);


    // call this method to draw barcode image

    public void repaintBarCode() {



    // get barcode image

    public Image getImage() {

        return this._img;


private SampleBarCodeItem bc;

public void startApp()


    if (midletPaused)








        // initialize the custom item

        bc = new SampleBarCodeItem("Aspose-BarCode Generation ");


        bc.setPreferredSize(300, 200);


    midletPaused = false;


// method to generate the barcode and render on device screen

public void generateBarcode()






More about Aspose.BarCode for Java

Contact Information

Aspose Pty Ltd

Suite 163, 79 Longueville Road

Lane Cove, NSW, 2066


Aspose – Your File Format Experts

Phone: 888.277.6734

Fax: 866.810.94651

You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic