Tutorials J2ME How to scale Image in J2ME

How to scale Image in J2ME

Below code snippet is use to scale image in J2ME. It takes the new width and height and scales as per aspect ratio.

public static Image scale(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];
    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);
 }
}
Total
0
Shares
Previous Post
j2me_tutorials_javatechig_featured_image

How to Send MMS from J2ME

Next Post
j2me_tutorials_javatechig_featured_image

LWUIT source code Download

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All