×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Registered User
    Join Date
    Jan 2011
    Location
    Cvt
    Posts
    7

    Question Help in outputing Image to phone..

    I'm new here and this is my first post..
    After looking and not finding anything that works for a week.. I've decided to ask.

    The problem is that i'm creating a module(function) that would output/save the image to the phone.. And after much trial and error, I could only output a currupted image.
    here's the code:

    Code:
        public void imgOutput(Image outputImg){//Outputs the image file to directory
            int outImgW = outputImg.getWidth();
            int outImgH = outputImg.getHeight();
            int[] pixels = new int[outImgW*outImgH];
            try{
                outputImg.createRGBImage(pixels, outImgW, outImgH, true);
            }catch (Exception thisError){
                switchDisplayable(null, getErrorHand());
                System.err.println(thisError+": Error in Image Output");
            }
            byte[] imgData = null;
    
            try{
                ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
                DataOutputStream dataOut = new DataOutputStream(byteOut);
                for(int i = 0;i < pixels.length;i++){
                    dataOut.write(pixels[i]);
                }
                imgData = byteOut.toByteArray();
            }catch (IOException thisError){
                switchDisplayable(null, getErrorHand());
                System.err.println(thisError+": Error in Image Output");
            }
    
            try{
                dLocation = dLocation + "Compressed.png";//Change to .jpg
                fileCon = (FileConnection)Connector.open(dLocation, Connector.READ_WRITE);
                if(fileCon.exists() == false){
                    fileCon.create();
                }
                outputS = fileCon.openOutputStream();
                outputS.write(imgData);
                outputS.close();
                switchDisplayable(null, getTypeSelection());
            }catch(IOException thisError){
                switchDisplayable(null, getErrorHand());
                System.err.println(thisError+": Error in Image Output");
            }
        }
    I'm new to J2ME and I'm not even sure if I'm on the right track in doing this. Also, please note that my real aim is to save the image in .jpg format, not .png unlike what the code above would do. I'm planning to run this on an S60 3rd Ed.
    Any suggestions or links to a solution would be much appreciated. Advance thanks to those who will reply. And sorry if I posted on the wrong place.

    ~Rei

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

    Re: Help in outputing Image to phone..

    Welcome to Forum Nokia!

    OK... a number of problems.

    First, createRGBImage() is a static method of Image, and creates a new image. It doesn't extract the pixel data from an existing image; you need getRGB() for that.

    Second, this code won't create a PNG. You're just writing a stream of blue channel values. Blue, because you're using:

    Code:
    dataOut.write(pixels[i]);
    which writes only a byte (the last 8 bits of the int you pass). You'd need:

    Code:
    dataOut.writeInt(pixels[i]);
    to write the entire pixel. But this would still not be a PNG. PNG images are a specific format of file, and are not simply a list of pixel values.

    If you take a look at the wiki, you'll find PNG Encoding in Java ME, with code for converting an Image object into a PNG data stream.

    Generating a JPEG is much more complex, because the file format and compression algorithm needed is more complex. I've never seen a JPEG encoder for Java ME. You'd need to find the appropriate file specifications and compression algorithms and write your own from scratch. You might learn to like PNG!

    Graham.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Location
    Cvt
    Posts
    7

    Re: Help in outputing Image to phone..

    Thanks for replying.. that was very informative. I did have it on getRGB at first, but that didn't work. And I didn't know that I need to encode to get a PNG (I guess I'll have to scrap the JPEG idea for now).
    Another question comes to mind. I saw the encoding PNG codes... Can the S60 3rd Ed. resources handle the process? or does it depend on the phone model? I often come up with the OutOfMemory error while doing the previous code.
    I'll try your tips later.. for now I'm off to school. Again, Thanks for the reply ^_^
    ~Rei (Student Programmer)

  4. #4
    Registered User
    Join Date
    Jan 2011
    Location
    Cvt
    Posts
    7

    Re: Help in outputing Image to phone..

    Tried the code and It works like magic in saving the file! Thanks Graham! But the file is larger than expected.. If you know a method of minimizing the size even just a little bit, that would really help.. ^_^
    ~Rei (Student Programmer)

  5. #5
    Super Contributor
    Join Date
    Mar 2005
    Location
    Paris
    Posts
    814

    Re: Help in outputing Image to phone..

    the png encoder provided is lossless, therefore, the compression is very little.
    the easiest would be to have a smaller image. where do you get this image from ?
    another solution would be to have the image compressed on a server,
    or to use another encoder that would indeed compress the image.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Location
    Cvt
    Posts
    7

    Re: Help in outputing Image to phone..

    I get the image from an original file from storage.. then I minimize it's resolution.. then output it to another file. That was the intended goal(To minimize the size of the image by reducing the resolution). I just tried reducing a 64KB png to 40% of it's max resolution.. it took off around 10KB, which already passed the intended goal of minimizing size. But if I could come up with a better way of reducing the file size, that would be great.
    I'm trying to look for a better way of encoding.. thanks for that tip
    ~Rei (Student Programmer)

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

    Re: Help in outputing Image to phone..

    Quote Originally Posted by njzk2 View Post
    the png encoder provided is lossless, therefore, the compression is very little.
    Actually, the implementation on the wiki doesn't have any compression, so the output is pretty big. Basically, 3 * width * height bytes, if you have no alpha channel. You could make the output smaller, but you'd need a proper ZLIB compressor. There's a Java ME implementation somewhere, if someone fancies bolting it into the PNG encoder...

  8. #8
    Registered User
    Join Date
    Jan 2011
    Location
    Cvt
    Posts
    7

    Re: Help in outputing Image to phone..

    yep.. I was looking for a way to incorporate a proper ZLIB or JZLIB compressor code with the codes from the wiki.. so far, I'm still on trial and error on how to go about that. If by some off chance I get it right, I'll post it.
    ~Rei (Student Programmer)

  9. #9
    Super Contributor
    Join Date
    Mar 2005
    Location
    Paris
    Posts
    814

    Re: Help in outputing Image to phone..

    on another thread was mentionned this jpeg encoder :
    http://ce.sharif.edu/~p_amini/j2me/JPEG_Encoder

  10. #10
    Registered User
    Join Date
    Jan 2011
    Location
    Cvt
    Posts
    7

    Re: Help in outputing Image to phone..

    Thanks.. I'll check that. maybe it will work better
    ~Rei (Student Programmer)

  11. #11
    Registered User
    Join Date
    Jan 2011
    Location
    Cvt
    Posts
    7

    Re: Help in outputing Image to phone..

    YOU JUST MADE MY DAY! it worked great! it turned a 68.8KB file into 18.2KB! Thanks for the link njzk2!
    Problem solved!
    This really is the best place to ask.. thanks for all the help guys!
    I'll be doing an mp3 compressor next (by reducing some of the bitrate), if I ever get a big problem with it, i'll post another question.
    ~Rei (Student Programmer)

Similar Threads

  1. i wanna get all images in my phone to add selected to image[ ]
    By buffon2009 in forum Mobile Java General
    Replies: 4
    Last Post: 2010-12-07, 18:16
  2. problems in audio inputing and outputing
    By sujithkannan in forum Symbian
    Replies: 2
    Last Post: 2010-11-18, 14:51
  3. Convert a color image in a black/white image... HELP!
    By xirc85 in forum Mobile Java Media (Graphics & Sounds)
    Replies: 0
    Last Post: 2007-10-18, 14:17
  4. How to show image in Help file
    By itsme in forum Symbian
    Replies: 0
    Last Post: 2007-04-30, 12:28
  5. HELP !!! how to save an image in the phone ???
    By rikow in forum Mobile Java General
    Replies: 1
    Last Post: 2006-07-24, 16:35

Posting Permissions

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