转自:C++ 获取图片文件信息 - 席纳霍霍 - 博客园

参考:图片头文件信息解析_adzcsx2的博客-CSDN博客_图片头文件

#include <gl/glut.h>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
using namespace cv;
#define JPEG_FILE_TYPE          1
#define BMP_FILE_TYPE           2
#define PNG_FILE_TYPE           3
#define GIF_FILE_TYPE           4/*通过文件头标识判断图片格式,jpg: FF, D8bmp: 42 4Dgif: 47 49 46 38png: 89 50 4E 47
*/class  ImageFile {public:static ImageFile* create(const string& path){FILE* fS;const char* filename = path.c_str();fS = fopen(filename,"rb");if (fS != NULL){int i;int iLen = path.length();int iPos = path.rfind('.');if (iPos <= 0){return NULL;}string name = path.substr(iPos + 1, iLen);cout << "iLen: " << iLen << endl;char s1[10];char s2[] = "jpg";char s3[] = "bmp";char s4[] = "gif";char s5[] = "png";char s6[] = "jpeg";for (i = 0; i < name.length(); i++){cout << name[i] << endl;s1[i] = name[i];}s1[i+1] = '\0';if (strncmp(s1, s2, 3) == 0 || strncmp(s1, s3, 3) == 0 || strncmp(s1, s4, 3) == 0 || strncmp(s1, s5, 3) == 0 || strncmp(s1, s6, 4) == 0)return new ImageFile(path);     //路径正确且图片文件格式是以上四种,创建文件对象else {cout << "error" << endl;return NULL;}}cout << "error 2" << endl;return NULL;}string type() const;string location() const;string fileName() const;double width() const;double height() const;private:ImageFile(const string& path);const char* src;string mpath;string mtype;double mwidth;double mheight;};int check_fileType(const unsigned char* buf)
{if (buf[0] == 0xFF && buf[1] == 0xd8 && buf[2] == 0xFF){return JPEG_FILE_TYPE;}if (buf[0] == 0x42 && buf[1] == 0x4d){return BMP_FILE_TYPE;}if (buf[0] == 0x47 && buf[1] == 0x49 && buf[2] == 0x46 && buf[3] == 0x38){return GIF_FILE_TYPE;}if (buf[0] == 0x89 && buf[1] == 0x50 && buf[2] == 0x4e && buf[3] == 0x47){return PNG_FILE_TYPE;}elsereturn 0;
}/*在构造函数内获取像素宽高:mwidth、mheigh*/ImageFile::ImageFile(const string& path)
{int type;mpath = path;mwidth = 0;mheight = 0;mtype = "";src = path.c_str();int i = 0;int size;unsigned char* buff = NULL;FILE* fp;if ((fp = fopen(src, "rb+")) == NULL){mtype = "The file was not opened!";return;}fseek(fp, 0, SEEK_END);size = ftell(fp);buff = (unsigned char*)malloc(size);if (buff)memset(buff, 0, size);fseek(fp, 0, SEEK_SET);if (fread(buff, 1, size, fp) != size){mtype = "read error!";return;}type = check_fileType(buff);switch (type){case JPEG_FILE_TYPE:mtype = "jpg file!";for (i = 0; i < size; i++){if (buff[i] == 0xff && buff[i + 1] == 0xc0){mwidth = (buff[i + 7] << 8) | buff[i + 8];mheight = (buff[i + 5] << 8) | buff[i + 6];break;}}break;case BMP_FILE_TYPE:mtype = "bmp file!";for (i = 0; i < size; i++){if (buff[i] == 0x28 && buff[i + 1] == 0x00){mwidth = (buff[i + 7] << 24) | buff[i + 6] << 16 | buff[i + 5] << 8 | buff[i + 4];mheight = (buff[i + 11] << 24) | buff[i + 10] << 16 | buff[i + 9] << 8 | buff[i + 8];break;}}break;case PNG_FILE_TYPE:mtype = "png file!";for (i = 0; i < size; i++){if (buff[i] == 0x49 && buff[i + 1] == 0x48){mheight = (buff[i + 8] << 24) | buff[i + 9] << 16 | buff[i + 10] << 8 | buff[i + 11];mwidth = (buff[i + 4] << 24) | buff[i + 5] << 16 | buff[i + 6] << 8 | buff[i + 7];break;}}break;case GIF_FILE_TYPE:mtype = "gif file!";for (i = 0; i < size; i++){if (buff[i] == 0x00 && buff[i + 1] == 0x2c){mwidth = (buff[i + 7] << 8) | buff[i + 6];mheight = (buff[i + 9] << 8) | buff[i + 8];break;}}break;default:break;}fclose(fp);free(buff);}string ImageFile::type() const
{return mtype;
}string ImageFile::location() const
{int length = mpath.length();int pos = mpath.rfind('/');while (pos == length - 1){pos = mpath.rfind('/', pos - 1);length--;}if (pos < 0){return "";}return mpath.substr(0, pos + 1);
}string ImageFile::fileName() const
{int length = mpath.length();int pos = mpath.rfind('/');while (pos == length - 1){pos = mpath.rfind('/', pos - 1);length--;}if (pos < 0){return "";}return mpath.substr(pos + 1, length);
}double ImageFile::width() const
{return mwidth;
}double ImageFile::height() const
{return mheight;
}int main()
{ImageFile* IMG = ImageFile::create("2.bmp");cout << IMG->width() << endl;return 0;
}

C++:获取图片文件信息-图片名称、类型、像素宽高相关推荐

  1. android 获取MP4文件的图片大小

    今天,简单讲讲怎么获取MP4视频的每一帧的图片大小. android使用 MediaMetadataRetriever 获取视频文件的 总时长 和视频的分辨率. 根据该方式获取视频信息可以看出不仅仅可 ...

  2. android播放器如何获取音乐文件信息

    转http://blog.csdn.net/hellofeiya/article/details/8464356, android自带的音乐播放器中,在获取音乐文件信息的时候是通过扫描得到相关信息的. ...

  3. jaudiotagger获取MP3文件信息及封面图片

    #jaudiotagger获取MP3文件信息及封面图片 ###maven依赖: <dependency><groupId>org</groupId><arti ...

  4. 上传图片并且判断图片大小、类型、宽高

    上传图片并且判断图片大小.类型.宽高 项目中需要上传图片并且判断大小.类型.宽高 changeCarousel(e){ //判断文件大小 var file = e.target.files[0]var ...

  5. php获取文件夹中的音乐文件_PHP获取音频文件信息的方法

    本篇文章主要介绍PHP获取音频文件信息的方法,感兴趣的朋友参考下,希望对大家有所帮助. 项目需求:现在有一个音频文件上传的功能,在上传后PHP需要获取这个音频文件的相关信息,例如:时长等,由于这个文件 ...

  6. 获取log文件信息,并保存至Excel文件

    获取log文件信息,并保存至Excel文件 日常工作中,可能需要我们从txt文件中截取海量的信息,只要寻找到这种信息的规律或者定位好信息情况,在我的txt文件中有海量的信息,而我只需要截取09/11/ ...

  7. python读取文件夹下所有图片_python 读取单文件夹中的图片文件信息保存到csv文件中...

    # -*- coding: utf-8 -*- # @Time : 2019-09-17 10:21 # @Author : scyllake import os import csv #要读取的文件 ...

  8. java获取pdf文件首页图片,用来当做封面

    目录 1.Maven依赖 2.代码 3.执行结果 4.图片效果 1.Maven依赖 <dependency><groupId>org.apache.pdfbox</gro ...

  9. 获取Resources文件下图片的精灵格式

    ReviewImg用于显示图片,所挂脚本为ImageName using UnityEngine; using System.Collections; public class ImageName : ...

最新文章

  1. ASP .NET Core Web MVC系列教程:使用ASP .NET Core创建MVC Web应用程序
  2. 小孩子如何学会语言?科学家使用计算机模型解释儿童语言学习过程
  3. 汇总pandas中dataframe的删除操作
  4. 启用Gzip压缩(IIS)提高客户端网站访问速度
  5. Eclipse+Tomcat7.0+MySQL 连接池设置
  6. 133. Leetcode 477. 汉明距离总和 (位运算-汉明距离相关题目)
  7. tesklink 管理员项目角色被修改后的解决方法
  8. Hyper-V 和Ubuntu Server 16.04 配置静态IP
  9. apr_pool -- 内存池
  10. Linux命令终端提示符显示-bash-4.2#解决方法【转载】
  11. c++ 读取访问权限冲突_Linux系统利用可执行文件的Capabilities实现权限提升
  12. fabric 启动peer_Hyperledger Fabric Peer 常用命令总结
  13. sqlserver垮库查询_sql跨库查询(sqlserver跨库查询)
  14. Matlab报错警告: 启动 Connector 时出现问题及解决方案
  15. 算法设计与分析基础(第3版)
  16. 快速开发一个混合APP(Hybrid APP)
  17. fw325r虚拟服务器,fw325r管理页面
  18. android MediaPlayer m3u8 播放
  19. 【计算机图形学 】绘制椭圆 | OpenGL+鼠标交互
  20. 2000 Followers-3D CSS text

热门文章

  1. 阿里道延:我对技术架构的理解与架构师角色的思考
  2. java批量执行多条Sql语句
  3. hadoop--HDFS的读写数据流程
  4. Mac下载安装sublime text 3
  5. 苹果面向Mac发布英特尔处理器漏洞缓解措施
  6. WinDbg 命令三部曲:(二)WinDbg SOS 扩展命令手
  7. canvas 粒子效果 - 手残实践纪录
  8. Step By Step 搭建 MySql MHA 集群
  9. sendBroadcast和sendStickyBroadcast的区别
  10. Windows Server 2003服务器安装前设置