Results 1 to 17 of 17
  1. #1
    sruthi_2009 is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default [SOLVED] Storing an image in the system as a file

    hi all

    I have been trying to store an image in the system. The problem I am encountering is that I have to convert an object of Image class into an object of BufferedImage class.

    Here is the sample code I have written:
    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.ImageIO;
    
    public class mig3{
    public static void main(String args[]){
    //loading an image
    Image one=null;
    int iw,ih,i;
    int pixels[];
    try { 
    File f = new File("C:/One.jpg");
    BufferedImage sl = ImageIO.read(f);
    one= (Image)sl;
    
    }catch(Exception en){en.printStackTrace(); System.out.println("Error! "+en);}
    
    //transforming to an array
    
    iw=one.getWidth(null);
    ih=one.getWidth(null);
    pixels = new int[iw * ih];
    try{
    PixelGrabber grabber = new PixelGrabber(one, 0, 0, iw, ih, pixels,0,iw); 
    grabber.grabPixels();
    } catch (InterruptedException ie) { }
    /*System.out.println("The pixel values are:\n");
    for(i=0;i<iw*ih;i++)
           System.out.println("\t"+pixels[i]);*/
    
    int [] pix = (int [])pixels;
    Canvas c=new Canvas();
    Image one1=c.createImage(new MemoryImageSource(iw,ih,pix,0,iw));
    //Storing
    try{
    File fil = new File("C:/One1.jpg");
    [COLOR="Red"]//I need the buffered image to use the write method here[/COLOR]
    
    ImageIO.write([COLOR="Red"]/*Buffered Image must come here*/[/COLOR], "jpg", fil);
    }catch(Exception en){en.printStackTrace(); System.out.println("Error! "+en);}
    }
    }
    Is there any other method to store the image as a file?

    Any help is appreciated!
    Thank you.
    with warm regards,
    Sruthi :)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    If you want to add to your code, the easiest thing is to construct a new BufferedImage of the same size, and drawImage to its Graphics context.

    But you should also be able to setRGB the pixel array to a BufferedImage and do away with the Image altogether.

    db

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    I agree with Daryl. Just construct the BufferedImage first, get its graphics context, and do a g.drawImage(...your image).

    (of course, in the exmaple you gave, 'one' is already a BufferedImage, because it's really 's1', so you could have just manipulated it as a BufferedImage and then called ImageIO.write)

  4. #4
    sruthi_2009 is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    @ Darryl and @ toadaly

    Thank you. I will try and let you know
    with warm regards,
    Sruthi :)

  5. #5
    sruthi_2009 is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Question Is this code correct?

    Hello all!!

    I have finally tried to do this to construct an image from the pixel array and store it as a file in the system.
    I am doing a project on steganography.
    I want to know if this code is right and will work for every other case.
    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.ImageIO;
    public class img{
    public static void main(String args[]){
    //loading an image
    Image one=null;
    int iw,ih,i;
    int pixels[];
    try { 
    File f = new File("C:/Winter.jpg");
    BufferedImage sl = ImageIO.read(f);
    one= (Image)sl;
    
    //transforming to an array
    
    iw=one.getWidth(null);
    ih=one.getHeight(null);
    pixels = new int[iw * ih];
    try{
    	PixelGrabber grabber = new PixelGrabber(one, 0, 0, iw, ih, pixels,0,iw); 
    	grabber.grabPixels();
        } catch (InterruptedException ie) { }
    
    int [] pix = (int [])pixels;  //Here actually the array is modified. didnt write the code here
    BufferedImage bi = new BufferedImage(iw,ih,sl.getType());
    bi.setRGB (0, 0, iw, ih, pix, 0, iw); 
    //Storing
    try{
    	File fil = new File("C:/y.jpg");
    	ImageIO.write(bi, "jpg", fil);
         }catch(Exception en){en.printStackTrace(); System.out.println("Error! "+en);}
    }catch(Exception en){en.printStackTrace(); System.out.println("Error! "+en);}
    }
    }
    I am getting an OutOfMemoryException - something about insufficient heap space for images of 3296x2472 pixels(this is a pic taken from my digicam)

    The winter image is the basic image found in sample pictures of Windows of 800x600 pixels

    Please help me in this regard.

    Any help is appreciated!

    Thank you :)
    Last edited by sruthi_2009; 03-19-2009 at 08:51 AM. Reason: a small mistake in the code
    with warm regards,
    Sruthi :)

  6. #6
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Increase your heap size with a -Xmx128m switch on the command line. If that still isn't enough, bump it to 256m.

  7. #7
    sruthi_2009 is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    Could you please explain in detail on how to increase the size of the heap?
    Thank you
    with warm regards,
    Sruthi :)

  8. #8
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Java Code:
    java -Xmx128m....

  9. #9
    sruthi_2009 is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    @toadaly

    thank you :)

    @anyone

    Can anyone please tell me if my code is correct?
    with warm regards,
    Sruthi :)

  10. #10
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    This is not going to work for all image types:

    Java Code:
    BufferedImage bi = new BufferedImage(iw,ih,sl.getType());
    Since you used a pixel grabber constructor with the default RGB color model, your data is now in ARGB format regardless of what it originally was. You should explicitly set your BufferedImage to TYPE_INT_ARGB. It should then work with any color model type for input JPEGs.

    By the way, it looks like you've reimplemented BufferedImage.getSubImage(). You could compress all this down to 3 lines of code and not have to worry about messing with various image types.

  11. #11
    sruthi_2009 is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    @ toadaly
    Thanks for the reply!

    I dont want a subimage. I want to manipulate the pixels of an original image and construct a new image from the manipulated pixel values. Thats the reason I used that.

    Well, you said that the data would be in TYPE_INT_ARGB format for any color model type for input JPEG's. what if the input is bmps??
    with warm regards,
    Sruthi :)

  12. #12
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Quote Originally Posted by sruthi_2009 View Post
    Well, you said that the data would be in TYPE_INT_ARGB format for any color model type for input JPEG's. what if the input is bmps??
    Any Image will be converted to ARGB format with the PixelGrabber constructor you are using. Since you are wanting to manipulate pixels, that's a good thing.

  13. #13
    sruthi_2009 is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    @toadaly

    Thanks!

    @anyone

    OK. Well I have written a code as per the algorithm specified for steganography. It worked well for dummy pixel values which I gave.But on taking actual data from image, I am not getting the correct message while extracting.
    Why is that? Any one got any ideas??
    Any help is appreciated. Thank you.
    with warm regards,
    Sruthi :)

  14. #14
    sruthi_2009 is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    hello all!!

    I have manipulated the pixel values of an image and stored that image as a file.Now when I am extracting the pixels from the stored files, the pixels I have manipulated are not the same. I am getting some different values. why is this happening??

    Anybody any ideas??
    Please help me!! This is very urgent as I have to submit my project in a week and I am unable to figure this out!! :(
    Any help is appreciated!!
    Thank you
    with warm regards,
    Sruthi :)

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    JPEG is a lossy compression.

    db

  16. #16
    sruthi_2009 is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    @ Darryl
    Thanks! I didnt know it would affect this. But my code is working fine for jpeg images. For BMP images - which is lossless compression, I am getting Null Pointer Exception. Why is that??
    with warm regards,
    Sruthi :)

  17. #17
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Quote Originally Posted by sruthi_2009 View Post
    @ Darryl
    Thanks! I didnt know it would affect this. But my code is working fine for jpeg images. For BMP images - which is lossless compression, I am getting Null Pointer Exception. Why is that??
    You need to be more specific and show your code. There could be 10,000 reaons for a NPE.

Similar Threads

  1. Storing data from text file in ArrayList
    By tjhodge in forum New To Java
    Replies: 1
    Last Post: 02-12-2009, 01:22 PM
  2. where i put my jar file in my system?
    By makpandian in forum New To Java
    Replies: 11
    Last Post: 02-11-2009, 04:51 AM
  3. File System
    By Zosden in forum Advanced Java
    Replies: 3
    Last Post: 07-26-2008, 04:33 AM
  4. To open an image file such as Jpeg file using JAva Program
    By itmani2020 in forum Advanced Java
    Replies: 10
    Last Post: 07-11-2008, 09:57 AM
  5. storing and retrieving a file as such
    By anil_manu in forum Advanced Java
    Replies: 0
    Last Post: 03-11-2008, 01:27 PM

Posting Permissions

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