In C#, I'm using a handheld Honeywell scanner as an imaging device via Serialport.read(). I issue commands to the scanner and although it returns a byte array of the image, the data also contains prefix and suffix data (of unknown length) that lead/trail the image itself (data I need to remove so that all that is left is just the image alone).
I can "look" for a JPEG image within the stream by converting it to hex and looking for "FF D8 FF" as the start and "FF D9" as the end (as explained here: How to identify contents of a byte[] is a JPEG?)
So I can select the image within the hex, and then I have what I believe to be a Hex string of a valid JPEG image (begins with FF D8 FF, etc.):
Excerpt of Hex String:
FFD8FFE000104A46494600010100000100010000FFDB004 --- 01ED309C5213C53335FFFD9
But using this method to convert it (c# hex string to byte image and filtering) into binary doesn't yield a valid image:
private byte[] HexString2Bytes(string hexString)
{
int bytesCount = (hexString.Length) / 2;
byte[] bytes = new byte[bytesCount];
for (int x = 0; x < bytesCount; ++x)
{
bytes[x] = Convert.ToByte(hexString.Substring(x * 2, 2), 16);
}
return bytes;
}
Am I missing something obvious here? Is there an easier way to "find" a jpeg image within a byte array that contains leading/trailing data (without having to convert it to hex)?
Thank you for any pointers.