从2d条形码中提取jfif图像

时间:2011-07-27 22:22:09

标签: image-processing barcode binaryfiles

我从2d条形码pdf417读取数据。它包含格式为(jfif)的嵌入图像,图像不在解码数据的开头,它有一些数据字段,而图像在某处,数据字段不接缝以具有固定长度。如何从解码数据中提取图像。我使用ClearImage Library来解码条形码,我把它作为文本和Hex。 请帮忙。提前谢谢

1 个答案:

答案 0 :(得分:1)

由于StackOverflow的许多专家,我能够提取图像,我正在审查他们的帖子。下面的代码解释了如何从混合二进制文件中提取图像,代码不是那么漂亮,但它可以完成这项工作。它搜索(JFIF)图像标题并将其提取到图像文件中。

public static void ExtractImage(string fname)
{
try
{
FileStream fs = new FileStream(fname, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
//read the first binary
char[] soi="Empty".ToCharArray();
br.BaseStream.Position = 0;
long imgpos = 0;
ushort r = 0;
while ((r = br.ReadUInt16())> 0)
{
Console.WriteLine(r);
if (r == 0xd8ff)
{
Console.WriteLine("Detcted----->");
imgpos = br.BaseStream.Position;
break;
//UInt16 jfif = br.ReadUInt16(); // JFIF marker
//Console.WriteLine("jfif " + jfif);
//if (jfif == 0xe0ff || jfif == 57855)
//    Console.WriteLine(" also Detected--->");
}
}
//now copy to stream
FileStream str = new FileStream("bcimage.jpg", FileMode.OpenOrCreate,   FileAccess.Write);
BinaryWriter bw = new BinaryWriter(str);
br.BaseStream.Position = imgpos-2;
int l = (int)(fs.Length - imgpos - 2);
bw.Write(br.ReadBytes(l));
fs.Close();
br.Close();
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}