Tuesday, January 27, 2009

Code: Image Scaling in J2ME

1 comments

This function scales images using an optimized form of bresenham's formula. You can also add bilinear sampling and such to it but I figured since it's on a cell phone it won't matter much and will also cause it to be slower.







public static Image scaleImage( Image original, int newWidth,

int newHeight) {


int[] rawInput = new int[original.getHeight()


* original.getWidth()];


original.getRGB(rawInput, 0, original.getWidth(), 0, 0,


original.getWidth(), original.getHeight());


int[] rawOutput = new int[newWidth * newHeight];


// YD compensates for the x loop by substracting the width


//back out.


int YD = (original.getHeight() / newHeight)


* original.getWidth() – original.getWidth();


int YR = original.getHeight() % newHeight;


int XD = original .getWidth() / newWidth;


int XR = original.getWidth() % newWidth;


int outOffset = 0;


int inOffset = 0;


for (int y= newHeight, YE= 0, y > 0; y--) {


for (int x= newWidth, XE= 0; x > 0; x--) {


rawOutput[outOffset++] = rawInput[inOffset];


inOffset += XD;


XE += XR;


if (XE >= newWidth) {


XE –= newWidth;


inOffset++;


}


}


inOffset += YD;


YE += YR;


if (YE >= newHeight) {


YE –= newHeight;


inOffset += original.getWidth();


}


}


return Image.createRGBImage(rawOutput, newWidth, newHeight, false);


}


 

Code: Enable the backlight and prevent the screen from turning off

0 comments

Some BlackBerry device screens, such as those on the BlackBerry 7100 series, become blank when not in use. BlackBerry device users can configure the screen timeout period.

You might prefer to control when the screen is turned on or off. For example, when implementing a peripheral keyboard you can simulate key events to make sure that the screen does not turn off while the BlackBerry device user types or navigates on the BlackBerry device.

The following code specifies that the screen backlight, once activated, has a timeout period of 30 seconds.

Code:

if(( Display.getProperties()
  & Display.DISPLAY_PROPERTY_REQUIRES_BACKLIGHT) != 0 )
{
    Backlight.enable( true, 30 );
}

The backlight.enable method only turns the backlight on for the period of time specified, up to a maximum of 255 seconds. BlackBerry device user activity that occurs while the backlight is on resets the value specified in this method.

However, some applications might require the backlight to stay on for longer than 255 seconds. For example, in navigation software design, the backlight might be required for extended periods of time even though BlackBerry device user activity is not continuous (e.g., while travelling). For this scenario, you can set a timer to turn the backlight on every 255 seconds.

Remember that BlackBerry device backlight usage consumes additional power. Excessive usage has a negative impact on the BlackBerry device battery.

Note: The code sample above requires code signing. See the Controlled APIs and Code Signing page for more information.

Code: Alert.startBuzzer() does not work

0 comments

Alert.startBuzzer() and Alert.setBuzzerVolume() are not supported by the BlackBerry 7100 Series device and later devices.

The BlackBerry 7100 Series device and later devices contain a hardware upgrade that changes the way audio is produced.
Resolution

As an alternative to Alert.startBuzzer(), we recommend Alert.startMIDI(). To determine the method to use, the following tests are performed:

if( Alert.isBuzzerSupported() )
{
Alert.startBuzzer(…);
} else if( Alert.isMIDISupported() )
{
Alert.startMIDI(…);
}

Note: If you use a polyphonic tone, use the above test to check the availability of Alert.startADPCM(). You can do so via Alert.isADPCMSupported().

Code: Add plain text or binary files to an application

1 comments

The class java.lang.Class contains methods that enable you to import a plain text or a binary file as an input stream. Once the contents of the file are in an input stream it can be read appropriately into the necessary format. Below is an example on how to import a plain text file into an application.

1. Open a text editor and add some text. For example, Test test test
2. Save the file as test.
3. Create a new project.
4. Add the file text to the project.
5. Create a new file called FileDemo.java.

The source for FileDemo.java looks as follows;
Code:
package com.packagename.stuff;


import java.io.*;

import net.rim.device.api.system.*;


class FileDemo extends Application {


public static void main(String args[]) {

FileDemo theApp = new FileDemo();

theApp.enterEventDispatcher();

}


public FileDemo() {

try {

//The class name is the fully qualified package name followed

//by the actual name of this class

Class classs = Class.forName("com.packagename.stuff.FileDemo");


//to actually retrieve the resource prefix the name of the file with a "/"

InputStream is = classs.getResourceAsStream("/test");


//we now have an input stream. Create a reader and read out

//each character in the stream.

InputStreamReader isr = new InputStreamReader(is);

char c;

while ((c = (char)isr.read()) != -1) {

System.out.print(c);

}

} catch(Exception ex) {

System.out.println("Error: " + ex.toString());

}

}

}
When the above demo is run in the simulator the debugger will show the output of "Test test test" or the contents in the test file.

Code: Add a custom menu item to an existing BlackBerry application

0 comments

Procedure

The ApplicationMenuItem class will allow the addition of a custom menu item to various screens within BlackBerry specific applications, or as a system-wide setting. The following example illustrates how to add a menu item to the email view screen.

Obtain an instance of the MenuItemRepository to add the menu item.
Code:
myMenuItem myMenuitem = new MyMenuItem(0);

ApplicationMenuItemRepository
.getInstance().addMenuItem
(ApplicationMenuItemRepository
.MENUITEM_EMAIL_VIEW,myMenuitem);

Create a class so that certain methods within the ApplicationMenuItem class can be overridden.

Code

class MyMenuItem extends ApplicationMenuItem{
 
    //using the default constructors here.
    MyMenuItem(int order){
        super(order);
    }
 
    //methods we must implement
    //Run is called when the menuItem is invoked
    public Object run(Object context){
        //context object should be a email message
        if (context instanceof Message){
            Message message = (Message)context;
            //this is where we would work the message
          //do something here
        }
        return context;
    }
 
    //toString should return the string we want to
    //use as the lable of the menuItem
    public String toString(){
        return "MyMenu Name";
    }
}

It should be noted that Object context that is passed into the run method will depend on the screen to which the menu item has been added. In this example, the context is an email message because the menu item has been added to the email view screen. If the item was added to the address book, it would return a personal information management (PIM) item.

Code : Allow an application to restart itself

0 comments

A Java application for a BlackBerry device is able to shut down and restart itself. Prior to exiting, the application should stop all threads and free up resources. The following code shows a simple application that will continuously exit and restart itself.







public final class RestartSample extends Application {

public static void main(String[] args) {


RestartSample theApp = new RestartSample();


theApp.enterEventDispatcher();


}


public RestartSample() {


System.out.println(“Application Started…”);


//Get the current application descriptor.


ApplicationDescriptor.currentApplicationDescriptor();


System.out.println(“Scheduling the restart in 1 minute…”);


/* Schedules are rounded to the nearest minute so ensure the application is scheduled for at least 1 minute in the future.*/


ApplicationManager.getApplicationManager


().scheduleApplication(current,


System.currentTimeMillis() + 60001, true);


System.out.println(“Application is exiting…”);


System.exit(0);


}


}

Feature Story : Blackberry Smartphones

0 comments


BlackBerry smartphones provide unparalleled wireless access to email, corporate data, phone, instant messaging, Internet and organizer information. Whatever your needs, there's a BlackBerry smartphone for you. You can get the full BlackBerry experience, plus multimedia capabilities and expandable memory with the BlackBerry Curve 8300 Series, BlackBerry 8800 Series and BlackBerry Pearl 8100 Series smartphones.