This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sample Mobile Device App. to Generate & Display Barcode Image  RSS feed

sherazam khan
Ranch Hand
Posts: 587
  • Mark post as helpful
  • send pies
  • 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

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!