home
home profile hosting info news fun stuff Technical Stuff support contact us
J2ME PROGRAMMING TIPS

Categories

Complete Mobile Applications

The (growing) list below shows all the mobile applications I've written in time and thought might be useful for other users as well. Feel free to download them, distribute them to your friends, install them on your phone and use them. Also, if you have an idea of a mobile application that you would want implemented, why not use the Support Page to send us a message about it.
Note: Please make sure you select Mobile Application Suggestion from the list so your request doesn't get ignored.

Other J2ME Tips

Here are a couple of tips that I have discovered throughout my J2ME programming experience:

Read an image from the device filesystem

Quite often in your mobile application you might need to allow the user to specify an image that s/he has already stored in the device filesystem (e.g. mobile memory, memory stick etc). This piece of code shows you how to read the image from the filesystem assuming you already have the file name.
Note that this can only apply to devices that support the FileConnection API (JSR 75). For details on this JSR, please visit the Java Community Process on www.jcp.org.
Simply add the following function to your class and invoke it with the filename:

protected Image createImage( String name ) throws IOException
{
 InputStream is = Connector.openInputStream( "file:///" + name );
 int read;
 byte readData[] = null;
 while( (read = is.read(buffer)) != -1 )
 {
  if( readData == null )
  {
   readData = new byte[read];
   System.arraycopy( buffer, 0, readData, 0, read );
  }
  else
  {
   byte tmp[] = new byte[readData.length + read];
   System.arraycopy( readData, 0, tmp, 0, readData.length );
   System.arraycopy( buffer, 0, tmp, readData.length, read );
   readData = null; // suggest to the gc that we don't need it!
   readData = tmp;
   tmp = null; // suggest to the gc that we don't need it!
  }
 }
 is.close();
 Image img = Image.createImage( readData, 0, readData.length );
 return img;
}

Scale an image to fit the screen size

It's more and more common with the new generation of mobiles nowadays to have a megapixel camera built in the mobile itself. And with that comes the fact that if you ever want to allow your user to select one of the images s/he has captured with this camera (see above) you are dealing with very large images. So if at some point all you want to do is to allow your user to browse through these images one at a time (so you will have to scale the image such that it fits the screen), you will be using a lot of memory for the purpose of showing the user just a 320x240 image!
In such cases, it is good to have a "scaling" function, that reduces the image to just the size of screen, thus reducing the memory consumption and also improving the "painting" time (the time it takes your application to draw the image onto the scren/canvas).
Simply add the following function to your class and invoke it passing it the Image object you want rescaled to the size of the screen:

/**
 * Function used to scale each image down/up to the size of the screen.
 * Adapted from http://forum.benqmobile.com/jive3/thread.jspa?threadID=16310&messageID=66929
 * Note that we rely on the fact that screenWidth and screenHeight are member of this class,
 * already initialized to the size of the screen!
 *
 * @param image Image to scale
 * @return Scaled version of the image, having the width and height equal to the
 * size of the screen.
 */
protected Image createScaledImage( Image image )
{
 final int sourceWidth = image.getWidth();
 final int sourceHeight = image.getHeight();
 final Image sImg = Image.createImage( screenWidth, screenHeight );
 final Graphics g = sImg.getGraphics();
 final int[] lineRGB = new int[sourceWidth];
 final int[] sourcePos = new int[screenWidth]; // cache for x positions
 {
  /*
   * Pre-calculate x positions with modified bresenham algorithm
   * http://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html
   */
  int y = 0;
  int eps = -(sourceWidth >> 1);
  for( int x = 0; x < sourceWidth; x++ )
  {
   eps += screenWidth;
   if ( (eps << 1) >= sourceWidth )
   {
    if( ++y == screenWidth )
     break;
    sourcePos[y] = x;
    eps -= sourceWidth;
   }
  }
 }
 for( int y = 0; y < screenHeight; y++ )
 {
  image.getRGB( lineRGB, 0, sourceWidth, 0, y * sourceHeight / screenHeight, sourceWidth, 1 );
  for( int x = 1; x < screenWidth; x++ ) // skip pixel 0
   lineRGB[x] = lineRGB[sourcePos[x]];
  g.drawRGB( lineRGB, 0, screenWidth, 0, y, screenWidth, 1, false );
 }
 return sImg;
}
IMPORTANT: The above function seems to only work when scaling images DOWN. I am currently working on making it work to scale images up as well though so stay tuned.

Go back to the Technical page.

Go back to the main page.

Home | Profile | Hosting | Info | News | Fun Stuff | Tech | Support | Contact Us | J2ME tips and news

© Copyright liviutudor.com.
Based on a template from TemplatesBox.com

Valid CSS! Valid XHTML 1.0 Transitional Get FireFox! Powered by Debian Linux Powered by Apache Web Server No Software Patents View My profile on LinkedIn

Liviu Tudor's mobile applications download free J2ME mobile applications download the source code for J2ME mobile applications J2ME mobile applications for Sony Ericsson and Nokia phones technical tips programming Java java j2me mobile knowledge base request a J2ME mobile application