我的应用下载Facebook用户的jpeg个人资料图片和base64编码。对其进行解码时,生成的JPEG质量和尺寸会显着减小。我如何避免这种情况?下载后显着jpeg质量损失和base64编码
这里是我下载/编码方式:
// Download the profile picture.
BufferedImage image = ImageIO.read(new URL("http://facebook-profile-pic"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(image, "jpeg", bos);
// Base64 encode it.
String imageData = new String(Base64.getEncoder().encode(bos.toByteArray()));
当我走的imageData的价值,并对其进行解码,图像比一个在原来的下载URL的方式更小。
'新的字符串(字节阵列)'是不可靠的,因为它使用的底层系统的默认字符集编码的字节数。如果该字符集是UTF-8或某些ISO-8859字符集,它将适用于Base64编码的字节,但如果是其他内容,则您的数据将被损坏。使用'String imageData = Base64.getEncoder()。encodeToString(bos.toByteArray());'而不是。 – VGR
JPEG是无损文件格式,写入时ImageIO使用特定质量。为了获得更高质量和/或无损,您需要明确指定质量 – copeg