Base64 编码图像压缩/调整大小

发布于 2024-10-27 05:11:43 字数 119 浏览 1 评论 0原文

我有字符串格式的 Base64 编码图像。需要将其压缩/调整大小到不同的大小,即从这些压缩/调整大小的base64编码图像创建的图像文件具有不同的大小。

Java 中可以使用什么压缩/调整大小算法/jar?

I had base64 encode image in string format. Need to compress/resize it to different size, i.e. Image files created from these compressed/resized base64 encoded images are of different size.

What Compression/resize algorithm/jar can be used in Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如歌彻婉言 2024-11-03 05:11:43

压缩的输出几乎总是二进制数据,而不是字符串......此时开始进行 Base64 转换是毫无意义的。

图像通常已经被压缩(大多数格式都使用压缩),因此您实际上不会获得太多好处。如果您确实需要字符串格式的数据,您可以尝试首先使用GZipOutputStream等压缩原始二进制数据,然后然后对其进行base64编码,但我怀疑你会节省很多空间。

The output of compression is almost always binary data, rather than a string... at which point it's pointless doing the base64 conversion to start with.

Images are usually compressed already (most formats use compression) so you won't actually get much benefit. If you do actually need the data in a string format, you could try compressing the original binary data first using GZipOutputStream etc and then base64 encode it, but I doubt that you'll save much space.

十雾 2024-11-03 05:11:43

我使用这个函数返回 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 "";
 }
}

I'm using this function to return an image .7 the size. (This was for screenshots returned from Selenium.... the image started looking pretty bad if I scaled it down too far.):

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 "";
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文