根據圖片文件名和圖片文件頭標識判斷圖片格式...具體請看這篇文章:http://www.cnblogs.com/sunsoft/archive/2012/01/04/2311989.html html
下面是本人本身寫的一點代碼,但願有高人路過,指點更高名的方法,謝謝。 指針
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #include <errno.h> #define EFFECTIVELEN 10 #define GETBYTELEN 2 /*---JPEG/JPG(1)---*/ #define IMAGE_JPEG_JPG "JPEG/JPG" #define JPEG_FIRSTBIT "FF" #define JPEG_SECONDBIT "D8" /*---BMP(2)---*/ #define IMAGE_BMP "BMP" #define BMP_FIRSTBIT "42" #define BMP_SECONDBIT "4D" /*---PNG(3)---*/ #define IMAGE_PNG "PNG" #define PNG_FIRSTBIT "89" #define PNG_SECONDBIT "50" /*---GIF(4)---*/ #define IMAGE_GIF "GIF" #define GIF_FIRSTBIT "47" #define GIF_SECONDBIT "49" #define GIF_THIRDBIT "46" #define GIF_FIFTHBIT1 "39" // 可能存在是37 #define GIF_FIFTHBIT2 "37" /*---TIFF(5)---*/ #define IMAGE_TIFF "TIFF" #define TIFF_FIRSTBIT "4D" #define TIFF_SECONDBIT "4D" #define TIFF_FIRSTBIT2 "49" // 或者 #define TIFF_SECONDBIT2 "49" /*---ICO(6)---*/ #define IMAGE_ICO "ICO" #define ICO_THIRDBIT "1" // 01 #define ICO_FIFTHBIT "1" // 01 /*---TGA(7)---*/ #define IMAGE_TGA "TGA" #define TGA_THIRDBIT "2" // 02 #define TGA_FIFTHBIT "0" // 00 /*---CUR(8)---*/ #define IMAGE_CUR "CUR" #define CUR_THIRDBIT "2" // 02 #define CUR_FIFTHBIT "1" // 01 /*---PCX(9)---*/ #define IMAGE_PCX "PCX" #define PCX_FIRSTBIT "A" // 0A /*---IFF(10)---*/ #define IMAGE_IFF "IFF" #define IFF_FIRSTBIT "46" #define IFF_SECONDBIT "4F" #define IFF_THIRDBIT "52" #define IFF_FORTHBIT "4D" /*---ANI(11)---*/ #define IMAGE_ANI "ANI" #define ANI_FIRSTBIT "52" #define ANI_SECONDBIT "49" #define ANI_THIRDBIT "46" #define ANI_FORTHBIT "46" char MImagesType[12][10] = {"JPEG","JPG","BMP","PNG","GIF","TIFF","aICO","TGA", "CUR","PCX","IFF","ANI"}; char LImagesType[12][10] = {"jpeg","jpg","bmp","png","gif","tiff","aico","tga", "cur","pcx","iff","ani"}; // 反轉字符串 void reserve(char *p, char *q) { while(p < q){ *p ^= *q; *q ^= *p; *p ^= *q; p++; q--; } } char *JudgmentImageByDate(char *Filename) { FILE *fd; unsigned char *imagebuf; int filesize = 0; unsigned char firstBit[3]; unsigned char secondBit[3]; unsigned char thirdBit[3]; unsigned char forthBit[3]; unsigned char fifthBit[3]; assert(Filename != NULL); if( (fd = fopen(Filename, "rb")) == NULL){ perror("fopen error\n"); exit(1); } fseek(fd, 0, SEEK_END); filesize = ftell(fd); // 返回當前文件位置,也就是說返回FILE指針當前位置。 rewind(fd); // 將文件內部的位置指針從新指向一個流(數據流/文件)的開頭 imagebuf = (unsigned char *)malloc(EFFECTIVELEN * sizeof(unsigned char) + 1); fread(imagebuf, sizeof(unsigned char), EFFECTIVELEN, fd); snprintf(firstBit, sizeof(firstBit), "%X", imagebuf[0]); snprintf(secondBit, sizeof(secondBit), "%X", imagebuf[1]); snprintf(thirdBit, sizeof(thirdBit), "%X", imagebuf[2]); snprintf(forthBit, sizeof(forthBit), "%X", imagebuf[3]); snprintf(fifthBit, sizeof(fifthBit), "%X", imagebuf[4]); if( (0 == strncmp(firstBit, JPEG_FIRSTBIT, GETBYTELEN)) && (0 == strncmp(secondBit, JPEG_SECONDBIT, GETBYTELEN))) { return IMAGE_JPEG_JPG; } if( (0 == strncmp(firstBit, BMP_FIRSTBIT, GETBYTELEN)) && (0 == strncmp(secondBit, BMP_SECONDBIT, GETBYTELEN))) { return IMAGE_BMP; } if( (0 == strncmp(firstBit, PNG_FIRSTBIT, GETBYTELEN)) && (0 == strncmp(secondBit, PNG_SECONDBIT, GETBYTELEN))) { return IMAGE_PNG; } if( (0 == strncmp(firstBit, GIF_FIRSTBIT, GETBYTELEN)) && (0 == strncmp(secondBit, GIF_SECONDBIT, GETBYTELEN)) && (0 == strncmp(thirdBit, GIF_THIRDBIT, GETBYTELEN)) && ((0 == strncmp(fifthBit, GIF_FIFTHBIT1, GETBYTELEN)) || (0 == strncmp(fifthBit, GIF_FIFTHBIT2, GETBYTELEN)))) { return IMAGE_GIF; } if( ((0 == strncmp(firstBit, TIFF_FIRSTBIT, GETBYTELEN)) && (0 == strncmp(secondBit, TIFF_SECONDBIT, GETBYTELEN))) || ((0 == strncmp(firstBit, TIFF_FIRSTBIT2, GETBYTELEN)) && (0 == strncmp(secondBit, TIFF_SECONDBIT2, GETBYTELEN)))) { return IMAGE_TIFF; } if( (0 == strncmp(thirdBit, ICO_THIRDBIT, GETBYTELEN)) && (0 == strncmp(fifthBit, ICO_FIFTHBIT, GETBYTELEN))){ return IMAGE_ICO; } if( (0 == strncmp(thirdBit, TGA_THIRDBIT, GETBYTELEN)) && (0 == strncmp(fifthBit, TGA_FIFTHBIT, GETBYTELEN))){ return IMAGE_TGA; } if( (0 == strncmp(thirdBit, CUR_THIRDBIT, GETBYTELEN)) && (0 == strncmp(fifthBit, CUR_FIFTHBIT, GETBYTELEN))){ return IMAGE_CUR; } if(0 == strncmp(firstBit, PCX_FIRSTBIT, GETBYTELEN)){ return IMAGE_PCX; } if( (0 == strncmp(firstBit, IFF_FIRSTBIT, GETBYTELEN)) && (0 == strncmp(secondBit, IFF_SECONDBIT, GETBYTELEN)) && (0 == strncmp(thirdBit, IFF_THIRDBIT, GETBYTELEN)) && (0 == strncmp(forthBit, IFF_FORTHBIT, GETBYTELEN))) { return IMAGE_IFF; } if( (0 == strncmp(firstBit, ANI_FIRSTBIT, GETBYTELEN)) && (0 == strncmp(secondBit, ANI_SECONDBIT, GETBYTELEN)) && (0 == strncmp(thirdBit, ANI_THIRDBIT, GETBYTELEN)) && (0 == strncmp(forthBit, ANI_FORTHBIT, GETBYTELEN))) { return IMAGE_ANI; } free(imagebuf); imagebuf = NULL; fclose(fd); return NULL; } char *JudgmentImageByName(char *Filename) { assert(Filename != NULL); char *TypeName; char *pStart = NULL; char *qEnd = NULL; char *name; int i = 0; name = (char *)malloc(strlen(Filename) + 1); memset(name, 0, sizeof(name)); strcpy(name, Filename); pStart = name; qEnd = pStart + strlen(name) - 1; reserve(pStart, qEnd); TypeName = (char *)malloc(strlen(name) + 1); memset(TypeName, 0, sizeof(TypeName)); sscanf(name, "%[^.]", TypeName); // 取「.」前面的部分 pStart = TypeName; qEnd = pStart + strlen(TypeName) - 1; reserve(pStart, qEnd); for(i=0; i<12; i++){ #if 0 if( (0 == strncmp(TypeName, MImagesType[i], strlen(MImagesType[i]))) || (0 == strncmp(TypeName, LImagesType[i], strlen(LImagesType[i])))) // 只匹配部分字符串,致使出錯。 return MImagesType[i]; #endif if( (0 == strcmp(TypeName, MImagesType[i])) || (0 == strcmp(TypeName, LImagesType[i]))) return MImagesType[i]; } free(name); free(TypeName); name = NULL; TypeName = NULL; return JudgmentImageByDate(Filename); } int main(int argc, char *argv[]) { int res = -1; char *imageType; if(argc < 2){ printf("usage: %s <imagefilename>\n", argv[0]); exit(1); } imageType = JudgmentImageByName(argv[1]); if(!imageType) printf("not a picture!\n"); else printf("%s\n", imageType); return 0; }