1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
| #include "imageinfo.h" #include <iostream> #include <fstream> #include <stdio.h> #include <stdlib.h> #include <QFile> #include <QFileInfo>
ImageInfo::ImageInfo(QObject *parent) : QObject(parent) { qDebug() << "---------------------------- image info constructed "; } ImageInfo::~ImageInfo() { } QDate ImageInfo::getImageDate(QString imageUrl) { QDate date; if(!imageUrl.isEmpty()) { QUrl fileUrl(imageUrl); QString filePath = fileUrl.toLocalFile(); if(QFile::exists(filePath)) { QFileInfo fileinfo(filePath); date = fileinfo.lastModified().date(); } } return date; }
int ImageInfo::getImageFormat(std::string path) { unsigned char BMPHeader[] = {0x42, 0x4d}; unsigned char JPGHeader1[] = {0xff, 0xd8, 0xff, 0xdb}; unsigned char JPGHeader2[] = {0xff, 0xd8, 0xff, 0xe0}; unsigned char JPGHeader3[] = {0xff, 0xd8, 0xff, 0xe1}; unsigned char JPGHeader4[] = {0xff, 0xd8, 0xff, 0xe2}; unsigned char JPGHeader5[] = {0xff, 0xd8, 0xff, 0xe3}; unsigned char JPGHeader6[] = {0xff, 0xd8, 0xff, 0xe8}; unsigned char GIFHeader1[] = {0x47, 0x49, 0x46, 0x38, 0x37, 0x61}; unsigned char GIFHeader2[] = {0x47, 0x49, 0x46, 0x38, 0x39, 0x61}; unsigned char PNGHeader[] = {0x89, 0x50, 0x4E, 0x47}; int count = 0; int step = 2; unsigned char header[16]; qDebug()<<"文件路径: "<<path.c_str(); std::ifstream readf(path.c_str(), std::ios::binary); if(!readf.is_open()) { qDebug()<<"打开文件失败"; return NVL_FORMAT; } for(int i=0; i<step; i++) { readf>>header[count+i]; } count = count + step; if(memcmp(header, BMPHeader, count) == 0) { qDebug()<<"文件格式特征码:"; for(int i=0; i<count; i++) { printf("%0x\t",header[i]); } printf("\n"); qDebug()<<"BMP格式"; return BMP_FORMAT; } for(int i=0; i<step; i++) { readf>>header[count+i]; } count = count + step; if((memcmp(header, JPGHeader1, count) == 0) || (memcmp(header, JPGHeader2, count) == 0) || (memcmp(header, JPGHeader3, count) == 0) || (memcmp(header, JPGHeader4, count) == 0) || (memcmp(header, JPGHeader5, count) == 0) || (memcmp(header, JPGHeader6, count) == 0)) { qDebug()<<"文件格式特征码:"; for(int i=0; i<count; i++) { printf("%0x\t",header[i]); } printf("\n"); qDebug()<<"JPG格式"; return JPG_FORMAT; } else if(memcmp(header, PNGHeader, count) == 0) { qDebug()<<"文件格式特征码:"; for(int i=0; i<count; i++) { printf("%0x\t",header[i]); } printf("\n"); qDebug()<<"PNG格式"; return PNG_FORMAT; } for(int i=0; i<step; i++) { readf>>header[count+i]; } count = count + step; if((memcmp(header, GIFHeader1, count) == 0) || (memcmp(header, GIFHeader2, count) == 0)) { qDebug()<<"文件格式特征码:"; for(int i=0; i<count; i++) { printf("%0x\t",header[i]); } printf("\n"); qDebug()<<"GIF格式"; return GIF_FORMAT; } qDebug()<<"文件格式特征码:"; for(int i=0; i<count; i++) { printf("%0x\t",header[i]); } printf("\n"); qDebug()<<"不属于以上任何一种格式"; return NVL_FORMAT; } QString ImageInfo::getImageFormat(QString imageUrl) { QString strFormat = "NA"; if(!imageUrl.isEmpty()) { QUrl fileUrl(imageUrl); QString filePath = fileUrl.toLocalFile(); if(QFile::exists(filePath)) { std::string path = filePath.toStdString(); int iFormat = getImageFormat(path); switch(iFormat) { case BMP_FORMAT: strFormat = "BMP"; break; case JPG_FORMAT: strFormat = "JPG"; break; case GIF_FORMAT: strFormat = "GIF"; break; case PNG_FORMAT: strFormat = "PNG"; break; default: break; } } } return strFormat; } QString ImageInfo::getImageSize(QString imageUrl) { QString strSize; long size = 0; if(!imageUrl.isEmpty()) { QUrl fileUrl(imageUrl); QString filePath = fileUrl.toLocalFile(); if(QFile::exists(filePath)) { QFile file(filePath); bool ret = file.open(QIODevice::ReadOnly); if (!ret) { qDebug()<<"打开文件失败"; size = 0; } else { size = file.size(); } file.close(); } } qDebug()<<"!!!!!"<<size; strSize = QString::number(size, 10); qDebug()<<strSize; return strSize; }
long ImageInfo::getBMPSize(std::string path) { FILE *fid; long int size; if((fid=fopen(path.c_str(),"rb+"))==NULL) { qDebug()<<"打开文件失败"; return 0; } fseek(fid, 2, SEEK_SET); fread(&size, sizeof(long), 1, fid); fclose(fid); qDebug()<<"size="<<size; return size; } long ImageInfo::getGIFSize(std::string path) { Q_UNUSED(path); return 0; } long ImageInfo::getPNGSize(std::string path) { Q_UNUSED(path); return 0; } long ImageInfo::getJPGSize(std::string path) { FILE *fid; long int size; if((fid = fopen(path.c_str(),"rb+")) == NULL) { qDebug()<<"打开文件失败"; return 0; } fseek(fid, 0, SEEK_END); size = ftell(fid); fclose(fid); qDebug()<<"size="<<size; return size; }
QSize ImageInfo::getBMPDimension(std::string path) { FILE *fid; if((fid=fopen(path.c_str(),"rb+"))==NULL) { qDebug()<<"打开文件失败"; return QSize(0, 0); } long int width; long int height; fseek(fid, 18, SEEK_SET); fread(&width, sizeof(long), 1, fid); fread(&height, sizeof(long), 1, fid); qDebug()<<"width="<<width; qDebug()<<"height="<<height; fclose(fid); return QSize(width, height); }
QSize ImageInfo::getJPGDimension(std::string path) { FILE *fid; if((fid = fopen(path.c_str(),"rb+")) == NULL) { qDebug()<<"打开文件失败"; return QSize(0, 0); } long int width; long int height; fseek(fid,0,SEEK_END); long length = ftell(fid); unsigned char *buffer = new unsigned char[length]; unsigned char *buffer_bakup = buffer; fseek(fid, 0, SEEK_SET); fread(buffer, length, 1, fid); fclose(fid); unsigned char *temp = buffer + length; unsigned char *temp_ori = buffer; unsigned char ff; unsigned char type=0xff; int m_pos = 0; for(int i=0; i<2; i++) { buffer++; } while((temp > buffer) && (type != 0xDA)) { do{ ff = *buffer++; } while(ff != 0xff); do{ type = *buffer++; } while(type == 0xff); switch(type) { case 0x00: case 0x01: case 0xD0: case 0xD1: case 0xD2: case 0xD3: case 0xD4: case 0xD5: case 0xD6: case 0xD7: break; case 0xC0: temp_ori = buffer; m_pos = (*buffer++)<<8; m_pos += *buffer++; buffer++; height = (*buffer++)<<8; height += *buffer++; width = (*buffer++)<<8; width += *buffer; break; case 0xE0: qDebug()<<"APP0段"; temp_ori = buffer; m_pos = (*buffer++)<<8; m_pos += *buffer++; buffer = buffer + 12; break; default: temp_ori = buffer; m_pos = (*buffer++)<<8; m_pos += *buffer++; break; } buffer = temp_ori + m_pos; } qDebug()<<"width="<<width; qDebug()<<"height="<<height; delete[] buffer_bakup; return QSize(width, height); }
QSize ImageInfo::getPNGDimension(std::string path) { FILE *fid = NULL; if((fid=fopen(path.c_str(),"rb+"))==NULL) { qDebug()<<"打开文件失败"; return QSize(0, 0); } long int width; long int height; unsigned char wtmp[4]={'0'}; unsigned char htmp[4]={'0'}; fseek(fid, 16, SEEK_SET); fread(wtmp, 4, 1, fid); fread(htmp, 4, 1, fid); fclose(fid); width = ((int)(unsigned char)wtmp[2]) * 256 + (int)(unsigned char)wtmp[3]; height = ((int)(unsigned char)htmp[2]) * 256 + (int)(unsigned char)htmp[3]; qDebug()<<"width="<<width; qDebug()<<"height="<<height; return QSize(width, height); }
QSize ImageInfo::getGIFDimension(std::string path) { std::ifstream ffin(path.c_str(), std::ios::binary); if (!ffin){ std::cout<<"Can not open this file."<<std::endl; return QSize(0, 0); } long int width; long int height; char s1[2] = {0}, s2[2] = {0}; ffin.seekg(6); ffin.read(s1, 2); ffin.read(s2, 2); width = (unsigned int)(s1[1])<<8|(unsigned int)(s1[0]); height = (unsigned int)(s2[1])<<8|(unsigned int)(s2[0]); ffin.close(); qDebug()<<"width="<<width; qDebug()<<"height="<<height; return QSize(width, height); } QSize ImageInfo::getImageDimension(QString imageUrl) { QSize dimension; if(!imageUrl.isEmpty()) { QUrl fileUrl(imageUrl); QString filePath = fileUrl.toLocalFile(); if(QFile::exists(filePath)) { std::string path = filePath.toStdString(); int iFormat = getImageFormat(path); switch(iFormat) { case BMP_FORMAT: dimension = getBMPDimension(path); break; case JPG_FORMAT: dimension = getJPGDimension(path); break; case GIF_FORMAT: dimension = getGIFDimension(path); break; case PNG_FORMAT: dimension = getPNGDimension(path); break; default: break; } } } qDebug()<<"图片尺寸:"<<dimension; return dimension; } QString ImageInfo::getImageTitle(QString imageUrl) { QString title; if(!imageUrl.isEmpty()) { QUrl fileUrl(imageUrl); QString filePath = fileUrl.toLocalFile(); if(QFile::exists(filePath)) { QFileInfo fileinfo(filePath); title = fileinfo.baseName(); } } return title; }
|