Camera

This GR-LYCHEE library allows for the use of a camera.

It is necessary to specify #include <Camera.h> to use this library.

Camera()

Description
Constructor of the Camera class. When creating an object, you can specify the size of the acquired image as an option.
Syntax
Camera camera
Camera camera(uint16_t width, uint16_t height)
Parameters
width: Width (option, default 640)
height: Height (option, default 480)

begin

Description
Image acquisition is started with the camera.
Syntax
begin()
Parameters
None
Returns
None

getImageAdr

Description
Get the address of the image.
Syntax
uint8_t* getImageAdr()
Parameters
None
Returns
Address of the image
Note
Images are stored in YUV422 format. Y0, U, Y1, V are stored in this order. The format cannot be changed.

getWidth

Description
Get the set width.
Syntax
uint16_t getWidth()
Parameters
None
Returns
Width

getHeight

Description
Get the set height.
Syntax
uint16_t getHeight()
Parameters
None
Returns
Height

createJpeg

Description
Compress the image to JPEG format.
Syntax
size_t createJpeg()
size_t createJpeg(uint16_t width, uint16_t height, uint8_t* buf, uint8_t format = FORMAT_YUV422);
Parameters
If nothing is specified, convert the image obtained from the camera.
width: Width
height: Height
buf: Storage address
format: Original image format (FORMAT_YUV422, FORMAT_RGB888, FORMAT_GRAY)
Returns
Image size

getJpegAdr

Description
Get the storage address of JPEG data.
Syntax
uint8_t* getJpegAdr()
Parameters
None
Returns
Storage address

stop

Description
Stop image acquisition.
Syntax
stop()
Parameters
None
Returns
None

restart

Description
Resume acquisition of images.
Syntax
restart()
Parameters
None
Returns
None

Sample Program

Save images taken with the camera to SD.


#include <Arduino.h>
#include <Camera.h>
#include <SD.h>
#include <SPI.h>
#include <RTC.h>
Camera camera(640, 480);
RTC rtc;
    
void dateTime(uint16_t* date, uint16_t* time) {
    int year, mon, day, hour, min, sec, week;
    rtc.getDateTime(year, mon, day, hour, min, sec, week);
    *date = FAT_DATE(year, mon, day);
    *time = FAT_TIME(hour, min, sec);
}
void setup() {
    
    Serial.begin(9600);
    Serial.println("start");
    pinMode(PIN_SW0, INPUT);
    pinMode(PIN_SW1, INPUT);
    pinMode(PIN_LED_GREEN, OUTPUT);
    pinMode(PIN_LED_YELLOW, OUTPUT);
    pinMode(PIN_LED_ORANGE, OUTPUT);
    pinMode(PIN_LED_RED, OUTPUT);
    
    camera.begin();
    rtc.begin();
    
    rtc.setDateTime(2017, 6, 17, 14, 1, 0);
    SdFile::dateTimeCallback(&dateTime);
    
    if (!SD.begin()) {
    Serial.println("Card failed, or not present.");
    digitalWrite(PIN_LED_RED, HIGH);
    while (1)
        ;
    }
    else {
    Serial.println("Card founded.");
    }
    Serial.println("Click button0 to take a picture.");
    digitalWrite(PIN_LED_GREEN, HIGH);
    
}
    
void loop() {
    static int count = 0;
    if (digitalRead(PIN_SW0) == 0) {
    char filename[13];
    sprintf(filename, "image%d.jpg", count);
    File file = SD.open(filename, FILE_WRITE);
    if (file) {
        digitalWrite(PIN_LED_ORANGE, HIGH);
        size_t size = camera.createJpeg();
        uint8_t* adr = camera.getJpegAdr();
        for (size_t i = 0; i < size; i++) {
        file.write(*adr);
        adr++;
        }
        file.close();
        Serial.print("Saved a picture as ");
        Serial.println(filename);
        digitalWrite(PIN_LED_ORANGE, LOW);
        count++;
    }
    else {
        Serial.println("Failed to open file.");
        digitalWrite(PIN_LED_RED, HIGH);
        while (1)
        ;
    }
    }
    
}