| In this section, we will 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. As mentioned in Setting the Environment, after creating a new project, you will see HelloMIDlet.java file that is created by the project wizard. We will make some modifications in this file to generate the barcode image. |
Working
The screen contains a default “screen item” with hello world text. Delete it, by right clicking on it and choosing “Delete” from the context menu.
The screen will now be empty, without any control. Right click on the screen; choose “New/Add” -> “Text Field” from the context menu. A text field control will be added. Set the label to “Codetext:” and text to “test-123”. The screen would look like the below one after doing these modifications.
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 setSymbologyType(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.setSymbologyType(_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.setForeColor(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;
}
}
The screen contains a default “screen item” with hello world text. Delete it, by right clicking on it and choosing “Delete” from the context menu.
The screen will now be empty, without any control. Right click on the screen; choose “New/Add” -> “Text Field” from the context menu. A text field control will be added. Set the label to “Codetext:” and text to “test-123”. The screen would look like the below one after doing these modifications.
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) { 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 setSymbologyType(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.setSymbologyType(_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.setForeColor(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; } }
Now, open the source view of the HelloMIDlet class and add the instance of SampleBarCodeItem class as a member. Also add the 3 lines in startApp() method as mentioned in the code snippet below to initialize the custom item.
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; }
Now, we need to add a new command to the application to generate the barcode. Go to the screen view of the main form and drag an “Item command” on it. Set its label to “Generate Barcode”.
After that, go to the “Flow” view of the form, right click on the blank area, choose “New/Add…” -> “Call Point”. A new call point will be created. Right click on it and choose “Properties” from the context menu. In call code, specify “generateBarcode(); ”. Then connect the newly added “item command” to this call point.
“generateBarcode” is a public method which is given in the code snippet below. This method will be called when “Generate Barcode” command is selected from the menu.
// method to generate the barcode and render on device screen public void generateBarcode() { bc.setCodeText(txtCodetext.getString()); bc.setSymbologyType(Symbology.Pdf417); bc.repaintBarCode(); }
Build and run the application. If there is no build error, the emulator will open and the application will be loaded. You can enter any value in the codetext text field. For example, change this value to “test-pdf417”, click on the menu and select “Generate Barcode”. The Pdf417 barcode image will now be rendered on the screen. The screenshot is given below:
| A similar example with all source code is also provided with the Aspose.BarCode for Java zip package. |
