Im programming a little add-on for our business application, the goal is to take pictures with a Barcodereader.
Everything works, but the problem is the Barcodereader sends the picture in intervals, they are pretty random (depends on the size of the image and the baud rate). Without full analysis of the bytes I receive there is no way to tell if picture is already loaded.
At the moment my logic tries to find start/end of the JPEG by searching for FF D8
and FF D9
bytes respectively. The problem is bytes FF D9
can occur inside image.
I obviously could do some specific analysis of the bytes, but as the Barcodereader continuously sends data, performing time consuming operations (debug, CPU, IO, etc) while receiving bytes will end up in a bluescreen.
What I exactly want is
Reading the byte on which the size of the image is shown (I couldn't even research if the size will take the header itself / footer itself in consideration... do I have to calculate for that? ).
Checking if the I received all bytes.
I will put the code of me receiving and working with the Bytes (its on a datareceived event, serialPortish) and a correct full Image in Bytes and and a corrupt image maybe that will help.
DataReceivedEvent
private void ScannerPort_DataReceived(object sender, DataReceivedEventArgs e)
{
if (_WaitingForImage)
{
List<byte> imageBufferList = new List<byte>(e.RawBuffer);
{
if (imageBufferList[0] == 0x24 && imageBufferList[1] == 0x69)
{
for (int i = 0; i < 17; i++)
{
imageBufferList.RemoveAt(0);
}
}
byte[] imageBuffer = imageBufferList.ToArray();
_ImageReceiving = false;
_WaitingForImage = false;
this.OnImageReceived(imageBuffer);
}
//imageBufferList.AddRange(e.RawBuffer);
}
Full ByteArray
https://codepen.io/NicolaiWalsemann/pen/KKzxaXg
Corrupt byte Array
https://codepen.io/NicolaiWalsemann/pen/YzqONxd
Solution 1
I could easily do a Timer which waits for 500ms-2000ms after the DataReceived event is first called. This would make sure that I have everything and then I can parse it as much as I want. But obviously always having to wait unreasonably is not what I want.