如何将图像对象转换为二进制blob [英] How to convert an image object to a binary blob
问题描述
是否可以在DOM中解析一个图像对象(< img>
),就像它已经上传并用FileReader打开一样?我正尝试使用 jpegmeta.js 从JPEG中提取EXIF元数据,但它需要由FileReader返回的二进制字符串。
Is it possible to parse an image object (<img>
) in the DOM as if it had been uploaded and opened with FileReader? I am trying to use jpegmeta.js to extract EXIF metadata from JPEGs, but it needs binary strings which are returned by the FileReader.
使用XHR从URL加载图像并使用FileReader解析它可能是可能的。但是,如果每个图像必须下载两次,这将产生大量开销。另一种选择可能是使用画布,但在转换时会丢失exif数据。
It might be possible to use XHR to load the image from its URL and parse it with FileReader. However, this would incur a lot of overhead if every image has to be downloaded twice. Another option could have been to use the canvas, but that loses the exif data when converting.
推荐答案
普遍的共识似乎是总结在这个答案:在JavaScript中获取图像数据?
The general consensus seems to be summed up in this answer: Get image data in JavaScript?
因此,如果您需要exif数据,则需要使用XHR技术。希望浏览器能够缓存图像,因此不会有太大的性能损失。否则画布技术应该完成这项工作。
So if you need the exif data, you'll need to use the XHR technique. Hopefully the browser will have the image cached, so it won't be too much of a performance hit. Otherwise the canvas technique should do the job.
这篇关于如何将图像对象转换为二进制blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!