Page 1 of 1
Problems using ESP-EYE and base64
Posted: Wed Jun 05, 2019 10:00 pm
by Luiz.Picolo
So, I'm doing a project and I need to turn an image into base64, but I do not know how to continue from here. I managed to make it work with text, but I could not with image.
I'm using the esp-eye and the crypto / base64.h file.
camera_fb_t * pic = NULL;
pic = esp_camera_fb_get();
size_t pic_len;
pic_len = pic->len;
char size[50];
char img[100]="test";
sprintf(size,"%zu",pic_len);
esp_camera_fb_return(pic);
size_t output_length; // note *NOT* a pointer
char * encoded = base64_encode((const unsigned char *)img, strlen(size), &output_length);
printf(encoded);
Can someone help me with this code?
Or find another way to make it work?
Re: Problems using ESP-EYE and base64
Posted: Thu Jun 06, 2019 12:19 am
by ESP_Sprite
Please take a long, hard look at what you actually feed into the base64 encoder in the 'size' argument.
Re: Problems using ESP-EYE and base64
Posted: Thu Jun 06, 2019 8:04 pm
by Luiz.Picolo
This is the problem, I can not understand what to put there, I need help from someone who has more understanding about it.
Re: Problems using ESP-EYE and base64
Posted: Fri Jun 07, 2019 2:25 am
by ESP_Sprite
Sure you can, just think methodologically.
- What do you think the base64_encode function wants to see in the 'len' argument? The length of what? In what units?
- What does pic_len contain exactly?
- If you use sprintf to convert the value of pic_len to a string and put it in the 'size' variable, what does 'size' contain?
- What does strlen() do?
- What does strlen(size) give as a result?
- From that, why does a base64-encode of "test" work, but one of the picture does not?
Re: Problems using ESP-EYE and base64
Posted: Mon Jun 10, 2019 6:55 pm
by Luiz.Picolo
-The base64_encode wants to see the number of bytes the data has
-I guess that is the number of bytes of the photo taken by the camera
-The number of bytes converted to char
-Returns the length of the string
-The length of the char variable
-I still do not know
To be easier to understand my problem, here is the rest of my code!
Now I'm having some problems on "printf (*encoded);"
#include "app_camera.h"
#include "app_wifi.h"
//#include "app_httpd.h"
#include "app_httpd.h"
#include "esp_timer.h"
#include "img_converters.h"
#include "fb_gfx.h"
#include "sdkconfig.h"
#include "esp_log.h"
#include "wpa/includes.h"
#include "os.h"
#include "wpa2/utils/base64.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
static const char *TAG = "Teste";
static void periodic_timer_callback(void *arg);
void app_main()
{
//app_wifi_main();
app_camera_main();
//app_httpd_main();
usleep(10000000);
const esp_timer_create_args_t periodic_timer_args = {
.callback = &periodic_timer_callback,
.name = "periodic"};
esp_timer_handle_t periodic_timer;
ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer));
/* The timer has been created but is not running yet */
/* Start the timers */
ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, 10000000)); //Time of the pic
ESP_LOGI(TAG, "Started timer");
}
static void periodic_timer_callback(void *arg) //Pic
{
camera_fb_t * pic = NULL;
pic = esp_camera_fb_get();
size_t pic_len;
pic_len = pic->len;
esp_camera_fb_return(pic);
size_t output_length; // note *NOT* a pointer
unsigned char * encoded = base64_encode((const unsigned char *)pic->buf, pic_len, &output_length);
printf (*encoded);
ESP_LOGI(TAG, "10 seconds");
}
Re: Problems using ESP-EYE and base64
Posted: Tue Jun 11, 2019 12:50 am
by ESP_Sprite
Seems you have at least found your logic error in your first post. I think the error you have now is one of ownership. esp_camera_fb_return is a function you're supposed to do if you're entirely done with the memory that esp_camera_fb_get gives you; you're effectively done parsing the camera image and tell the camera API that it can re-use the memory. Is that true in your code?
Re: Problems using ESP-EYE and base64
Posted: Tue Jun 11, 2019 9:11 pm
by Luiz.Picolo
It's working, I've modified the memory release and data output. now it's working 100%
Thank you for your help!
#include "app_camera.h"
#include "app_wifi.h"
#include "app_httpd.h"
#include "esp_timer.h"
#include "img_converters.h"
#include "fb_gfx.h"
#include "sdkconfig.h"
#include "esp_log.h"
#include "wpa/includes.h"
#include "os.h"
#include "wpa2/utils/base64.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
static const char *TAG = "Teste";
static void periodic_timer_callback(void *arg);
void app_main()
{
//app_wifi_main();
app_camera_main();
//app_httpd_main();
usleep(10000000);
const esp_timer_create_args_t periodic_timer_args = {
.callback = &periodic_timer_callback,
.name = "periodic"};
esp_timer_handle_t periodic_timer;
ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer));
/* The timer has been created but is not running yet */
/* Start the timers */
ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, 10000000)); //Time of the pic
ESP_LOGI(TAG, "Started timer");
}
static void periodic_timer_callback(void *arg) //Pic
{
camera_fb_t * pic = NULL;
pic = esp_camera_fb_get();
size_t pic_len;
pic_len = pic->len;
size_t output_length; // note *NOT* a pointer
unsigned char * encoded = base64_encode((const unsigned char *)pic->buf, pic_len, &output_length);
printf("%.*s", output_length, encoded);
free(encoded);
esp_camera_fb_return(pic);
ESP_LOGI(TAG, "10 seconds");
}