Tips: Save and show a jpg in a storage such as SD

This is a simple example to save and show a jpg in a storage such as SD.

After taking a photo with camera, that is saved into SD. And then the image is shown on display app.

Saved jpg image with camera:

Shown jpg image in SD:

 

#include <Arduino.h>
#include <Camera.h>
#include <RTC.h>
#include <DisplayApp.h>
#include "SdUsbConnect.h"

#define IMAGE_HW 640
#define IMAGE_VW 480

Camera camera(IMAGE_HW, IMAGE_VW, 1); // CMOS Camera
DisplayApp display_app;
SdUsbConnect storage("storage");
RTC rtc; // for time stamp

void setup() {
Serial.begin(9600);
pinMode(PIN_LED_RED, OUTPUT);
pinMode(PIN_LED_GREEN, OUTPUT);
pinMode(PIN_LED_BLUE, OUTPUT);

Serial.println("Test starts");

// RTC
rtc.begin();
rtc.setDateTime(2017, 10, 14, 15, 40, 0);

// Camera
camera.begin();

// SD & USB
Serial.print("Finding strage..");
storage.wait_connect();
Serial.println("done");

}

void loop() {
FILE * wp = fopen("/storage/peach_camera.jpg", "w");
if (wp != NULL) {
digitalWrite(PIN_LED_BLUE, HIGH);
size_t size = camera.createJpeg();
fwrite(camera.getJpegAdr(), sizeof(char), (int)size, wp);
fclose(wp);
digitalWrite(PIN_LED_BLUE, LOW);
} else {
Serial.print("Not found jpg");
digitalWrite(PIN_LED_RED, HIGH);
delay(1);
}

FILE * fp = fopen("/storage/peach_camera.jpg", "r");
if (fp != NULL) {
digitalWrite(PIN_LED_GREEN, HIGH);
fseek(fp, 0, SEEK_END);
uint32_t jpegSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
uint8_t * buffer = (uint8_t*) malloc(jpegSize);
for (uint32_t i = 0; i < jpegSize; i++) {
buffer[i] = (uint8_t) fgetc(fp);
}
display_app.SendJpeg(buffer, jpegSize);
fclose(fp);
digitalWrite(PIN_LED_GREEN, LOW);
} else {
Serial.print("Not found jpg");
digitalWrite(PIN_LED_RED, HIGH);
delay(1);
}
}