×

Discussion Board

Results 1 to 7 of 7

Thread: Saving an image

  1. #1
    Super Contributor
    Join Date
    Nov 2008
    Posts
    675

    Saving an image

    Hi All,
    I am newbie in Mobile Java. In my application i want click a photo,save the image and then send the image to a webservice as base64 binary.
    The webservice accepts images in PNG, GIF, JPEG, BMP.

    I have taken a snapshot of the image and tried to save it as jpeg as follows
    Code:
    private void saveImageToMobile(Image aImage)
    {
    
    try
    {
    final FileConnection fc = (FileConnection) Connector.open("file:///c:/Data/Images/a.jpg");
    if(!fc.exists())
    {
    fc.create();
    final OutputStream out;
    out = fc.openOutputStream();
    //Create MediaProcessor for raw Image
    final MediaProcessor mediaProc = GlobalManager.createMediaProcessor("image/raw");
    //Get control over the format
    ImageFormatControl formatControl = (ImageFormatControl)
            mediaProc.getControl("javax.microedition.amms.control.ImageFormatControl");
    //Set necessary format
    formatControl.setFormat("image/jpeg");
    mediaProc.setInput(aImage);
    mediaProc.setOutput(out);
    mediaProc.start();
    mediaProc.addMediaProcessorListener(new MediaProcessorListener() {
    
                        public void mediaProcessorUpdate(MediaProcessor mp, String string, Object o) {
    
                            if(mediaProc.getState()==mediaProc.STOPPED){
                                try {
                                    out.flush();
                                    out.close();
                                    fc.close();
                                 } catch (IOException ex) {
                                    ex.printStackTrace();
                                }
                            }
                        }
                    });
    
    
    }
    
    
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    But the image created is of 0B and cannot be opened.
    I checked various posts on this subject but could not find any right pointers.
    Also instead of saving this in a image can i send the raw image.What is the format of the raw image.Is it going to .bmp??

    Regards
    Biju

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

    Re: Saving an image

    I'm afraid MediaProcessors won't work. They're unlikely to be implemented on the phone.

    I suggest you read the wiki: How to write data to a file in Java ME and PNG Encoding in Java ME.

    Graham.

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

    Re: Saving an image

    @ Graham,

    If the photo is clicked by using VideoControl.getSnapshot(null), as per API document it returns the byte[] in PNG format only.

    Will saving it in fileSystem and giving it a .PNG extension save it as a PNG file ? (which can be opened by any other PNG file viewer)

    or Do we still need to add PNGEncoding Signature & headers manually to it to make it compatible to PNG standards?

    From API Doc
    Code:
    getSnapshot
    
    public byte[] getSnapshot(java.lang.String imageType) throws MediaException
    
    Gets a snapshot of the displayed content. Features and format of the captured image are specified by the imageType. All implementations support at least PNG format. Available formats can be queried from System.getProperty with video.snapshot.encodings as the key.
    
    Parameters:
    
    imageType - Format and resolution of the returned image. If null is given, it will default to using the PNG format.
    
    Returns: image as a byte array in required format.
    
    Throws:
    IllegalStateException - Thrown if initDisplayMode has not been called.
    MediaException - Thrown if the requested format is not supported.
    SecurityException - Thrown if the caller does not have the security permission to take the snapshot.
    Thanks,
    Ekta

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

    Re: Saving an image

    Quote Originally Posted by ektasrv View Post
    f the photo is clicked by using VideoControl.getSnapshot(null), as per API document it returns the byte[] in PNG format only.

    Will saving it in fileSystem and giving it a .PNG extension save it as a PNG file ?
    Yes. If you simply snap the image as a byte[], and then save it, you're OK.

    However, once you convert it to an Image (for example, if you want to resize it or modify it in some other way), then you need to convert it back to a byte[], in a suitable format. Unfortunately, there's no easy way to do this. Much better if you keep the original byte[] returned from getSnapshot(), which, as you say, is already in a useful format.

    Graham.

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

    Re: Saving an image

    Quote Originally Posted by chandran.biju007 View Post
    Hi All,
    I am newbie in Mobile Java. In my application i want click a photo,save the image and then send the image to a webservice as base64 binary.
    The webservice accepts images in PNG, GIF, JPEG, BMP.

    I have taken a snapshot of the image and tried to save it as jpeg as follows

    Also instead of saving this in a image can i send the raw image.What is the format of the raw image.Is it going to .bmp??

    Regards
    Biju
    if you have taken the snapshot using MMAPI Camera in J2me i.e. using - VideoControl.getSnapshot(null)
    ---> See example: http://wiki.forum.nokia.com/index.ph...ots_in_Java_ME

    the byte[] that you get is already in PNG format and you can just convert it to base64 before sending it to the Webservice no need to use MediaProcessor.

    If you want to store this as PNG image in the fileSystem use JSR-75 File Connection and store it ina filename with .png extension.
    Last edited by ektasrv; 2010-06-23 at 12:09.
    Thanks,
    Ekta

  6. #6
    Registered User
    Join Date
    Sep 2009
    Location
    Bangalore, India
    Posts
    49

    Re: Saving an image

    follow up the below links for the same...

    Converting Image to bytes
    Saving and retriving image in RMS

    Hope it ll help u....
    Regards
    Nilanchala

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    20

    Re: Saving an image

    Hi, Iknow this post is quite old, but may be this will help some one. I wrote a post about sending an Image to the mobile client in Base64 format as part of a JSON String. The String is received by the mobile client and the image is converted back to bytes and to an Image again. Check it out:

    http://www.java-n-me.com/2011/01/ser...avame-iii.html

    Hope it helps!

Similar Threads

  1. Image combination and saving !!!
    By divyas in forum Mobile Java General
    Replies: 1
    Last Post: 2006-10-19, 13:19
  2. Saving capture image
    By ckt in forum Symbian Media (Closed)
    Replies: 0
    Last Post: 2006-09-26, 19:47
  3. Is it possible for saving animated image
    By mottaisami in forum Mobile Java General
    Replies: 1
    Last Post: 2005-12-06, 08:06
  4. saving an image
    By gaurav17281 in forum Symbian User Interface
    Replies: 1
    Last Post: 2003-06-16, 05:28
  5. Image Preview without saving
    By kashifqasim in forum Mobile Commerce
    Replies: 2
    Last Post: 2003-06-11, 10:20

Posting Permissions

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