Results 1 to 17 of 17
- 02-18-2009, 03:41 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
[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:
Is there any other method to store the image as a file?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);} } }
Any help is appreciated!
Thank you.with warm regards,
Sruthi :)
- 02-19-2009, 01:43 AM #2
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
- 02-20-2009, 06:30 AM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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)
- 02-20-2009, 07:41 AM #4
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
@ Darryl and @ toadaly
Thank you. I will try and let you knowwith warm regards,
Sruthi :)
- 03-16-2009, 04:02 PM #5
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
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.
I am getting an OutOfMemoryException - something about insufficient heap space for images of 3296x2472 pixels(this is a pic taken from my digicam)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);} } }
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 :)
- 03-17-2009, 01:28 AM #6
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Increase your heap size with a -Xmx128m switch on the command line. If that still isn't enough, bump it to 256m.
- 03-17-2009, 02:29 PM #7
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
Could you please explain in detail on how to increase the size of the heap?
Thank youwith warm regards,
Sruthi :)
- 03-17-2009, 03:46 PM #8
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Java Code:java -Xmx128m....
- 03-18-2009, 03:07 AM #9
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
@toadaly
thank you :)
@anyone
Can anyone please tell me if my code is correct?with warm regards,
Sruthi :)
- 03-18-2009, 10:38 PM #10
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
This is not going to work for all image types:
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.Java Code:BufferedImage bi = new BufferedImage(iw,ih,sl.getType());
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.
- 03-19-2009, 09:02 AM #11
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
@ 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 :)
- 03-19-2009, 03:30 PM #12
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
- 03-19-2009, 04:58 PM #13
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
@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 :)
- 03-20-2009, 09:32 AM #14
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
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 youwith warm regards,
Sruthi :)
- 03-20-2009, 12:49 PM #15
JPEG is a lossy compression.
db
- 03-20-2009, 03:14 PM #16
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
@ 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 :)
- 03-23-2009, 05:18 AM #17
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Similar Threads
-
Storing data from text file in ArrayList
By tjhodge in forum New To JavaReplies: 1Last Post: 02-12-2009, 01:22 PM -
where i put my jar file in my system?
By makpandian in forum New To JavaReplies: 11Last Post: 02-11-2009, 04:51 AM -
File System
By Zosden in forum Advanced JavaReplies: 3Last Post: 07-26-2008, 04:33 AM -
To open an image file such as Jpeg file using JAva Program
By itmani2020 in forum Advanced JavaReplies: 10Last Post: 07-11-2008, 09:57 AM -
storing and retrieving a file as such
By anil_manu in forum Advanced JavaReplies: 0Last Post: 03-11-2008, 01:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks