从URL加载图像部分

从URL加载图像部分

本文介绍了从URL加载图像部分,就好像有什么在WhatsApp的实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WhatsApp的开发者最近改进了图片加载中立即加载图像的某些部分加载整个图像之后(获得其尺寸和图像的一些像素),然后替换为完整图像占位符:

WhatsApp developers recently improved the image loading in which immediately loading some portion of the image (getting its dimension and some pixels of the images) and then after loading the entire image, replace the placeholder with the full image:

我的问题是,他们是如何实现的呢?做它们通过读取其标题(元数据)读出的图像的尺寸?如何对图像内容?或者,他们有两个版本的图像在服务器端,一个较小的低质量的被装载第一和一个更大的是完整的图像?需要注意的是,如果它是第二种方法则他们仍然需要提取图像的较小版本在服务器侧,一旦从发送者接收到的图像。任何其他办法?

My question is, how did they implement it? Do they read the dimension of the image by reading its header (meta-data)? How about the image content? Or do they have two versions of the image at the server-side, a smaller one with low-quality which is loaded first and a bigger one which is the full image? Note that if it's the second approach then they still need to extract the smaller version of the image at the server side once receiving the image from the sender. Any other approaches?

推荐答案

经过多次实验,我得到了尺寸,而无需下载整个图像:

After several experiments, I got the dimensions without downloading the entire image:

String address = "image_url";
URL url = new URL(address);
URLConnection urlC = url.openConnection();
urlC.connect();
InputStream is = urlC.getInputStream();
for(int i = 0; i < 92; i++) is.read();

nt byte1 = is.read();
int byte2 = is.read();

int width = (byte1 << 8) + byte2;

for(int i = 0; i < 10; i++) is.read();

byte1 = is.read();
byte2 = is.read();

int height = (byte1 << 8) + byte2;

System.out.println("width = " + width + " | height = " + height);

is.close();

这篇关于从URL加载图像部分,就好像有什么在WhatsApp的实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:36