|
| |||||||||||||||
|
|
Categories |
|
Complete Mobile ApplicationsThe (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.
|
|
Other J2ME TipsHere are a couple of tips that I have discovered throughout my J2ME programming experience: |
|
Read an image from the device filesystemQuite 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.
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 sizeIt'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!
/**
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.
* 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; } |
|
|
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.
| |||||||||||||||
| |
|||||||||||||||