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

转自: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, D8
  bmp: 42 4D
  gif: 47 49 46 38
  png: 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;
    }
    else
        return 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;
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值