IT Community - Software Programming, Web Development and Technical Support

Resizing images in Midlets (Mobile)

This is a discussion on Resizing images in Midlets (Mobile) within the Mobile Software Development forums, part of the Software Development category; Resizing images in Midlets (Mobile)...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Mobile Software Development

Register FAQ Members List Calendar Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 07-19-2007, 10:41 PM
itbarota itbarota is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 547
itbarota is on a distinguished road
Default Resizing images in Midlets (Mobile)

Resizing images in Midlets (Mobile)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-20-2007, 01:02 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default Re: Resizing images in Midlets (Mobile)

You can use the following code to resize images in J2ME Midlets.

// fixed point constants
private static final int FP_SHIFT = 13;
/** fast resampling mode - no antialiase */
public static final int FAST_RESAMPLE = 0;
/** slow resampling mode - with antialiase */
public static final int SLOW_RESAMPLE = 1;

/**
* resizeImage
* Gets a source image along with new size for it and resizes it.
* @param src The source image.
* @param destW The new width for the destination image.
* @param destH The new heigth for the destination image.
* @param mode A flag indicating what type of resizing we want to do: FAST_RESAMPLE or SLOW_RESAMPLE.
* @return The resized image.
*/
Image resizeImage(Image src, int destW, int destH, int mode)
{
try
{
int srcW = src.getWidth();
int srcH = src.getHeight();

// create pixel arrays
int[] srcPixels = new int[srcW];
int[] destPixels = new int[destW * destH]; // array to hold destination pixels

// precalculate src/dest ratios
int ratioW = (srcW << FP_SHIFT) / destW;
int ratioH = (srcH << FP_SHIFT) / destH;
if (mode == FAST_RESAMPLE)
{
long ini = System.currentTimeMillis();
// simple point smapled resizing
// loop through the destination pixels, find the matching pixel on the source and use that
int p =0;
for (int destY = 0; destY < destH; ++destY)
{
int srcY = (destY * ratioH) >> FP_SHIFT; // calculate beginning of sample
// int srcY = (destY * srcH) / destH;
src.getRGB(srcPixels,0,srcW,0,srcY,srcW,1);
for (int destX = 0; destX < destW; ++destX)
{
int srcX = (destX * ratioW) >> FP_SHIFT; // calculate beginning of sample
// int srcX = (destX * srcW) / destW;
destPixels[p++] = srcPixels[srcX];
}
}
System.out.println("fast: "+(System.currentTimeMillis()-ini)+"ms");
}
else
{
long ini = System.currentTimeMillis();

byte[] tmpR = new byte[destW * srcH]; // temporary buffer for the horizontal resampling step
byte[] tmpG = new byte[tmpR.length];
byte[] tmpB = new byte[tmpR.length];
byte[] tmpA = new byte[tmpR.length];

// variables to perform additive blending
int argb; // color extracted from source
int a, r, g, b; // separate channels of the color
int count; // number of pixels sampled for calculating the average

// the resampling will be separated into 2 steps for simplicity
// the first step will keep the same height and just stretch the picture horizontally
// the second step will take the intermediate result and stretch it vertically

// horizontal resampling
int p =0;
for (int y = 0; y < srcH; ++y)
{
src.getRGB(srcPixels,0,srcW, 0,y, srcW,1);
for (int x = 0; x < destW; ++x)
{
int srcX = (x * ratioW) >> FP_SHIFT; // calculate beginning of sample
int srcX2 = ((x + 1) * ratioW) >> FP_SHIFT; // calculate end of sample
if (srcX2 >= srcW) srcX2 = srcW-1;

count = srcX2 - srcX + 1;
// now loop from srcX to srcX2 and add up the values for each channel
for (a= r = b = g = 0; srcX <= srcX2; srcX++)
{
argb = srcPixels[srcX];
a += (argb >> 24) & 0xFF;
r += (argb >> 16) & 0xFF;
g += (argb >> 8) & 0xFF;
b += argb & 0xFF;
}
// average out the channel values
tmpA[p] = (byte)(a/count);
tmpR[p] = (byte)(r/count);
tmpG[p] = (byte)(g/count);
tmpB[p] = (byte)(b/count);
p++;
}
}

// vertical resampling of the temporary buffer (which has been horizontally resampled)
for (int x = 0; x < destW; ++x)
for (int y = 0, xx=x; y < destH; y++, xx += destW)
{
int srcY = (y * ratioH) >> FP_SHIFT; // calculate beginning of sample
int srcY2 = ((y + 1) * ratioH) >> FP_SHIFT; // calculate end of sample
if (srcY2 >= srcH) srcY2 = srcH-1;

count = srcY2 - srcY + 1;
// now loop from srcY to srcY2 and add up the values for each channel
p = x + srcY * destW;
for (a = r = b = g = 0; srcY <= srcY2; srcY++, p += destW)
{
a += tmpA[p] & 0xFF;
r += tmpR[p] & 0xFF; // red channel
g += tmpG[p] & 0xFF; // green channel
b += tmpB[p] & 0xFF; // blue channel
}
// recreate color from the averaged channels and place it into the destination buffer
destPixels[xx] =((a/count) << 24) | ((r/count) << 16) | ((g/count) << 8) | (b/count);
}
System.out.println("slow: "+(System.currentTimeMillis()-ini)+"ms");
}
srcPixels = null;
// return a new image created from the destination pixel buffer
return Image.createRGBImage(destPixels,destW,destH,true);
// note that if you put back alpha support, have to change false above to true or the alpha channel will be ignored
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/mobile-software-development/2251-resizing-images-midlets-mobile.html
Posted By For Type Date
Digg / Technology / Upcoming This thread Refback 07-20-2007 01:21 AM

Similar Threads
Thread Thread Starter Forum Replies Last Post
How u Reduce Image Memory Usage in Game Midlets itbarota Mobile Software Development 1 07-23-2007 11:19 PM
How you Create silent midlets itbarota Mobile Software Development 1 07-23-2007 07:06 AM
Which is the best preferred development environment for Java Midlets on Windows OS? itbarota Mobile Software Development 1 07-19-2007 01:04 AM
Resizing windows tripnautic HTML, CSS and Javascript Coding Techniques 4 04-03-2007 10:11 AM
dynamically resizing accordion component nssukumar Flash Actionscript Programming 1 03-09-2007 03:55 AM


All times are GMT -7. The time now is 11:30 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0