×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    problem with saving resized image

    I've got a problem with getting correct bytes of a newly resized image. The flow is that I retrive an image from the filesystem. Due to the fact that it is large I resize it to a 50x50px thumbnail. I can display this thumbnail in #benchmark1 (see code below). Unfortunately something's wrong with my imageToBytes funtion which returns reasonably small size of image but is totally useless - I can't make an image of it anymore so at #benchmark2 the application either crashes or keeps freezing. I saved this byte array on my disk and tried to preview under Windows how does it look but I got a message "Preview unavailabe". I did some digging in the Internet and I supposed that it's because I don't use any jpg or png encoders to save the file. Actually I think that it's not the case, as the bytes returned from method imageToBytes look weird - I cannot even make a new image of them and display it without any saving in memory.

    Code:
    						byte[] bytes = FileHandler.readFile (
    						        FileHandler.PHOTOS_PATH, fileName);
    						
    						Image img2 = Image.createImage (bytes, 0, bytes.length);
    						img2 = ImageHandler.getInstance ().resize (img2);
                                                    //#benchmark1
    						bytes = ImageUtils.imageToBytes (img2);
    						
    						img2 = Image.createImage (bytes, 0, bytes.length);
                                                    //#benchmark2
    my imageToBytes function is as follows:

    Code:
    	public static byte[] imageToBytes (Image img)
    	{
    		int[] imgRgbData = new int[img.getWidth () * img.getHeight ()];
    		byte[] imageData = null;
    		try
    		{
    			img.getRGB (imgRgbData, 0, img.getWidth (), 0, 0, img.getWidth (),
    			        img.getHeight ());
    		}
    		catch (Exception e)
    		{
    		}
    		ByteArrayOutputStream baos = new ByteArrayOutputStream ();
    		DataOutputStream dos = new DataOutputStream (baos);
    
    		try
    		{
    			for (int i = 0; i < imgRgbData.length; i++)
    			{
    				dos.writeInt (imgRgbData[i]);
    			}
    
    			imageData = baos.toByteArray ();
    			baos.close ();
    			dos.close ();
    		}
    		catch (Exception e)
    		{
    
    		}
    		return imageData;
    	}
    I've run totally out of any idea what's wrong, please help!

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: problem with saving resized image

    Welcome to Forum Nokia!

    Quote Originally Posted by ba_sta666 View Post
    it's because I don't use any jpg or png encoders
    Yes, you're absolutely correct.

    Quote Originally Posted by ba_sta666 View Post
    Code:
    img2 = Image.createImage (bytes, 0, bytes.length);
    I'm guessing this line throws an IllegalArgumentException, which means that the method cannot understand the format of the data.

    Image.createImage(byte[], int, int) is guaranteed only to understand PNG, though on most devices it will understand JPEG too. But you have invented your own image format.

    Try using a PNG encoder.

    Graham.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Re: problem with saving resized image

    it works, thanks!
    the other option was to save image in RMS but then we are limited in cache size

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: problem with saving resized image

    In Place of //#benchmark1
    bytes = ImageUtils.imageToBytes (img2);

    img2 = Image.createImage (bytes, 0, bytes.length);
    //#benchmark2

    Try Image.getRGB() and Image.CreateRGBImage() methods of MIDP.

    Instead of RMS you can save Image to filesystem also.
    Thanks,
    Ekta

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Re: problem with saving resized image

    Thanks for your answer. Yeah of course I can do both and since I got PNG encoder saving in filesystem works as well.

Similar Threads

  1. Saving an image
    By chandran.biju007 in forum Mobile Java General
    Replies: 6
    Last Post: 2011-01-12, 18:54
  2. Images are getting resized?
    By cleclercq in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2009-11-10, 11:12
  3. Problem in saving and retreiving image from RMS
    By vineetbillorey in forum Mobile Java General
    Replies: 10
    Last Post: 2009-03-23, 22:47
  4. Problem saving an image using FileConnection
    By pmgf_14 in forum Mobile Java General
    Replies: 7
    Last Post: 2008-08-18, 23:41
  5. saving an image
    By gaurav17281 in forum Symbian User Interface
    Replies: 1
    Last Post: 2003-06-16, 05:28

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×