File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "save image on disk" Watch "save image on disk" New topic

save image on disk

Rupali Raut

Joined: Jun 26, 2008
Posts: 2

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);
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,


Joe Ess

Joined: Oct 29, 2001
Posts: 9189

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.

[How To Ask Questions On JavaRanch]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
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 ]
Rupali Raut

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...


[ June 27, 2008: Message edited by: Rupali Raut ]
I agree. Here's the link:
subject: save image on disk
It's not a secret anymore!