我有一个字节数组(byte[] imgData
)中的图像数据。我想获得它的元数据,例如:从字节[]提取图像元数据
- 尺寸
- 彩色/黑白&白
- 文件类型(JPEG,PNG,...)
- ...
我该怎么做?如果有一个我必须知道的图书馆,请告诉我。我发现Getting metadata from JPEG in byte array form但它说它与JPEG图像有关。我想为所有图像做到这一点。另外,它没有解释它是如何工作的。
我有一个字节数组(byte[] imgData
)中的图像数据。我想获得它的元数据,例如:从字节[]提取图像元数据
我该怎么做?如果有一个我必须知道的图书馆,请告诉我。我发现Getting metadata from JPEG in byte array form但它说它与JPEG图像有关。我想为所有图像做到这一点。另外,它没有解释它是如何工作的。
不幸的是,支持所有想像中的图像格式(或将要被考虑的)是不实际的。有太多了。使用标准的J2SE,我们可以满足从ImageIO.getReaderFileSuffixes()
获得的String[]
中返回的类型。如this answer所示。
将Java高级映像添加到运行时为映像格式(包括TIFF)添加支持(通过服务提供程序接口)。
感谢您的答案。我很抱歉,但这怎么回答我的问题?只是为了澄清,我不是在寻找“每一种形式的图像格式”。只是更多使用的就足够了。 –
顺便说一句,downvote不是从我! –
为此,您可以使用普通的ImageIO:
ImageInputStream stream = ImageIO.createImageInputStream(new ByteArrayInputStream(imgData); // assuming imgData is byte[] as in your question
Iterator<ImageReader> readers = ImageIO.getImageReaders(stream);
if (!readers.hasNext()) {
// We don't know about this format, give up
}
ImageReader reader = readers.next();
reader.setInput(stream);
// Now query for the properties you like
// Dimensions:
int width = reader.getWidth(0);
int height = reader.getHeight(0);
// File format (you can typically use the first element in the array):
String[] formats = reader.getOriginatingProvider().getFormatNames();
// Color model (note that this will return null for most JPEGs, as Java has no YCbCr color model, but at least this should get you going):
ImageTypeSpecifier type = reader.getRawImageType(0);
ColorModel cm = type.getColorModel();
// ...etc...
对于你可能想看看IIOMetadata
更高级的属性,但我发现,大部分的时间我不需要它(和API是太麻烦了)。
正如Andrew所说,您仍然仅限于ImageIO.getReaderFileSuffixes()
所列的格式。您可能需要为特定格式添加插件。
你是否找到了你的问题的答案? – feisal
@feisal对不起,这是一个旧项目。不知道我如何(如果)我做到了。我会尝试通过我的代码存档并找到它,但不知道它需要多长时间 –