我有base64编码图像的字符串格式。需要将其压缩/调整为不同大小,即从这些压缩/调整大小的base64编码图像创建的图像文件大小不同。
Java中可以使用什么压缩/调整大小算法/ jar?
答案 0 :(得分:1)
压缩的输出几乎总是二进制数据,而不是字符串......此时,以base64转换开始是毫无意义的。
图像通常已被压缩(大多数格式使用压缩),因此您实际上不会获得太多好处。如果你确实需要字符串格式的数据,你可以尝试首先使用GZipOutputStream
等压缩原始二进制数据然后然后 base64编码它,但是我怀疑你会节省很多空间。
答案 1 :(得分:0)
我使用此功能返回图像.7大小。 (这是从Selenium返回的屏幕截图....如果我将它缩小得太远,图像开始看起来非常糟糕。):
public String SeventyPercentBase64(String in_image)
{
String imageData = in_image;
//convert the image data String to a byte[]
byte[] dta = DatatypeConverter.parseBase64Binary(imageData);
try (InputStream in = new ByteArrayInputStream(dta);) {
BufferedImage fullSize = ImageIO.read(in);
// Create a new image .7 the size of the original image
double newheight_db = fullSize.getHeight() * .7;
double newwidth_db = fullSize.getWidth() * .7;
int newheight = (int)newheight_db;
int newwidth = (int)newwidth_db;
BufferedImage resized = new BufferedImage(newwidth, newheight, BufferedImage.SCALE_REPLICATE);
Graphics2D g2 = (Graphics2D) resized.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
//draw fullsize image to resized image
g2.drawImage(fullSize, 0, 0, newwidth, newheight, null);
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
ImageIO.write( resized, "png", baos );
baos.flush();
byte[] resizedInByte = baos.toByteArray();
Base64Encoder enc_resized = new Base64Encoder();
String out_image = enc_resized.encode(resizedInByte);
return out_image;
}
} catch (IOException e) {
System.out.println("error resizing screenshot" + e.toString());
return "";
}
}