• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

save image on disk

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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,


Posts: 9615
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Posts: 43016
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic