This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes save image on disk Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "save image on disk" Watch "save image on disk" New topic
Author

save image on disk

Rupali Raut
Greenhorn

Joined: Jun 26, 2008
Posts: 2
Hi,

I am using following code to save an pixel array as an jpg image on disk

public static BufferedImage toImage(byte[] pixels, int w, int h) {
DataBuffer db = new DataBufferByte(pixels, w*h);
WritableRaster raster = Raster.createInterleavedRaster(db,
w, h, w, 1, new int[]{0}, null);
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorModel cm = new ComponentColorModel(cs, false, false,
Transparency.OPAQUE ,DataBuffer.TYPE_BYTE);
return new BufferedImage(cm, raster, false, null);
}


public static void saveImage(String filename,byte OriginalByte[], int width1,int height1)
{
BufferedImage image1 = toImage(OriginalByte, width1, height1);
try{
ImageIO.write(image1, "jpg", new File(filename));
}
catch(IOException e){}
}

I saved pixel array of 1024(32*32) bytes on disk as image.jpg and pixel array of 800(40*20) bytes as image1.jpg on disk using above code.

But the size of image1.jpg on disk is greater then image.jpg.

why this is happening?

Please help,

Thanks,

Rupali
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8705
    
    6

Welcome to the JavaRanch.
JPEG is a compression format so it is quite possible that a "larger" image size on screen could be shrunk down smaller on disk than a "smaller" display size, depending on the content of the image. Without knowing exactly what the images contain we can only speculate.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
JPEG applies various algorithms to compress the image. Those may work better on one image than the other (they depend on the image content).

An extreme example would an image that is 1 million pixels large, but all pixels have the same color: that would be much smaller than an image that's 10000 pixels large, yet contains 10000 different pixel values.

PS: Which, as I now see, is just about the same Joe said.
[ June 26, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - updated with new look and Ping home screen widget
Rupali Raut
Greenhorn

Joined: Jun 26, 2008
Posts: 2
Thanks a lot.....

I am studing lossless image compression.

I use a jpg image of 200 kb as input image.
I applied 3 lossless image compression techniques, decompress it,and saved them as .jpg images using above code.
All 3 files are look like original image and each image size is 120 kb.

when I save original 200 kb image as .bmp it gives 300kb bmp image.
also when I save 120 kb image as .bmp it also gives 300kb bmp image.

Also.. when I save original 200 kb image again as .jpg it gives 200kb jpg image.

and when I save 120 kb image as .jpg it gives 130kb jpg image.

then why the size is different 200 and 120 as I am using lossless compresion techniques.(ie.input pixel data is same as output pixel data)

Please help...

Thanks,

Rupali.
[ June 27, 2008: Message edited by: Rupali Raut ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: save image on disk
 
Similar Threads
Creating an image from a byte array and a color palette
Problem in loading/saving images using ImageIO
bufferd Image!
changing gray pixels....
byte or int array into BufferedImage