jeudi 26 mars 2015

Image size getting decreased after converting it into byte[] using BufferedImage and ImageIO

I am converting an Image into byte[] using following code.



public static byte[] extractBytes (String ImageName) throws IOException {

ByteArrayOutputStream baos=new ByteArrayOutputStream();
BufferedImage img=ImageIO.read(new File(ImageName));
ImageIO.write(img, "jpg", baos);
return baos.toByteArray();
}


Now when I am testing my code:



public static void main(String[] args) throws IOException {
String filepath = "image_old.jpg";
File outp=new File(filepath);
System.out.println("Size of original image="+outp.length());
byte[] data = extractBytes(filepath);
System.out.println("size of byte[] data="+data.length);
BufferedImage img = ImageIO.read(new ByteArrayInputStream(data));
//converting the byte[] array into image again
File outputfile = new File("image_new.jpg");
ImageIO.write(img, "jpeg", outputfile);
System.out.println("size of converted image="+outputfile.length());
}


I am getting very strange results:



Size of original image=78620
size of byte[] data=20280
size of converted image=20244


After converting image into byte[], its size getting decreased by around 1/4th and also when I am converting byte[] back to image its size alters.But output image is successfully getting created in the desired location.


Aucun commentaire:

Enregistrer un commentaire