Regarding using Images in J2ME

Forum for J2ME mobile games related topics including programming doubts, books and other resources for J2ME game development

Regarding using Images in J2ME

Postby uvrpavan » Tue Nov 27, 2007 11:18 am

Hi,
I am using the below code for zooming the images.but its taking lot of time.Is threre any other Optimised code for this?
If yes tell me how?
Code i am using................
private Image createThumbnail(Image image,int w)
{
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
int thumbWidth = w;
int thumbHeight = -1;

if (thumbHeight == -1)
thumbHeight = thumbWidth * sourceHeight / sourceWidth;

Image thumb = Image.createImage(thumbWidth, thumbHeight);
Graphics g = thumb.getGraphics();

for (int y = 0; y < thumbHeight && y<screen_height; y++)
{
for (int x = 0; x < thumbWidth && x<screen_width; x++)
{
g.setClip(x, y, 1, 1);
int dx = x * sourceWidth / thumbWidth;
int dy = y * sourceHeight / thumbHeight;
g.drawImage(image, x - dx, y - dy, Graphics.LEFT | Graphics.TOP);
}
}

Image immutableThumb = Image.createImage(thumb);

return immutableThumb;
}



This code is working well but taking a lot of time with increasing the Image size.

if i try to use the code with Image.getRGB then i am getting out of memory Exception at creation of 2-dimensional integer array.
i.e..,
int rgb[][]= temp.getRGB(rgb,0,temp.getWidth(),0,0,temp.getWidth(),temp.getHeight());
can u suggest me the better solution for this?
uvrpavan
 
Posts: 19
Joined: Sun Feb 11, 2007 8:52 am

Re: Regarding using Images in J2ME

Postby DevelopmentTeam » Tue Nov 27, 2007 11:40 am

I am sure your code without getRGB will take hours to complete zooming a big image.
And the second one will be the best option.

meanwhile let me know how big is the image, so that we can do some trick to avoid out of memory...
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: Regarding using Images in J2ME

Postby uvrpavan » Tue Nov 27, 2007 11:57 am

Hi,

My image sizes of 400 * 600.
what r the methods zoom this ?


SonyEriccson is not opening the files of size more than 1200*1200.
how can i do this with java ?

give me some more information regarding these images handling.............
uvrpavan
 
Posts: 19
Joined: Sun Feb 11, 2007 8:52 am

Re: Regarding using Images in J2ME

Postby DevelopmentTeam » Tue Nov 27, 2007 12:04 pm

now, 400x600 is comparatively high for mobile phones to open and process. What you can do is, read only the specific portion of the image which you are going to show on the screen, if you are zooming in.

Steps:
1) open the image using createimage
2) use createImage(Image image, int x, int y, int width, int height, int transform) and read the portion which you are going to zoom in
3) now use getRGB and zoom your data and create another image again.

Note: at any point, dont create an Image of size greater than the size of the screen.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: Regarding using Images in J2ME

Postby uvrpavan » Tue Nov 27, 2007 12:16 pm

i am having lot of methods for zooming In/Out the Images
can u give me the optimized code to reduce the stress on the mobile?
uvrpavan
 
Posts: 19
Joined: Sun Feb 11, 2007 8:52 am

Re: Regarding using Images in J2ME

Postby DevelopmentTeam » Wed Nov 28, 2007 4:19 am

Trying zooming an image with the public function.

The other private function is the normal zooming function and the public is a wrapper function to it which optimizes the memory usage.
Now the maximum buffer used is not more than the screen size. And even with this optimization some devices may fail, in that case we cannot open a large image.

Code: Select all
    public static Image ZoomIn(Image Img, int zoomx) {
        int ImgHeight = Img.getHeight();
        int ImgWidth = Img.getWidth();
        int outHeight = ImgHeight * zoomx;
        int outWidth = ImgWidth * zoomx;
        int portionx = 0, portiony = 0;

        if (outHeight > Store.DisplayHeight) {
            portiony = (Store.DisplayHeight / zoomx) + 1;
        } else {
            portiony = ImgHeight;
        }

        if (outWidth > Store.DisplayWidth) {
            portionx = (Store.DisplayWidth / zoomx) + 1;
        } else {
            portionx = ImgWidth;
        }

        Image newImage = Image.createImage(Img, 0, 0, portionx, portiony, 0);

        return ZoomInx(newImage, zoomx);
    }

    private static Image ZoomInx(Image Img, int zoomx) {
        int ImgHeight = Img.getHeight();
        int ImgWidth = Img.getWidth();
        int outHeight = ImgHeight * zoomx;
        int outWidth = ImgWidth * zoomx;

        int[] ImgRGBdata = new int[ImgWidth * ImgHeight];
        int[] NewImgRGBdata = new int[outWidth * outHeight];

        Img.getRGB(ImgRGBdata, 0, ImgWidth, 0, 0, ImgWidth, ImgHeight);

        int j = 0;
        if (zoomx <= 1) {
            return Img;
        }
        for (int i = 0; i < ImgRGBdata.length; i++) {

            for (int k = 0; k < zoomx; k++) {

                NewImgRGBdata[j] = ImgRGBdata[i];
                j++;
            }

            if (j % outWidth == 0) {
                for (int l = 0; l < outWidth; l++) {
                    for (int m = 0; m < (zoomx - 1); m++) {
                        NewImgRGBdata[j] = NewImgRGBdata[j - outWidth];
                        j++;
                    }
                }
            }
        }

        Image ZoomedImg = Image.createRGBImage(NewImgRGBdata, outWidth, outHeight, true);
        return ZoomedImg;
    }
}
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: Regarding using Images in J2ME

Postby AnArChYm » Fri Jan 04, 2008 10:30 am

I also am having problems with opening JPEG files...Iam trying to open over 150kb images but an out of memory error show up...I would like either:

1)Is athere anyway to resize the image withour creating the image first?
2)creating partial of the jpg image and show it in tiles?

Note: Using other format is not an option since I am loading the files using the FileConnection API (files which are stored in the gallery on the phone)
AnArChYm
 
Posts: 1
Joined: Fri Jan 04, 2008 10:24 am

Re: Regarding using Images in J2ME

Postby DevelopmentTeam » Sat Jan 05, 2008 4:34 am

I dont think you can resize images before opening it. It is always a compulsion in J2ME that you need to load an image atleast once before cropping a portion or resizing it. 150Kb is a bit high and when you try to open higher resolution images it much harder. I dont think you can achieve this without native code.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India


Return to J2ME Games

Who is online

Users browsing this forum: No registered users and 1 guest

cron