⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jpginfo.cpp

📁 DOS命令行程序, 查找指定目录及子目录下的所有文件, 得到JPG文件的长与宽的像素值的程序.
💻 CPP
字号:
// 查找所有的JPG文件并生成 JPG 文件长宽信息

#include "stdafx.h"
//#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#define MAC_FILENAMELENOPATH 1024

long g_file_count = 0;

FILE * fp_info;  // JPG信息文件
//#define MAX_JPG_HEADER 96000
#define MAX_JPG_HEADER 1000
unsigned char *g_jpg_header;

#define  TOSHORT(p) ((*((unsigned char *)(p))<<8)|(*((unsigned char *)(p)+1)))

int get_jpg_info(char *fn, unsigned short * width, unsigned short * height)
{
    FILE * fp_jpg;
    int i, readed, len;
    unsigned char *pos;

    fp_jpg = fopen(fn, "rb");
    if (NULL == fp_jpg)
    {
        printf("\n can not open file %s", fn);
        return 1;
    }
    readed = fread(g_jpg_header, 1, MAX_JPG_HEADER, fp_jpg);
    fclose(fp_jpg);
    if (readed < MAX_JPG_HEADER)
    {
        *height = 0;
        *width = 0;
        return 1;
    }
    
    if (0 != memcmp(g_jpg_header + 6, "JFIF", 4))
    {
        printf("\n %s seems not JPG file. not found JFIF in g_jpg_header",fn);
        return 3;
    }

    for (i=2;i<readed;)
    {
        pos = g_jpg_header + i;
        if (0xff != *pos)
        {
            //printf(" \n seems error %d len=%d\n", i, len);
            break;
        }
        if (0xc0 == *(pos+1))
        {
            pos += 5;
            //memcpy(height, pos, 2);
            *height = TOSHORT(pos);
            pos += 2;
            *width = TOSHORT(pos);
        }
        pos += 2;
        len = TOSHORT(pos);
        i += (2 + len);
    }
    return 0;
}

/*得到文件的长度*/
unsigned long get_file_len(char * fn)
{
    FILE * fp;
    unsigned long len;

    fp = fopen(fn, "r");
    if (NULL == fp)
    {
        printf("\n can not open file:%s", fn);
        return 0;
    }
    fseek(fp, 0, SEEK_END);
    len = ftell(fp);
    fclose(fp);
    return len;
}

// 做字符替换
void replace_str2(char * ori, char *dst, char be_replaced, char to_replace)
{
    char * pos, *pos_dst;
    char ch;
    
    pos_dst = dst;
    for (pos = ori;*pos!=0;pos++)
    {
        ch = *pos;
        if (ch == be_replaced)
        {
            *pos_dst++ = to_replace;
        }
        else
        {
            *pos_dst++ = ch;
        }
    }
    *pos_dst = 0;
}

// 做字符替换
void replace_str(char * ori, char be_replaced, char to_replace)
{
    char * pos;
    char ch;
    
    for (pos = ori;*pos!=0;pos++)
    {
        ch = *pos;
        if (ch == be_replaced)
        {
            *pos = to_replace;
        }
    }
}

#define MAX_LINE 10240
void file_process(char *dir, char *fn, WIN32_FIND_DATA *fd)
{
    unsigned short width, height;
    char fullfn[MAC_FILENAMELENOPATH];

    CTime tfile(fd->ftLastWriteTime);
    CTime basetime(2007, 9, 19, 22, 15, 0 ); // 10:15PM March 19, 1999

    // has already processed, pass
    if (tfile > basetime)
    {
        //fprintf(fp_info, "\n\nFile name = %s\nImage dimensions = %d x %d  Pixels",
        //    fn, 0, 0);
        return;
    }

    if ((fd->nFileSizeHigh = 0) && (fd->nFileSizeLow < MAX_JPG_HEADER))
    {
        fprintf(fp_info, "\n\nFile name = %s\nImage dimensions = %d x %d  Pixels",
            fn, 0, 0);
        return;
    }

    strcpy(fullfn, dir);
    strcat(fullfn, fn);

    width = 0;
    height = 0;
    get_jpg_info(fullfn, &width, &height);
    if (width > 1)
    {
        fprintf(fp_info, "\n\nFile name = %s\nImage dimensions = %d x %d  Pixels",
            fn, width, height);
    }
    else
    {
        fprintf(fp_info, "\n\nFile name = %s\nImage dimensions = %d x %d  Pixels",
            fn, 0, 0);
    }
    g_file_count++;
}

void file_process2(char *fn, WIN32_FIND_DATA *fd)
{
#define  MAX_SRC_FILE_LEN  (256000UL) // maximun source file len = 250k
    FILETIME local;
    SYSTEMTIME systm;

#if 0
    if (fd->nFileSizeHigh > 0)
    {
        return;
    }
    else if (fd->nFileSizeLow > MAX_SRC_FILE_LEN)
    {
        return;
    }

    // file modify time must be late than pre process time.
    if (fd->ftLastWriteTime.dwHighDateTime < g_pre_process.dwHighDateTime)
    {
        return;
    }
    else if (fd->ftLastWriteTime.dwHighDateTime == g_pre_process.dwHighDateTime)
    {
        if (fd->ftLastWriteTime.dwLowDateTime < g_pre_process.dwLowDateTime)
        {
            return;
        }
    }
#endif /*#if 0, Comment temporary */
    
    FileTimeToLocalFileTime(&(fd->ftLastWriteTime), &local);
    FileTimeToSystemTime(&local, &systm);
    printf("\n%s\t%04d-%02d-%02d %02d:%02d:%02d", fn,
        systm.wYear     ,
        systm.wMonth    ,
        systm.wDay      ,
        systm.wHour     ,
        systm.wMinute   ,
              systm.wSecond);
    g_file_count++;
}

/*文件名通配算法, 第一个参数为详细文件名, 第二个参数为通配符*/
BOOL FileMatch( LPCTSTR lpszFileName, LPCTSTR lpszMatch )
{
    if ( '\0' == *lpszFileName && '\0' == *lpszMatch )
    {
        // 如果都达到了字符串尾部,则说明匹配成功
        return TRUE;
    }
    if ( '\0' == *lpszMatch )
    {
        // 如果通配符字符串达到了尾部,则说明不匹配
        return FALSE;
    }
    if ( '\0' == *lpszFileName )
    {
        if ( '*' == *lpszMatch )
        {
            ++lpszMatch;
            return FileMatch( lpszFileName, lpszMatch );
        }
        else
        {
            return FALSE;
        }
    }
    if ( '?' == *lpszMatch )
    {                   
        ++lpszMatch;
        ++lpszFileName;
        return FileMatch( lpszFileName, lpszMatch );
    }
    if( '*' == *lpszMatch )
    {
        // 尝试匹配0个字符
        ++lpszMatch;
        if ( FileMatch( lpszFileName, lpszMatch ) )
        {
            return TRUE;
        }
        else
        {
            --lpszMatch;
        }
        // 尝试匹配1个字符
        ++lpszMatch;
        ++lpszFileName;
        if ( FileMatch( lpszFileName, lpszMatch ) )
        {
            return TRUE;
        }
        else
        {
            --lpszMatch;
            --lpszFileName;
        }
        // 尝试匹配多个字符
        ++lpszFileName;
        return FileMatch( lpszFileName, lpszMatch );
    }
    // 匹配普通字符
    if ( *lpszMatch != *lpszFileName )
    {
        return FALSE;
    }
    ++lpszMatch;
    ++lpszFileName;
    return FileMatch( lpszFileName, lpszMatch );
} 

/* rootDir : 搜索的起始目录, 带最后的\\, file_spec : 通配符*/
void FindFileInDir(char* rootDir, char * file_spec)
{
    char fname[MAC_FILENAMELENOPATH];
    WIN32_FIND_DATA fd;
    HANDLE hSearch;
    char filePathName[MAC_FILENAMELENOPATH];
    char tmpPath[MAC_FILENAMELENOPATH];
    BOOL bSearchFinished = FALSE;

    ZeroMemory(fname, MAC_FILENAMELENOPATH);
    ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
    ZeroMemory(filePathName, MAC_FILENAMELENOPATH);
    ZeroMemory(tmpPath, MAC_FILENAMELENOPATH);

    strcpy(filePathName, rootDir);
    strcat(filePathName, "*.*");
    
    hSearch = FindFirstFile(filePathName, &fd);
    
    //Is directory
    if (strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, ".."))
    {
        if( fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            strcpy(tmpPath, rootDir);
            strcat(tmpPath, fd.cFileName);
            strcat(tmpPath, "\\");
            FindFileInDir(tmpPath, file_spec);
        }
        else
        {
            if (FileMatch(fd.cFileName, file_spec))
            {
                //sprintf(fname, "%s%s", rootDir, fd.cFileName);
                file_process(rootDir, fd.cFileName, &fd);
            }
        }
    }

    while( !bSearchFinished )
    {
        if( FindNextFile(hSearch, &fd) )
        {
            if (strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, ".."))
            {
                if( fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                {
                    strcpy(tmpPath, rootDir);
                    strcat(tmpPath, fd.cFileName);
                    strcat(tmpPath, "\\");
                    FindFileInDir(tmpPath, file_spec);
                }
                else
                {

                    if (FileMatch(fd.cFileName, file_spec))
                    {
                        //sprintf(fname, "%s%s", rootDir, fd.cFileName);
                        file_process(rootDir, fd.cFileName, &fd);
                    }
                }
            }
        }
        else
        {
            bSearchFinished = TRUE; //Terminate Search
        }
    }
    
    FindClose(hSearch);
}

int main(int argc, char **argv)
{
    char dir[1024];

    //get_last_dir("D:\\photo.search@&yearsearch=2000&nr_of_rows=52772&first_this_page=29790&page_limit=15&sort_order=photo_id+DESC&nr_pages=3519.1.html", dir);

    if (argc < 3)
    {
        printf("\n usage : jpginfo dir file_name info_file_name");
        printf("\n    ex : jpginfo c:\\ *.jpg _jpginfo.txt");
        return 3;
    }

    strcpy(dir, argv[1]);
    if ('\\' != *(dir+strlen(dir)-1))
    {
        strcat(dir, "\\");
    }


    g_jpg_header = (unsigned char *)malloc(MAX_JPG_HEADER);
    if (NULL == g_jpg_header)
    {
        printf(" \ncan not alloc memory=%d\n", MAX_JPG_HEADER);
        return 5;
    }
    fp_info = fopen(argv[3], "w");
    if (NULL == fp_info)
    {
        printf("\n can not open %s for write" ,argv[3]);
        return 4;
    }
    
    // process file by file
    FindFileInDir(dir, argv[2]);


    fclose(fp_info);
    free(g_jpg_header);
    printf("\n\n total file count = %ld", g_file_count);

    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -