static void saveJPGFile(const char* filename) {
    char backupName[400];
    struct stat buf;

#ifdef SUPERDEBUG
    LOGE("Modified: %s\n", filename);
#endif

    strncpy(backupName, filename, 395);
    strcat(backupName, ".t");

    // Remove any .old file name that may pre-exist
#ifdef SUPERDEBUG
    LOGE("removing backup %s", backupName);
#endif
    unlink(backupName);

    // Rename the old file.
#ifdef SUPERDEBUG
    LOGE("rename %s to %s", filename, backupName);
#endif
    rename(filename, backupName);

    // Write the new file.
#ifdef SUPERDEBUG
    LOGE("WriteJpegFile %s", filename);
#endif
    if (WriteJpegFile(filename)) {

        // Copy the access rights from original file
#ifdef SUPERDEBUG
        LOGE("stating old file %s", backupName);
#endif
        if (stat(backupName, &buf) == 0){
            // set Unix access rights and time to new file
            struct utimbuf mtime;
            chmod(filename, buf.st_mode);

            mtime.actime = buf.st_mtime;
            mtime.modtime = buf.st_mtime;

            utime(filename, &mtime);
        }

        // Now that we are done, remove original file.
#ifdef SUPERDEBUG
        LOGE("unlinking old file %s", backupName);
#endif
        unlink(backupName);
#ifdef SUPERDEBUG
        LOGE("returning from saveJPGFile");
#endif
    } else {
#ifdef SUPERDEBUG
        LOGE("WriteJpegFile failed, restoring from backup file");
#endif
        // move back the backup file
        rename(backupName, filename);
    }
}
Пример #2
0
void writeExif( void *origData, void *destData , int origSize , uint32_t *resultSize, int orientation,camera_position_type  *pt, int wb, int ledm ) {
  const char *filename = "/cache/tmp/temp.jpg";

    dump_to_file( filename, (uint8_t *)origData, origSize );
    LOGV("WRITE EXIF Filename %s", filename);
    chmod( filename, S_IRWXU );
    ResetJpgfile();


    memset(&ImageInfo, 0, sizeof(ImageInfo));
    ImageInfo.MeteringMode = -1;

    int gpsTag = 0;
    if( pt != NULL ) {
        LOGV("EXIF ADD GPS DATA ........");
        gpsTag = 6;
    } else{
        LOGV("EXIF NO GPS ........");
    }


    ExifElement_t *t = (ExifElement_t *)malloc( sizeof(ExifElement_t)*(EXIF_TOTAL_DATA+gpsTag) );

    ExifElement_t *it = t;
    // Store file date/time.
    (*it).Tag = TAG_ORIENTATION;
    (*it).Format = FMT_USHORT;
    (*it).DataLength = 1;
    unsigned short v;
    LOGV("EXIF Orientation %d", orientation);
    if( orientation == 90 ) {
        (*it).Value = "6\0";
    } else if( orientation == 180 ) {
        (*it).Value = "3\0";
    } else {
        (*it).Value = "1\0";
    }
    (*it).GpsTag = FALSE;

    it++;

    (*it).Tag = TAG_MAKE;
    (*it).Format = FMT_STRING;
    (*it).Value = "Motorola\0";
    (*it).DataLength = strlen((*it).Value);
    (*it).GpsTag = FALSE;

    it++;

    (*it).Tag = TAG_MODEL;
    (*it).Format = FMT_STRING;
    (*it).Value = "MB200 with CyanogenMOD\0";
    (*it).DataLength = strlen((*it).Value);
    (*it).GpsTag = FALSE;

    it++;

    (*it).Tag = TAG_FLASH;
    (*it).Format = FMT_USHORT;
    (*it).Value = (ledm ? "1\0" : "0\0");
    (*it).DataLength = 1;
    (*it).GpsTag = FALSE;

    it++;

    (*it).Tag = TAG_WHITEBALANCE;
    (*it).Format = FMT_USHORT;
    (*it).Value = (wb ? "1\0" : "0\0");
    (*it).DataLength = 1;
    (*it).GpsTag = FALSE;

    if( pt != NULL ) {
        LOGV("pt->latitude == %f", pt->latitude );
        LOGV("pt->longitude == %f", pt->longitude );
        LOGV("pt->altitude == %d", pt->altitude );

        it++;
        (*it).Tag = 0x01;
        (*it).Format = FMT_STRING;
        if( pt->latitude > 0 ) {
            (*it).Value = "N\0";
        } else {
            (*it).Value = "S\0";
        }
        (*it).DataLength = 2;
        (*it).GpsTag = TRUE;

        it++;
        (*it).Value = coord2degminsec( pt->latitude );
        LOGV("writeExif: latitude is: %s", (*it).Value);

        (*it).Tag = 0x02;
        (*it).Format = FMT_URATIONAL;
        (*it).DataLength = 3;
        (*it).GpsTag = TRUE;

        it++;
        (*it).Tag = 0x03;
        (*it).Format = FMT_STRING;
        if( (*pt).longitude > 0 ) {
            (*it).Value = "E\0";
        } else {
            (*it).Value = "W\0";
        }
        (*it).DataLength = 2;
        (*it).GpsTag = TRUE;

        it++;
        (*it).Value = coord2degminsec( pt->longitude );
        LOGV("writeExif: longitude is: %s", (*it).Value);

        (*it).Tag = 0x04;
        (*it).Format = FMT_URATIONAL;
        (*it).DataLength = 3;
        (*it).GpsTag = TRUE;

        it++;
        (*it).Tag = 0x05;
        (*it).Format = FMT_USHORT;
        if( (*pt).altitude > 0 ) {
            (*it).Value = "0\0";
        } else {
            (*it).Value = "1\0";
        }
        (*it).DataLength = 1;
        (*it).GpsTag = TRUE;

        it++;
        (*it).Value = float2rationnal( fabs( pt->altitude ) );
        LOGV("writeExif: altitude is: %s", (*it).Value);

        (*it).Tag = 0x06;
        (*it).Format = FMT_SRATIONAL;
        (*it).DataLength = 1;
        (*it).GpsTag = TRUE;
    }

   {
        struct stat st;
        if (stat(filename, &st) >= 0) {
            ImageInfo.FileDateTime = st.st_mtime;
            ImageInfo.FileSize = st.st_size;
        }
    }
    strncpy(ImageInfo.FileName, filename, PATH_MAX);
    LOGV("Image EXIF Filename %s", filename);

    ReadMode_t ReadMode;
    ReadMode = READ_METADATA;
    ReadMode |= READ_IMAGE;
    int res = ReadJpegFile(filename, (ReadMode_t)ReadMode );
    LOGV("READ EXIF Filename %s", filename);

    create_EXIF( t, EXIF_TOTAL_DATA, gpsTag);

    WriteJpegFile(filename);
    chmod( filename, S_IRWXU );
    DiscardData();

    FILE *src;
    src = fopen( filename, "r");

    fseek( src, 0L, SEEK_END );
    (*resultSize) = ftell(src);
    fseek( src, 0L, SEEK_SET );

    int read = fread( destData, 1, (*resultSize), src );

    free( t );

    unlink( filename );
}