This discussion is archived
7 Replies Latest reply: Feb 13, 2012 9:01 AM by 916969 RSS

Why is the size of new image constructed using Bufferd Image class less?

916969 Newbie
Currently Being Moderated
I am trying to compress image losslessly, for start I what I am doing is:
step1. Access pixels of .jpg image and store in array
Step2. Reconstruct the image from same array

What I have found is, that the size of new image constructed is less than the original.
I am pasting the code below

public static void main(String[] args) {

BufferedImage sourceImage = null;
try {
sourceImage = File("Sample.jpg"));
} catch (IOException e) {

int type = sourceImage.getType();
int w = sourceImage.getWidth();
int h = sourceImage.getHeight();
byte[] pixels = null;
if (type == BufferedImage.TYPE_3BYTE_BGR) {
pixels = (byte[]) sourceImage.getData().getDataElements(0, 0, w, h, null);
try {
BufferedImage edgesImage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
edgesImage.getWritableTile(0, 0).setDataElements(0, 0, w, h, pixels);
ImageIO.write(edgesImage, "jpg", new File("result.jpg"));
} catch (IOException e) {

Please could any one explain me why is it so?


  • Correct Answers - 10 points
  • Helpful Answers - 5 points