From 79ba4c7d55a64e55514724514595af4c0e0cf924 Mon Sep 17 00:00:00 2001 From: Nick Van Doorn Date: Sun, 5 Nov 2017 19:08:04 -0800 Subject: Implement method to write snapshot to file Mostly lifted from here https://github.com/adafruit/Adafruit-VC0706-Serial-Camera-Library/tree/master/examples/Snapshot --- camera.c | 35 +++++++++++++++++++++++++++++++++++ camera.h | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/camera.c b/camera.c index c9474a6..2cb95be 100644 --- a/camera.c +++ b/camera.c @@ -252,3 +252,38 @@ bool setPTZ(Camera *cam, uint16_t wz, uint16_t hz, uint16_t pan, uint16_t tilt) }; return !runCommand(cam, VC0706_SET_ZOOM, args, 5); } + +bool snapshotToFile (Camera *cam, char *path, byte imgSize) { + setImageSize(cam, imgSize); + LE_INFO("Taking photo..."); + bool photoTaken = takePicture(cam); + if (photoTaken) { + LE_INFO("Photo taken"); + char writePath[100]; + // e.g /mnt/sd/.jpg + sprintf(writePath, "%s/%d.jpg", path, (int)time(0)); + LE_INFO("Opening file pointer for path %s", writePath); + FILE *filePtr = fopen(writePath, "w"); + if (filePtr != NULL) { + LE_INFO("Got valid file pointer"); + int jpgLen = frameLength(cam); + while (jpgLen > 0) { + byte *buff; + uint8_t bytesToRead = 32 < jpgLen ? jpgLen : 32; + buff = readPicture(cam, bytesToRead); + fwrite(buff, sizeof(*buff), bytesToRead, filePtr); + jpgLen -= bytesToRead; + } + fclose(filePtr); + return true; + } + else { + LE_ERROR("Invalid file pointer for %s", writePath); + return false; + } + } + else { + LE_ERROR("Failed to take photo"); + return false; + } +} diff --git a/camera.h b/camera.h index 6fbb3cc..b24d508 100644 --- a/camera.h +++ b/camera.h @@ -83,4 +83,4 @@ byte getCompression (Camera *cam); bool setCompression(Camera *cam, byte c); bool getPTZ(Camera *cam, uint16_t *w, uint16_t *h, uint16_t *wz, uint16_t *hz, uint16_t *pan, uint16_t *tilt); bool setPTZ(Camera *cam, uint16_t wz, uint16_t hz, uint16_t pan, uint16_t tilt); -size_t camToFile (Camera *cam, char *path); +bool snapshotToFile (Camera *cam, char *path, byte imgSize); -- cgit v1.2.3