图片位深的获取

大概思路:图像的位深度,就存储在文件的byte数组里,找到位置,就能获取到。
//获取bmp图片位深
int getBmpBitDepth(constQString&imagePath)
{
QFile imageFile(imagePath);
if(!imageFile.open(QIODevice::ReadWrite)){
return -1;
}
QByteArray ba =imageFile.readAll();
int bitsPerPixel =(ba[28]& 0xff)| (ba[29]& 0xff)<< 8;
printf("Bmpdepth=%d\n",bitsPerPixel);
imageFile.close();
return bitsPerPixel;
}

//获取png图片位深
int getPngBitDepth(constQString&imagePath)
{
QFile imageFile(imagePath);
if(!imageFile.open(QIODevice::ReadWrite)){
return -1;
}
QByteArray ba =imageFile.readAll();

int bitsPerPixel =ba[24]& 0xff;
if((ba[25]& 0xff)== 2){
bitsPerPixel*= 3;
}else if((ba[25]& 0xff)== 6){
bitsPerPixel*= 4;
}
printf("Pngdepth=%d\n",bitsPerPixel);
imageFile.close();
return bitsPerPixel;
}

//获取Jpeg图片位深
int getJpegBitDepth(constQString&imagePath)
{
QFile imageFile(imagePath);
if(!imageFile.open(QIODevice::ReadWrite)){
return -1;
}
QByteArray ba =imageFile.readAll();

int i =2;
int bitsPerPixel=0;
while(true){
int marker =(ba[i] &0xff)<< 8| (ba[i+ 1]& 0xff);
int size =(ba[i +2]& 0xff)<< 8| (ba[i+ 3]& 0xff);
if(marker >=0xffc0&& marker<= 0xffcf&& marker!= 0xffc4
&&marker !=0xffc8){
bitsPerPixel= (ba[i+ 4]& 0xff)* (ba[i+ 9]& 0xff);
break;
}else{
i+= size+ 2;
}
}
printf("Jpegdepth=%d\n",bitsPerPixel);
imageFile.close();
return bitsPerPixel;
}


//获取gif位深
gif:获取到是第一张图片的位深度,对于那种多张图片组成的动态图的位深度,应该是所有图片中位深度最高的那个才是最终的值。  

int bitsPerPixel=(bytes[10] & 0x07) + 1;  

int main()
{
QString path_0("D:\\workspace\\aaa.bmp");
int bmpDepth =getBmpBitDepth(path_0);

QString path_1("D:\\workspace\\tray.png");
int pngDepth =getPngBitDepth(path_1);

QString path_2("D:\\workspace\\1111.jpg");
int jpegDepth =getJpegBitDepth(path_2);

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值