繁体   English   中英

如何在下载图像之前确定图像大小和尺寸

[英]How can I determine image size and dimension before downloading the image

我有JPEG,GIF和PNG文件的图像URL。 我想检查这些网址中的图片是否小于特定尺寸。

在此基础上,我想下载图像。

在Java中有ImageIO库,但是在AWT库中,我需要Android的东西。

使用URLConnection方法getContentLength()可以轻松获得图像大小

 URL url = new URL("https://www.google.com.pk/images/srpr/logo4w.png");
 URLConnection conn = url.openConnection();

  // now you get the content length
 int contentLength = conn.getContentLength();
 // you can check size here using contentLength

 InputStream in = conn.getInputStream();
 BufferedImage    image = ImageIO.read(in);
  // you can get size  dimesion      
  int width          = image.getWidth();
  int height         = image.getHeight(); 

无论Chrylis说什么都是对的。 您只能在下载后才能这样做。 首先下载您的图像文件并将其保存到特定路径说它文件夹

然后从那里读取它并使用下面的代码获得它的高度和宽度:

BufferedImage readImage = null;

try {
    readImage = ImageIO.read(new File(folder);
    int h = readImage.getHeight();
    int w = readImage.getWidth();
} catch (Exception e) {
    readImage = null;
}

编辑:要在Android中获取ImageIO类,请尝试以下操作:

转到项目属性 * java build pata->添加库 *

并添加JRE系统库并单击完成 现在你可以使用java.awt包 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM