开发者

Extracting jfif image from 2d barcode

开发者 https://www.devze.com 2023-03-23 04:38 出处:网络
I have data read from 2d bar code pdf417. It contains an embedded image in the format of (jfif), The image is not at the beginning of the decoded data it has some data 开发者_如何学Cfields and the ima

I have data read from 2d bar code pdf417. It contains an embedded image in the format of (jfif), The image is not at the beginning of the decoded data it has some data 开发者_如何学Cfields and the image is somewhere after, the data fields does not seam to have fixed lengths. How can I extract the Image from the decoded data. I used ClearImage Library to decode the barcode and I have it as text and Hex. Please help. Thank you in advance


I was able to extract the image thanks to many experts in StackOverflow, I have being reviewing their posts. The following code explains how to extract the image from a mixed binary file, the code is not so beautiful but it can do the job. It searches for (JFIF) image header and extracts it into an image file.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号