在 C# 中解码 JFIF 图像文件
我有一个 JFIF 格式的 JPEG 图像。我想解码它并获取尺寸。
这是我的代码:
while (binaryReader.ReadByte() == 0xff)
{
byte marker = binaryReader.ReadByte();
ushort chunkLength = binaryReader.ReadLittleEndianInt16();
if (marker == 0xc0)
{
binaryReader.ReadByte();
int height = binaryReader.ReadLittleEndianInt16();
int width = binaryReader.ReadLittleEndianInt16();
return new Size(width, height);
}
binaryReader.ReadBytes(chunkLength - 2);
}
好的。这段代码很常见,你可以在互联网上找到它。它适用于大多数 JPEG 图像。
现在,由相机“Canon EOS 300D DIGITAL”拍摄的这张特定图像不支持这段代码。尺寸标记是 0xFFC2 而不是 0xFFC0。
我的问题是哪一个是正确的?如果代码正确,那么佳能相机怎么能拍出不标准的图像呢?如果佳能相机是正确的,那么我们如何修复代码以正确找到该图像的尺寸?
谢谢。
I have a JPEG image which is JFIF formatted. I want to decode it and get the dimension.
Here is my code:
while (binaryReader.ReadByte() == 0xff)
{
byte marker = binaryReader.ReadByte();
ushort chunkLength = binaryReader.ReadLittleEndianInt16();
if (marker == 0xc0)
{
binaryReader.ReadByte();
int height = binaryReader.ReadLittleEndianInt16();
int width = binaryReader.ReadLittleEndianInt16();
return new Size(width, height);
}
binaryReader.ReadBytes(chunkLength - 2);
}
Ok. This piece of code is common and you can find it all over the internet. It works fine for most of the JPEG images.
Now, this specific image which was taken by the camera - "Canon EOS 300D DIGITAL", does not support this piece of code. The marker for the dimension is 0xFFC2 instead of 0xFFC0.
My question is which one is correct? If the code is correct, then how can a Canon camera produce a non-standard image? If the Canon camera is correct, then how can we fix the code to correct find the dimension of this image?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FFC2 似乎是渐进式图像的标记。
请参阅示例 http://en.wikipedia.org/wiki/JPEG 其中解释了“渐进式”格式是(参见“JPEG 压缩”部分)。
是的,我认为您可以更改 if 语句以检查 0xc0 (SOF0 标记)和 0xc2 (SOF2 标记),因为它们似乎具有相似的结构(请参阅“语法和结构”部分)。另请参见此处:http://fjcore.googlecode.com/svn/trunk /FJCore/Decoder/JpegDecoder.cs
我不是 JPEG 格式方面的专家,因此如果您正在开发关键任务代码,您可能需要查看专门的论坛。
FFC2 seems to be the marker for progressive images.
please see for example http://en.wikipedia.org/wiki/JPEG which explains what "progressive" format is (see "JPEG compression" section).
yes, i think you can change your if statement to check for both 0xc0 (SOF0 marker) and 0xc2 (SOF2 marker) because they seem to have similar structure (see "syntax and structure" section). see also here: http://fjcore.googlecode.com/svn/trunk/FJCore/Decoder/JpegDecoder.cs
i am not expert in JPEG formats, so you might want to check with specialised forums if you are developing a mission critical code.