我正在使用以下代码将图像转换为byte[]。
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();
}
现在当我测试我的代码时:
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());
}
我得到了非常奇怪的结果:
Size of original image=78620
size of byte[] data=20280
size of converted image=20244
将图像转换为字节数组后,其大小减小了约四分之一,当我将byte[]转换回图像时,其大小也会改变。但是,在所需位置成功创建输出图像。在放大500-600%后,我可以看到原始图像和新图像的轻微差异。缩放后新图像有些模糊。
这里是我正在测试的图像 http://pbrd.co/1BrOVbf 请解释此大小更改的原因,并告诉我是否有任何方法可以在此之后获得相同的大小。