代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
获取图片文件格式方法
作者:
wqOoops
/ 发布于
2013/5/28
/
634
根据图片文件名和图片文件头标识判断图片格式.
#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 *Check_Image_Header(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 *Get_image_type(char *Filename) { assert(Filename != NULL); char *TypeName; char *pStart = NULL; char *pEnd = NULL; char *name; int i = 0; name = (char *)malloc(strlen(Filename) + 1); memset(name, 0, sizeof(name)); strcpy(name, Filename); pStart = name; pEnd = pStart + strlen(name) - 1; reserve(pStart, pEnd); TypeName = (char *)malloc(strlen(name) + 1); memset(TypeName, 0, sizeof(TypeName)); sscanf(name, "%[^.]", TypeName); // 取“.”前面的部分 pStart = TypeName; pEnd = pStart + strlen(TypeName) - 1; reserve(pStart, pEnd); 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 Check_Image_Header(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 = Get_image_type(argv[1]); if(!imageType) printf("not a picture!\n"); else printf("%s\n", imageType); return 0; }
试试其它关键字
文件格式
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
wqOoops
贡献的其它代码
(
1
)
.
获取图片文件格式方法
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3