Menu

#108 File saved on system takes more space than the one saved on camera.

None
open
5
2014-05-24
2014-04-14
Gaurav Raj
No

I'm trying to capture image using NIKON 5200 camera and save it on camera sd card as well as my computer. I use the method given in the libgphoto2 sample. The image size in camera is ~6Mb while image size on my pc is ~15Mb. I don't understand why so much difference in the image size?

Discussion

  • Gaurav Raj

    Gaurav Raj - 2014-04-14

    This is the function used for the purpose

    int TestCamera::capture(const char filename, string &data, string &saveFileName){
    this->_is_busy = true;
    int ret, fd ;
    CameraFile
    file;
    CameraFilePath path;

    strcpy(path.folder, "/");
    strcpy(path.name, filename);
    
    ret = gp_camera_capture(this->_camera, GP_CAPTURE_IMAGE, &path, this->_ctx);
    if (ret != GP_OK) {
        this->_is_busy = false;
        return ret;
    }
    
    std::cout<<"Captured"<<std::endl;
    if(this->_save_images == false){
        ret = gp_file_new(&file);
    } else {
        fd  = open(filename, O_CREAT | O_RDWR, 0644);
        ret = gp_file_new_from_fd(&file, fd);
    }
    
    if (ret != GP_OK){
        this->_is_busy = false;
        return ret;
    }
    
    std::cout<<"New file created"<<std::endl;
    
    ret = gp_camera_file_get(this->_camera, path.folder, path.name, GP_FILE_TYPE_NORMAL, file, this->_ctx);
    
    if (ret != GP_OK) {
        this->_is_busy = false;
        return ret;
    }
    
    unsigned long int file_size = 0;
    const char *file_data = NULL;
    
    ret = gp_file_get_data_and_size (file, &file_data, &file_size);
    
    if (ret != GP_OK) {
        this->_is_busy = false;
        return ret;
    }
    
    FILE* pFile;
    pFile = fopen("temp.jpg", "wb");
    fwrite(file_data, 1, file_size*sizeof(char), pFile);
    fclose(pFile);
    std::cout<<"File saved to disk."<<std::endl;
    
    if(this->_save_images == false) {
        ret = gp_camera_file_delete(this->_camera, path.folder, path. name, this->_ctx);
        gp_file_free(file);
    
        if (ret != GP_OK) {
            this->_is_busy = false;
            return ret;
        }
    }
    
    int waittime = 10;
    CameraEventType type;
    void *eventdata;
    
    printf("Wait for events from camera\n");
    while(1) {
    
        gp_camera_wait_for_event(this->_camera, waittime, &type, &eventdata, this->_ctx);
    
        if(type == GP_EVENT_TIMEOUT) {
            break;
        } else if (type == GP_EVENT_CAPTURE_COMPLETE || type == GP_EVENT_FILE_ADDED) {
            waittime = 10;
        } else if (type != GP_EVENT_UNKNOWN) {
            printf("Unexpected event received from camera: %d\n", (int)type);
        }
    }
    
    this->_is_busy = false;
    return true;
    

    }

     
  • Marcus Meissner

    Marcus Meissner - 2014-04-24

    I am guessing the file you are looking at has two jpegs after each other for osme reason.

    if you are using the file_from_fd method, you do not need to write out the image seperately.

    the gp_file_get_data_and_size() might be incorrect here.

     
  • Marcus Meissner

    Marcus Meissner - 2014-04-24
    • assigned_to: Marcus Meissner
    • Group: -->
     
    • Gaurav Raj

      Gaurav Raj - 2014-05-22

      There was some issue with the fwrite statement. Earlier I used -
      fwrite(file_data, 1, file_size*sizeof(char), pFile);
      and now I'm using -
      fwrite(file_data, file_size, 1, pFile)

      Now things are working fine on Mac OSX but when I transferred my code to raspberry pi its still large. Don't know what's the issue now.

       
  • Marcus Meissner

    Marcus Meissner - 2014-05-22

    is it exactly twice the size on camera? can you try deleting the file before the fopen() to ensure you do not open a existing one?

     
  • Gaurav Raj

    Gaurav Raj - 2014-05-24

    No its not exactly double. The file ideally is 7 MB but sometimes takes 18 MB. And deleting the image doesn't help. Still trying to compare all variables on MAC during saving to the ones on Raspberry PI. I'll let you know as soon as I find anything.

     

Log in to post a comment.