为什么使用 ImageIO 无法从 URL 获取 BufferedImage [英] Why use ImageIO can't get BufferedImage from URL

查看:63
本文介绍了为什么使用 ImageIO 无法从 URL 获取 BufferedImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

imageURL: https://fbexternal-a.akamaihd.net/safe_image.php?d=AQBB77GLfY75FNWL&安培; W = 720&安培; H = 2048&安培; URL = HTTP%3A%2F%2Fwww.facebook.com%2Fads%2Fimage%2F%3FD%3DAQI0duFYFcmydWNutbwmSk2DfOmHcDrhPfsMJTUoEObbWkVzYUtrHgCuN_LFrWcPRzJi6jPgbn80oFs0Kj_WrdROjdnJkjbnS5-UJv9l9cJyhKCWS-LR-MXlc263Ul3Txe-VFqXfRrA6BOjt4DF-Sww2&安培; EXT =最好

URL url = new URL(imageURL);
BufferedImage image = ImageIO.read(url);

URL url = new URL(imageURL);
BufferedImage image = ImageIO.read(url.openStream());

结果 image 为空?为什么?

the result image is null? why?

推荐答案

ImageIO.read(URL) 确实支持从您描述的 URL 读取图像,但是,它只支持有限的一组图像格式.内置格式有 JPEG、PNG、GIF、BMP 和 WBMP.还有许多其他格式的插件,如 TIFF、JPEG 2000 等.

ImageIO.read(URL) does support reading images from URL like you describe, however, it does support only a limited set of image formats. Built-in formats are JPEG, PNG, GIF, BMP and WBMP. There are plugins for many other formats, like TIFF, JPEG 2000, etc.

问题在于链接的图像不是任何内置格式,而是在 WEBP 中格式,一种由谷歌创建的新图像格式,目前还没有被广泛使用.它在您的浏览器(和我的 :-) )中显示良好的原因很可能是您使用的是 Chrome,并且 Chrome 内置了对 WEBP 的支持.

The problem is that the linked image is not in any of the built-in formats, it's in WEBP format, a new image format created by Google, and which does not have very widespread use yet. The reason it displays fine in your browser (and mine :-) ), is most likely that you are using Chrome, and Chrome has built-in support for WEBP.

至少有一个 WEBP ImageIO 插件可用.如果你构建并安装了这个插件,你上面的代码应该可以正常工作并且读取图像就好了.如果启动应用程序时插件位于类路径上,则不需要调用 ImageIO.scanForPlugins().

There's at least one WEBP ImageIO plugin available. If you build and install this plugin, your code above should work and read the image just fine. There should be no need to invoke ImageIO.scanForPlugins() if the plugin is on class path when you launch your application.

这篇关于为什么使用 ImageIO 无法从 URL 获取 BufferedImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆