2011-07-27 29 views
0

我从2d条码pdf417中读取数据。它包含一个格式为(jfif)的嵌入式图像,图像不在解码数据的开头,它有一些数据字段,图像在某处,数据字段没有缝到固定长度。我如何从解码数据中提取图像。我使用ClearImage Library来解码条形码,并将其作为文本和十六进制。 请帮忙。预先感谢您从2d条码中提取jfif图像

回答

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); 
} 
} 
+0

该代码假定图像对象是二进制文件中的最后一个对象,如果不是这种情况,则必须修改代码以搜索描述图像大小的图像标记的其他部分。 – MTom