我有base64编码图像的字符串格式。需要将其压缩/调整为不同的大小,即从这些压缩/调整大小的base64编码图像创建的图像文件大小不同。base64编码的图像压缩/调整大小
在Java中可以使用什么压缩/调整大小算法/ jar?
我有base64编码图像的字符串格式。需要将其压缩/调整为不同的大小,即从这些压缩/调整大小的base64编码图像创建的图像文件大小不同。base64编码的图像压缩/调整大小
在Java中可以使用什么压缩/调整大小算法/ jar?
压缩的输出几乎总是二进制数据,而不是一个字符串......在这一点上,base64转换开始时毫无意义。
图像通常已经被压缩(大多数格式使用压缩),所以你实际上不会获得太多好处。如果你真的需要字符串格式的数据,你可以尝试首先使用GZipOutputStream
etc和然后 base64对它进行压缩,但是我怀疑你会节省很多空间。
我正在使用此功能返回图像.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 "";
}
}