0

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.

Hairgami_Master
  • 5,429
  • 10
  • 45
  • 66
  • off-topic comment: it would be better to try find out what prefix/sufix data means ... prolly it contains lengh of itself and the jpg file – Selvin Oct 31 '19 at 15:19
  • Thank you, @Selvin that's not a bad idea. I actually know what the extra data means- they are acknowledgement codes from the scanner manufacturer but are inconsistent and don't always come back the same way. – Hairgami_Master Oct 31 '19 at 15:32
  • From wiki the start of image should match the byte signature of [... FF D8 ...] I would open up a stream reader and look for the start of the image – fanuc_bob Oct 31 '19 at 16:47
  • The data is probably a 64 base string and you need Convert.FromBase64String(string). – jdweng Oct 31 '19 at 16:48

0 Answers0