我正在使用imagescalr来调整图像大小,但我还想压缩图像以使大小保持在32kb以下(因为8支持base64字符串高达32kb)。我正在调整像这样的图像
Map resizeImage(BufferedImage imageData, int width, int height, String imageFormat){
BufferedImage thumbnail = Scalr.resize(imageData, Scalr.Method.SPEED, Scalr.Mode.FIT_EXACT ,
width, height, Scalr.OP_ANTIALIAS);
ByteArrayOutputStream baos = new ByteArrayOutputStream()
ImageIO.write(thumbnail, imageFormat, baos)
baos.flush()
byte[] imageBytes = baos.toByteArray()
baos.close()
return [imageBytes:imageBytes, imageFormat:imageFormat]
}
我想要一种方法,以便我可以压缩,最少更改代码。
答案 0 :(得分:0)
你可以尝试
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream gzip = new GZIPOutputStream(baos);
ImageIO.write(thumbnail, imageFormat, baos)
gzip.close();
byte[] zippedImageBytes = baos.toByteArray();
解压缩
InputStream in = new GZIPInputStream(new ByteArrayInputStream(zippedImageBytes));
....