【发布时间】:2015-10-12 22:19:37
【问题描述】:
我想使用Gravatar,但我不想publish users MD5 hashes 他们的电子邮件地址。还有more potential problems。所以我决定下载它们并将它们存储在我的数据库中。
但是我的个人资料图片(Earlybird)在下载后看起来很糟糕:
这是我使用的代码。
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
final URL url = new URL("http://www.gravatar.com/avatar/" + account.getGravatarHash() + "?d=identicon");
final BufferedImage image = ImageIO.read(url);
ImageIO.write(image, "jpg", baos);
pic = baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
pic 中的值然后直接存储到数据库中。我数据库中的其他图片都存储得很好,所以问题一定出在这些行中。
编辑:
我只是通过将"jpg" 更改为"png" 来部分解决问题,甚至认为Gravatar tutorial 提到了"jpg"。我也不想指定图像格式(除非所有 Gravatar 都是 png)。我可以避免吗?我只想保存我得到的字节。
【问题讨论】:
-
单元测试拯救世界。针对从本地文件加载的同一张图片测试您的图片 byte[]。