我正在尝试使用以下代码在neo4j中插入1 MB图像:
File fnew = new File("C:\\Users\\myimage.jpg");
BufferedImage originalImage = ImageIO.read(fnew);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.flus();
ImageIO.write(originalImage, "jpg", baos);
return baos.toByteArray();
然后我使用:
插入这个字节数组 user.setProperty("photo", photo);
这一切都很顺利。当我尝试使用以下方法选择照片时,它会将其作为 536KB 而不是1 MB原始大小写在我的硬盘驱动器磁盘上。
byte[] imageInByte = (byte[]) user.getProperty("photo");
InputStream in = new ByteArrayInputStream(imageInByte);
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert, "jpg", new File("C:\\newimage.jpg"));
现在奇怪的部分:我可以看到图像,打开它,相同的分辨率,我看不出质量方面的任何差异。看起来像是压缩的。
为什么会这样?
答案 0 :(得分:3)
通过ImageIO保存jpg图像会导致jpg的有损压缩(我相信质量默认为70%)。您可以a)在写入文件时更改图像的质量(参见Setting jpg compression level with ImageIO in Java)或b)如果您实际上不需要BufferedImage,只需读取/写入从文件到数据库的字节。