Problems using ESP-EYE and base64

Luiz.Picolo
Posts: 7
Joined: Wed Jun 05, 2019 9:04 pm

Problems using ESP-EYE and base64

Postby Luiz.Picolo » Wed Jun 05, 2019 10:00 pm

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.
  1.   camera_fb_t * pic = NULL;
  2.   pic = esp_camera_fb_get();
  3.  
  4.   size_t pic_len;
  5.   pic_len = pic->len;
  6.  
  7.   char size[50];
  8.   char img[100]="test";
  9.  
  10.   sprintf(size,"%zu",pic_len);
  11.  
  12.   esp_camera_fb_return(pic);
  13.  
  14.   size_t output_length; // note *NOT* a pointer
  15.   char * encoded = base64_encode((const unsigned char *)img, strlen(size), &output_length);
  16.  
  17.   printf(encoded);
Can someone help me with this code?
Or find another way to make it work?

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: Problems using ESP-EYE and base64

Postby ESP_Sprite » Thu Jun 06, 2019 12:19 am

Please take a long, hard look at what you actually feed into the base64 encoder in the 'size' argument.

Luiz.Picolo
Posts: 7
Joined: Wed Jun 05, 2019 9:04 pm

Re: Problems using ESP-EYE and base64

Postby Luiz.Picolo » Thu Jun 06, 2019 8:04 pm

This is the problem, I can not understand what to put there, I need help from someone who has more understanding about it.

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: Problems using ESP-EYE and base64

Postby ESP_Sprite » Fri Jun 07, 2019 2:25 am

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?

Luiz.Picolo
Posts: 7
Joined: Wed Jun 05, 2019 9:04 pm

Re: Problems using ESP-EYE and base64

Postby Luiz.Picolo » Mon Jun 10, 2019 6:55 pm

-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);"
  1. #include "app_camera.h"
  2. #include "app_wifi.h"
  3. //#include "app_httpd.h"
  4. #include "app_httpd.h"
  5. #include "esp_timer.h"
  6. #include "img_converters.h"
  7. #include "fb_gfx.h"
  8. #include "sdkconfig.h"
  9. #include "esp_log.h"
  10.  
  11. #include "wpa/includes.h"
  12. #include "os.h"
  13. #include "wpa2/utils/base64.h"
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18.  
  19. static const char *TAG = "Teste";
  20.  
  21. static void periodic_timer_callback(void *arg);
  22.  
  23. void app_main()
  24. {
  25. //app_wifi_main();
  26. app_camera_main();
  27. //app_httpd_main();
  28. usleep(10000000);
  29. const esp_timer_create_args_t periodic_timer_args = {
  30. .callback = &periodic_timer_callback,
  31. .name = "periodic"};
  32.  
  33. esp_timer_handle_t periodic_timer;
  34. ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer));
  35. /* The timer has been created but is not running yet */
  36.  
  37. /* Start the timers */
  38. ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, 10000000)); //Time of the pic
  39. ESP_LOGI(TAG, "Started timer");
  40. }
  41.  
  42. static void periodic_timer_callback(void *arg) //Pic
  43. {
  44. camera_fb_t * pic = NULL;
  45. pic = esp_camera_fb_get();
  46. size_t pic_len;
  47. pic_len = pic->len;
  48. esp_camera_fb_return(pic);
  49. size_t output_length; // note *NOT* a pointer
  50. unsigned char * encoded = base64_encode((const unsigned char *)pic->buf, pic_len, &output_length);
  51. printf (*encoded);
  52.  
  53. ESP_LOGI(TAG, "10 seconds");
  54. }

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: Problems using ESP-EYE and base64

Postby ESP_Sprite » Tue Jun 11, 2019 12:50 am

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?

Luiz.Picolo
Posts: 7
Joined: Wed Jun 05, 2019 9:04 pm

Re: Problems using ESP-EYE and base64

Postby Luiz.Picolo » Tue Jun 11, 2019 9:11 pm

It's working, I've modified the memory release and data output. now it's working 100%
Thank you for your help!
  1. #include "app_camera.h"
  2. #include "app_wifi.h"
  3. #include "app_httpd.h"
  4. #include "esp_timer.h"
  5. #include "img_converters.h"
  6. #include "fb_gfx.h"
  7. #include "sdkconfig.h"
  8. #include "esp_log.h"
  9.  
  10. #include "wpa/includes.h"
  11. #include "os.h"
  12. #include "wpa2/utils/base64.h"
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17.  
  18. static const char *TAG = "Teste";
  19.  
  20. static void periodic_timer_callback(void *arg);
  21.  
  22. void app_main()
  23. {
  24.     //app_wifi_main();
  25.     app_camera_main();
  26.     //app_httpd_main();
  27.     usleep(10000000);
  28.     const esp_timer_create_args_t periodic_timer_args = {
  29.         .callback = &periodic_timer_callback,
  30.         .name = "periodic"};
  31.  
  32.     esp_timer_handle_t periodic_timer;
  33.     ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer));
  34.     /* The timer has been created but is not running yet */
  35.  
  36.     /* Start the timers */
  37.     ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, 10000000)); //Time of the pic
  38.     ESP_LOGI(TAG, "Started timer");
  39. }
  40.  
  41. static void periodic_timer_callback(void *arg) //Pic
  42. {
  43.   camera_fb_t * pic = NULL;
  44.   pic = esp_camera_fb_get();
  45.  
  46.   size_t pic_len;
  47.   pic_len = pic->len;
  48.  
  49.  
  50.   size_t output_length; // note *NOT* a pointer
  51.   unsigned char * encoded = base64_encode((const unsigned char *)pic->buf, pic_len, &output_length);
  52.  
  53.   printf("%.*s", output_length, encoded);
  54.   free(encoded);
  55.   esp_camera_fb_return(pic);
  56.  
  57.   ESP_LOGI(TAG, "10 seconds");
  58. }

Who is online

Users browsing this forum: Bing [Bot] and 109 guests