Monday, May 31, 2010

How to scale an image to fix mobile screen

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 subtracting 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);
}

Sunday, March 28, 2010

contact number from phone book : using j2me

public static Vector retrieveContactInfo()
{
Vector vContactInfo = new Vector();

//Check that PIM Optional Package is available
String pimVersion = System.getProperty("microedition.pim.version");
if(pimVersion == null)
return null;
else
{
PIM pim = PIM.getInstance();
ContactList contactList = null;
try
{
contactList = (ContactList) pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
if(contactList == null)
{
return null;
}

Enumeration clEnum = contactList.items();
while(clEnum.hasMoreElements())
{
Contact contact = (Contact) clEnum.nextElement();
String name = "";

try
{
//if(contactList.isSupportedField(Contact.FORMATTED_NAME))
{
name = contact.getString(Contact.FORMATTED_NAME, 0);
}
}
catch(Exception ex) //An unexpected error occurred while reading the contact name.
{
continue;
}

if(!contactList.isSupportedField(Contact.TEL))
{
continue;
}

int noOfPhones = contact.countValues(Contact.TEL);
Vector vTelInfo = new Vector();
for(int i = 0; i < contactattribute =" contact.getAttributes(Contact.TEL," number =" contact.getString(Contact.TEL," teltype = "" contactattribute ="="" teltype = "Home" contactattribute ="="" teltype = "Mobile" contactattribute ="="" teltype = "Work" contactattribute ="="" teltype = "Other" contactattribute ="="" teltype = "Pager" contactattribute ="="" teltype = "General"> 0)
{
vContactInfo.addElement(new ContactInfo(name, vTelInfo));
}
}
}
catch (PIMException ex)
{
return null;
}
catch (SecurityException ex)
{
return null;
}

return vContactInfo;
}

Thursday, March 4, 2010

String manipulation, split string with specified delimeter and store into vector

public class StringManip {

public static Vector split(String str, final String delim, boolean discardEndData) {
Vector vComponents = new Vector();
int index = 0;

while (index < str.length()) {
int newIndex = str.indexOf(delim, index);
if (newIndex > 0) {
vComponents.addElement(str.substring(index, newIndex));
} else {
if (discardEndData == false) {
vComponents.addElement(str.substring(index));
}
break;
}

index = newIndex + delim.length();
}

return vComponents;
}

example :-
Vector v = StringManip.split("aaa,aaaa,aaa,aa" , ",", false);

Thursday, February 25, 2010

J2me source code to find the screen size to work the application in different screen size

import com.sun.lwuit.Display;

/**
* This class enumerates the different phone resolution categories.
* Note that enums are not supported in source 1.3.
* @author Sirajuddin
*/

public abstract class PhoneDisplay {

public static int LOWRES = 0;
public static int MEDRES = 1;
public static int HIGHRES = 2;

public static int getCategory(Display display)
{
int width = display.getDisplayWidth();
int height = display.getDisplayHeight();

int diagonal = (int) Math.sqrt((width * width) + (height * height));

if(diagonal < 205)
return LOWRES;

return HIGHRES;
}
};