Downloading Image to a specific folder using gp_filesystem_get_file()

614 Views Asked by At

Trying my hands on libgphoto2 library examples and while going through simple-capture.c file. Can i download foo.jpg captured image to a specified folder on my computer?

As far as i understood, in capture_to_file() camera_file_path.folder is the folder in which the file can be found on the camera. So open() should specify the host(computer) location. But nothing worked, i get following error:

You need to specify a folder starting with /store_xxxxxxxxx/

Am i missing something here? Any help would be appreciated, thanks!

1

There are 1 best solutions below

0
On

I got this working. Written small application for multiple cameras. Including main() for about question.

int main(int argc, char **argv) 
{
    CameraList      *list;
    Camera      **cams;
    int         retval, count, i;
    GPContext       *context;
    FILE        *f;
    char        *data;
    unsigned long   size;
    const char      *name, *value;


    /* 
     * Create context
     */
    context = sample_create_context();


    /*
     * Setup Images DB directory.
     */ 
    char* home = getenv("HOME");
    if (home == NULL) 
    {
        printf("Error: Unable to fetch home env! \n"); 
        exit(1);
    }
    char* path = "/Desktop/mw/";
    size_t len = strlen(home) + strlen(path) + 1;
    char* imgdb = malloc(len);
    if (imgdb == NULL) 
    {
        printf("Error: Unable to malloc(). \n"); 
        exit(1);
    }
    strcpy(imgdb, home);
    strcat(imgdb, path);
    directory_exists_or_create(imgdb);


    /*
     * Logs
     */ 
    gp_log_add_func(GP_LOG_ERROR, errordumper, NULL);


    /* 
     * Detect all the cameras that can be autodetected
     */ 
    retval = gp_list_new(&list);
    if (retval < GP_OK) 
    {
        printf("Unable to create camera list.\n");
        return 1;
    }
    count = sample_autodetect(list, context);
    if (count < GP_OK)
    {
        printf("No cameras detected.\n");
        return 1;
    }

    /* 
     * Now open all the cameras we autodetected for usage.
     */
    printf("Number of cameras: %d\n", count);
    cams = calloc(sizeof(Camera*), count);
    for (i = 0; i < count; i++)
    {
        gp_list_get_name(list, i, &name);
        gp_list_get_value(list, i, &value);
        retval = sample_open_camera(&cams[i], name, value, context);
        if (retval < GP_OK)
        {
            fprintf(stderr, "Camera %s on port %s failed to open\n", name, value);
        }
    }


    if (argc > 0)
    {
        while ((++argv)[0])
        {
            if (argv[0][0] == '-')
            {
                switch (argv[0][1])
                {
                    case 'h':
                    case 'H':
                        {
                            /* Now call a simple function in each of those cameras. */
                            for (i = 0; i < count; i++) 
                            {
                                CameraText      text;
                                char            *owner;
                                retval = gp_camera_get_summary (cams[i], &text, context);
                                if (retval < GP_OK) 
                                {
                                    fprintf (stderr, "Failed to get summary.\n");
                                    continue;
                                }

                                gp_list_get_name  (list, i, &name);
                                gp_list_get_value (list, i, &value);
                                printf("%-30s %-16s\n", name, value);
                                printf("Summary:\n%s\n", text.text);

                                /* Query a simple string configuration variable. */
                                retval = get_config_value_string (cams[i], "owner", &owner, context);
                                if (retval >= GP_OK) 
                                {
                                    printf("Owner: %s\n", owner);
                                    free (owner);
                                } 
                                else 
                                {
                                    printf("Owner: No owner found.\n");
                                }
                            }
                        }
                        /* Graceful exit from the program */
                        goto exit_;;

                    default:
                        printf("Unknown option -%c\n\n", argv[0][1]);
                        break;
                }
            }
        }
    }


    /* When I set GP_LOG_DEBUG instead of GP_LOG_ERROR above, I noticed that the
     * init function seems to traverse the entire filesystem on the camera.  This
     * is partly why it takes so long.
     * (Marcus: the ptp2 driver does this by default currently.)
     */
    printf("Cameras init.  Takes about 10 seconds each.\n");
    for (i = 0; i < count; i++)
    {
        retval = gp_camera_init(cams[i], context);
    if (retval != GP_OK) 
        {
            printf("  Camera [%d] init failed with retval %d\n", i, retval);
        exit (1);
    }
    }


    printf(" ----------------\n");
    printf(" Sampler is ready \n");
    printf(" ----------------\n");
    printf("Usage : \n");
    printf("  ESC - Exit the program\n");
    printf("  i/I - Insert new product barcode manually\n");
#if defined(BARCODE_ENABLED)
    printf("  b/B - Insert new product barcode using barcode-scanner\n");
#endif

    char get_key;
    char exit_key = 0;
    char bcr_buf[128] = {0};
    int hemispheres_counts = 0;
    int rotar_steps = 0;

    do
    {
        get_key = getchar();

        switch (get_key)
        {
            // Gracefull Exit
            case _ESC_:
                exit_key = 1;
                break;

            // Manual insert mode
            case 'i':
            case 'I':
                printf("ACTION: Type in the name.\n");
                scanf("%128s", bcr_buf);

process:

                press_enter();

                printf("ACTION: Shall we start? press return key.\n");
                press_enter();

                hemispheres_counts = 0;
                rotar_steps = 0;

                char product_filename[256] = {0};    
                strcpy(product_filename, imgdb);
                strcat(product_filename, bcr_buf);
                if (directory_exists_or_create(product_filename))
                {
                    printf("\n\n!!! ATTENTION: The product already exists !!!\n\n");
                    printf("\nEnter options:\n");
                    printf("  ESC - Exit the program\n");
                    printf("  i/I - Insert new product barcode manually\n");
#if defined(BARCODE_ENABLED)
                    printf("  b/B - Insert new product barcode using barcode-scanner\n");
#endif
                    break;
                }

                while (hemispheres_counts < MAX_HEMISPHERES)
                {
                    while (rotar_steps < MAX_ROTAR_STEPS)
                    {
                        for (i = 0; i < count; i++)
                        {
                        capture_to_memory(cams[i], context, (const char**)&data, &size);
                            char fname[64] = {0};
                            char mk_filename[256] = {0};
                            strcpy(mk_filename, product_filename);
                            snprintf(fname, sizeof(fname), "/%d-%d-%d.jpg", i, hemispheres_counts, rotar_steps);
                            strcat(mk_filename, fname); 
                            printf("file name %s\n", mk_filename);
                        f = fopen(mk_filename, "wb");
                        if (f) 
                            {
                        retval = fwrite (data, size, 1, f);
                        if (retval != size) 
                                {
                        printf("  fwrite size %ld, written %d\n", size, retval);
                        }
                        fclose(f);
                        } 
                            else 
                            {
                        printf("  fopen *.jpg failed. %s\n", strerror(errno));
                            }
                            usleep(500*1000);    
                        }
                        rotar_steps++;
                    }

                    rotar_steps = 0;
                    hemispheres_counts++;

                    if (hemispheres_counts < MAX_HEMISPHERES)
                    {
                        printf("Flip the product and hit 'RETURN' key\n");
                        press_enter();      // This expect some input from user, thats it.
                        printf("Started capturing other hemisphere!\n");
                    } else {
                        printf("Sampling Done for barcode: %s\n", bcr_buf);
                        printf(" -------------------------------------\n");
                        printf("\nEnter options:\n");
                        printf("  ESC - Exit the program\n");
                        printf("  i/I - Insert new product barcode manually\n");
#if defined(BARCODE_ENABLED)
                        printf("  b/B - Insert new product barcode using barcode-scanner\n");
#endif
                        break;
                    }
                }
                break;
        }
    } while (exit_key != 1);


exit_:
    /* 
     * Release all the resources.
     */

    printf("\nReleasing all the resources ... \n");
    for (i = 0; i < count; i++)
    {
    gp_camera_exit(cams[i], context);
    }
    if (cams) {
        free(cams);
    }

    free(imgdb);
#if defined(BARCODE_ENABLED)
    close_bcr();
#endif
    printf("Done.\n");
    return 0;
}