在 Java 中将字节数组转换为图像 - 不知道类型 [英] Convert Byte Array to image in Java - without knowing the type

查看:28
本文介绍了在 Java 中将字节数组转换为图像 - 不知道类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

听起来很简单吧?使用

ImageIO.read(new ByteArrayInputStream(bytes));

这是皱纹.出于某种原因,它将 jpeg 检测为 bmp,这是我调用

Here's the wrinkle. For some reason it is detecting a jpeg as a bmp, and that is the first ImageReader returned when I call

ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(bytes));
Iterator<ImageReader> readers=ImageIO.getImageReaders(iis);

这会导致图像损坏.有没有办法通过 java 直接查看标头的字节,如果失败了,有没有人知道不同图像的字节标头的良好参考?

This causes the image to come out corrupted. Is there a way to tell through java short of looking directly at the bytes for the header, and failing that does anyone know of a good reference for the byte headers for the different images?

只是让你们知道我仍在努力.如果/当我有答案时,我会通知您.我感谢大家到目前为止的答复.

Just letting you guys know I am still working on this. I'll let you know if/when I have an answer. I thank all of you for your responses so far.

推荐答案

有一段时间没玩过 ImageIO,也没有测试过,但我似乎记得有这样的工作.(因为你说你知道你的文件是 jpg 而不是位图,我正在使用这些信息来帮助找到合适的加载器).

Haven't played with ImageIO in a awhile, and have not tested this, but I seem to recall something like this working. (since you say you know your file is a jpg and not a bitmap, I am using that information to help find the right loader).

String inFormat = "jpg";

Iterator inReaders = ImageIO.getImageReadersByFormatName(inFormat);

...

nextInReader.setInput( iis );

这篇关于在 Java 中将字节数组转换为图像 - 不知道类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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