Hexdecimal byte for Jpeg file

Hi,

I am retrieving some data byte in hexadecimal format but the format looks incorrect.

One byte of hexadecimal data is usually in the format of FF, D8, 09 or 03
However when I retrieve my data, some are shown in single digit.
For example, 03 is shown as 3. I have attached a file as well.

The string of hexadecimal byte data becomes FF, D8, 9 or 3

I am retrieving this data to convert in to a Jpeg file and I think this single digit hexadecimal might be giving me the problem to write a proper Jpeg file.

Please help.

IMAGE.TXT (35.8 KB)

How about showing us some code?

If all you want to do is convert individual lines of text into a value for each line, why not just add a 0 to the front of the single digit ones?

I am getting the data from a serial camera.

I have attached the code which connects my mega2560 to a serial camera.

If you could help me, as I have been trying to save this image data but no success.

I have also attached the image file which I have tried to save all those hex jpeg data.

I have also attached a screenshot of the serial monitor which prints the jpeg data received from the camera.

Thanks.

IMAGE.JPG|0x0

Resend with 2 attachments

Serial monitor.pdf (271 KB)

Serial camera.txt (7.27 KB)

It look as if the code that formatted these values as hex did not pad the output to two characters with leading zeroes.

If that formatting is done in your code, you could fix it to pad the output. For example, if you're using one of the printf() functions, you can do that by using the %02X format specifier.

If that's not done in your code, you could still measure the length of the ascii string and insert a leading zero when required.

If you're trying to write the data to an image file then you should be writing the binary data (i.e. one binary byte), and not its hexadecimal representation (i.e. two ascii characters)

Hi Peter,

Thanks for your response.

So in order to write the data to an image file, it should be written in binary?

If i write these data from FF D8 ..... FF D9 to an image, it will work?

So in order to write the data to an image file, it should be written in binary?

Yes, using the write() methods, not the print() methods with the BIN second argument.

If i write these data from FF D8 ..... FF D9 to an image, it will work?

If you write the data to a file, it might work.

Thanks, do I need to pay attention to any formatting? I mean do i just write byte after byte of the binary data to the file?

do I need to pay attention to any formatting?

No. Binary data doesn't need formatting.

I mean do i just write byte after byte of the binary data to the file?

Yes.

Thanks I will give it a shot.

The camera code is supposed to give me the number of bytes in the last package data that it send but I am unable to decipher it.

I try in my code to include a receiveLast() which is supposed to check if a byte of FF followed by a D9 is received. Can this work?

You need to receive the image data stream from the camera and write the image to the file in the correct format. The two formats may be identical, but I don't know enough about your system to assume that.

If it doesn't work I suggest you use a binary file editor to compare the content of your saved file against a known good image file of a similar size, colour depth and format - that would give you an indication of whether there was anything substantially wrong with your file format, such as a missing header.

Hi,

For hexadecimal command that send to a camera, if the command is 0xAA, 0x0D… for example

However when I read from the serial monitor, it shows AA, D

does it matter if the 0D is only a D? how can I stack another 0 in front of the D?

Thanks

However when I read from the serial monitor, it shows AA, D

Let's get something straight. You are the only one that can read what is on your serial monitor. Your Arduino can not.

Your Arduino can print() to the serial port. The Serial Monitor can read from the serial port, and display what it read on the text field on the window.

So, you need to clarify what this statement means.

does it matter if the 0D is only a D?

Not to me. And not to the Arduino.

how can I stack another 0 in front of the D?

The same way that you would "stack" "0x" in front of the D.

Let’s try and clear something up…

  1. You are receiving NUMBERS from the camera.
  2. You are then printing those as HEXADECIMAL TEXT to the console.
  3. You are saving those numbers as HEXADECIMAL TEXT to the file.

2 is meaningless, and 3 is completely wrong.

Numbers do not have a leading 0 - only the text representations of them. Hexadecimal is a purely human representation of binary numbers. The file should contain the binary numbers, NOT the human readable textual representation of them in some arbitrary base.

So, what do you do?

First, forget anything anyone has told you about adding leading zeros - it’s completely pointless in this situation.

Second, don’t use SD.print() as it formats the numbers for a HUMAN to read, not a computer. Instead use SD.write() to write the RAW values to the file.

Now the JPEG problem…

Does the camera actually create a compressed JPEG stream of data, or does it send raw pixel data? If the latter you will have fun compressing that to a JPEG with an Arduino - it is very processor intensive and is complex code.

If it sends a pre-compressed JPEG stream, which is possible, then the raw data written to a file AS RAW DATA may work.

Thanks you have clear my understanding.

I have been trying to sycn with the camera however it is inconsistent, it works sometimes and don't on other time.

When I send the sync command, sometimes I get the ACK and sometimes no response at all.

I am using this " while( Serial3.available() > 0 )" to check after I send my sycn command for the ACK reply. If there any other better ways to do it?

That will exit immediately if called before the ACK is slow in coming.

You should first wait until there is some data to receive before receiving it:

while(Serial3.available() == 0)
  continue;

Hi,

The camera datasheet requires the sync command to send up to 60 times (maybe it is difficult to sync)

I have to keep the if statement so that it will continue sending until there are serial data available on the port.

In that case I would send the SYN command, then wait for a specific period for an ACK. If it doesn't arrive within a timeout period, then do it all again.

Some variation of:

int retries = 60;
boolean ackFound = false;

// Do this up to 60 times or until ack has been received.
while ((retries > 0) && (!ackFound)) {
  int timeout = 10;
  Serial3.write(SYN); // or whatever

  // Wait up to 1 millisecond for the ACK reply - tune this to optimize it.
  while ((Serial3.available() == 0) && ( timeout > 0)) {
    timeout--;
    delayMicroseconds(100);
  }

  // If the ACK arrived, remember the fact.
  if ((Serial3.available()) {
    if (Serial3.read() == ACK) {
      ackFound = true;
    }
  }

  retries--;
}

// If we didn't find the ACK, do something about it.
if (!ackFound) {
  Serial.println("SYN/ACK failed!  Something went horribly wrong!");
}

first, sorry for my english
i have some trouble
can i convert raw image data (FF D8 … FF D9) to RGB values ?
thanks for help