# Convert Binary Image To Jpeg

## Recommended Posts

I have a camera that output serial binary image data through a COM port. Can DF convert this data and display the image in JPEG format?

##### Share on other sites

Depends.  Is the binary data RGB?  Or is it compressed into JPEG?  I suppose either would work, but the jpeg form would be more involved, requiring you to write the stream to disk, then load it into a symbol component.  With binary RGB, you can simply process the data into an array and pass that array into an Image component.

##### Share on other sites

The data is the binary RGB. The image resolution is 80x60 which is 4800 pixels. Each pixel is a 16-bit 565 color. The data receive will be similar like

pixel@(x,y)   16bit colour
0,0                      88 21
1,0                      90 42
2,0                      90 40
... ...

If I check the data is RGB, the 16bit color value will be pass to the X,Y scaling or expression?

##### Share on other sites

You'll have to convert it to RGB.  And for speed you'll need to do it in one step.  Assuming the data is actually in the format:  rrrr rggg gggb bbbb, and the data is in a variable named "c":

global cRGB = (((c & f800) > 11) * 65536) + (((c & 0x07e0) > 5) * 256) + (c & 0x001F)

That will work even when c is an array.

Note that you are taking 5 and 6 bit colors and making them 8, so you may want to multiply the segments so they are left shifted in the eight bits (i.e. the first one * 8, the second * 4, and the third * 8)

##### Share on other sites

Question:   Can you help me with the code below:

Global Length1

Global Length2

Global Picture_Size

Device.COMPORT.Write(chra({0xAA, 0x04, 0x02, 0x00, 0x00, 0x00}))

Try

Length1 = AscA(Mid(Byte_In, 9, 1))

Length2 = AscA(Mid(Byte_In, 10, 1))

Picture_Size = To.UrWord({Length2, Length1})

Catch()

? "error"

Endcatch

Delay(1)

Device.COMPORT.Write(chra({0xAA, 0x0E, 0xFF, 0x00, 0x00, 0x00}))

Length1 and 2 return the correct decimal value of the Hex bytes input. In this case, Length1 = 192 and Length2 = 18. However, the code to calculate the Picture_Size seem to be incorrect.

If I try

Picture_Size = To,UrWord({18, 192})

Picture_Size would be equal to 4800

Question:   Is there away I can count the number of incoming bytes. I try to read 4812 bytes but many times I don't receive enough.

This will explain how the camera work.

1. Send the get picture command to the camera.

2. The camera acknowledge with a 12 bytes: \xAA\x0E\x04E\x00\x00\xAA\x0A\x02\xC0\x12 then continue with the 4800 byte image data

\xC0\x12 is the size of the picture which is 4800 in this case.

3. After the camera send out 4812 bytes, it require the host to reply with an acknowledge. Without receiving a reply from the host within 15 seconds, the camera will be in sleep mode.

I tried:

private Count = 0

While

Try

Catch

...

Endcatch

Count +=1

If (Count >= 4812)

break

endif

Endwhile

The sequence will never end because Count can't reach up to 4812

Question:  How to create a condition that if there is no data incoming for longer than 2 second, the while loop will be terminated?

I include a portion of the image data for reference.

Rx: \xAA\x0E\x04E\x00\x00\xAA\x0A\x02\xC0\x12\x0060D[envwwvqtrjmigfghhhknpoopsrssvtvvvvtvvsspomkcddb^]XVUYTPOKGFD@A?941/-)(%\x1F\x1D\x1C\x19\x157)/@Rcptuuppolqnmmljlmorpqrsvwxxyxyxxywxwwvrqpneddca`^]\\ZWTPLIIFDDA<951/-,("\x1F\x1C\x19\x161012:Kbtlppnnlrnooomopqrtvvwyxyz}|}}}|yzyyytssqhiigdc_^]\\YWSONKIHFB><841/-*\$!\x1E\x1A\x1833/)+4CP^imoqqqopqqruwwvxy{||z}}\x7F~\x80\x7F\x7F\x7F|}}{{wvusjlljgfa_^_\\YWSQOKIHD@>;730-)%# \x1C\x1A563*(')-@O_fmrruttuvz{||{|\x7F\x80\x80\x80\x82\x83\x82\x81\x82\x81\x81\x81\x7F\x80\x81~~{zxvnmmljifedb_]ZVURNLKEBA?:63/+'% \x1C\x1B222.(#\x1F"\x1C,?L^jowy|}{}~~~\x80\x80\x81\x82\x84\x82\x84\x84\x83\x82\x85\x83\x85\x85\x83\x84\x82\x81\x81}|{xpoonlljhhcba]YVTOMLIFD@<853.*&!\x1D\x1CJ7,&%#\x1B\x12\x0D\x15\x1F*@P[jt|\x81\x82\x82\x85\x87\x85\x85\x85\x85\x86\x86\x85\x84\x84\x86\x87\x89\x89\x87\x86\x85\x85\x84\x83\x83\x7F~~|tssqonkjidec_[[XROOLHFC>9952-)#\x1F\x1FmWE2!\x1A\x1B\x18\x0D\x08\x07\x0A\x1F2>P^jx\x80\x85\x88\x87\x87\x87\x86\x87\x87\x89\x88\x89\x88\x88\x88\x8B\x8A\x88\x87\x85\x86\x88\x85\x85\x82\x81\x80~vttsqpmlkihec_][uTSPLID@=:61.+&! {{kZA!\x12\x0C\x09\x09\x08\x05\x00\x02\x1B2DNXclz\x8C\x8A\x89\x88\x88\x87\x89\x89\x8B\x8C\x87\x88\x88\x89\x89\x88\x88\x87\x87\x87\x86\x83\x83\x7Fwwutsqpnmmjjfc`\\YXTQNLHEC?=72.*&"\x1D~\x86\x82xcD+\x17\x0B\x05\x04\x07\x05\x02\x04\x09\x1E-=Y^m\x93\x90\x8C\x8B\x8A\x8A\x8A\x8A\x8B\x8C\x88\x8A\x8A\x8A\x8A\x8A\x8A\x88\x89\x87\x87\x83\x86\x81xwwvusronnlkifc_\\ZYUQMHDA=>950,)\$ \x7F\x88\x8A\x87~iS>4\x1F\x0A\x03\x03\x03\x04\x02\x02\x0A\x1B/J^\x94\x96\x8F\x8E\x8D\x8C\x8C\x8C\x8B\x8C\x8C\x8D\x8C\x8B\x8B\x8B\x8A\x89\x8B\x89\x88\x85\x89\x84{yyyxutrqponjhfc`]\\WTOKGEB@;83.)&"\x86\x8C\x8C\x89\x and so on.............

Thanks,

##### Share on other sites

I think you have two questions here:

1) getting the length.  You can't put variables inside of {}.  You have to do:

private j = length2

j[1] = length1

You are, I believe, the third person to make this mistake and post on the forum about it in the last month, so I'm thinking maybe we should change the way {} behaves...

2) put your try / catch() outside the while loop.  When the read times out, it jumps to the catch.  Alternatively, leave the catch where it is, and put a break statement inside the catch so it breaks out of the loop on timeout.