jpg sd file problem although header flag ok

I have an Arducam Mini 2mp and an SD Card (and mega 2560).

The camera will capture an image and will save it to the SD Card. The jpg file starts with an FF,D8 and ends with an FF,D9 and the data inside appears to be roughly normal for a jpg, but I can't read the file using paint or photoshop.

I can send the capture only via the serial port, capture it from a serial capture program and the resulting file is a jpg that opens just fine in paint. It's perfect. I've compared the two files (the one via serial and the one via the SD Card) and there are only slight differences. File sizes are similar and as I said, they start with an FF, D8 and end with an FF, D9.

I've tried different cards too.

I'm using IDE 1.69, and the example below is from the ArduCam_OV2640_DIgital_Camera.ino - unchanged except the cs pins - it's the same thing - something is wrong inside the completed jpg file.

Anyone else had this issue?

I've used in addition to my own code - just straight examples. Here is the relevant section - basically it's just right out of the examples..

Serial.println("Capture Done!");

byte buf[256];
uint8_t temp,temp_last;
int total_time = 0;
File myFile;
char str[8];
static int k = 0;
//Construct a file name
k = k + 1;
itoa(k, str, 10);
strcat(str,"_JJ.jpg");
//Open the new file
myFile = SD.open(str,O_WRITE | O_CREAT | O_TRUNC);
if (! myFile)
{
Serial.println("open file failed");
return;
}
total_time = millis();
int i = 0;
temp = myCAM1.read_fifo();
//Write first image data to buffer
buf = temp;

  • i++;*

  • //Read JPEG data from FIFO*

  • while( (temp != 0xD9) | (temp_last != 0xFF) )*

  • {*

  • temp_last = temp;*

  • temp = myCAM1.read_fifo();*

  • //Write image data to buffer if not full*

  • if(i < 256)*

  • {*
    _ buf = temp;_
    * i++;*
    * }*
    * else*
    * {*
    * //Write 256 bytes image data to file*
    * myFile.write(buf,256);*
    * i = 0;*
    _ buf = temp;
    * i++;
    }
    }
    //Write the remain bytes in the buffer*
    * if(i > 0)
    {
    myFile.write(buf,i);
    }
    //Close the file*
    * myFile.close();_
    total_time = millis() - total_time;
    _ Serial.print("Total time used:");_
    Serial.print(total_time, DEC);
    _ Serial.println(" millisecond");
    //Clear the capture done flag*_

    * myCAM1.clear_fifo_flag();*

I found the issue for the problem I stated above.

Changing this line in the SD.CPP library resolved it -

Original line -in the begin()

return card.init(SPI_HALF_SPEED, csPin) &&
volume.init(card) &&
root.openRoot(volume);

Revised line -

return card.init(SPI_FULL_SPEED, csPin) &&
volume.init(card) &&
root.openRoot(volume);

I noticed when I tried quarter speed it seemed worse so I tried full speed and the above issue was resolved.

Also in my first post, it isn't displaying some of the code properly even though it was ok when i posted it - something in the web page translation or the way I posted it.

But the
buf = temp;
obviously won't work it's supposed to be

buf Bracket i Bracket = temp;

For some reason the brackets and the i disappeared in my post.

That's in a couple of places - 3 I think in the code. Weird.

joeSouthern:
Also in my first post, it isn't displaying some of the code properly even though it was ok when i posted it - something in the web page translation or the way I posted it.

But the
buf = temp;
obviously won't work it's supposed to be

buf Bracket i Bracket = temp;

For some reason the brackets and the i disappeared in my post.

That's in a couple of places - 3 I think in the code. Weird.

This websites forum software uses square brackets for markups.

To be able to write code without it being interpreted by the website, surround your code with code markups:

[code]at the beginging and [/code]
 after your code

.

Your code will show up in a scroll-able box like this:

//comment
char a[24]; // a twentyfour byte char array.

Chuck.