ios - 如何使用 objective c 将 JPEG 文件转换为 BITMAP?

标签 ios objective-c bitmap jpeg

我想使用 Objective C 将 jpeg 文件转换为位图。我们必须将其转换为 png,但 Objective c 中没有将 jpeg 转换为位图的特定方法。

最佳答案

首先从 jpeg 图像创建 get BGR 数组。创建位图图像标题(所有 bmp 图像具有相同的标题)。将 header 附加到字节数组,然后像 B 字节然后 G 字节然后 r 字节一样附加 BGR 数组。然后最后将字节数组转换为 NSDATA。您将获得 BMP 图像的数据。此代码适用于具有 512 X 512 像素的图像,并且在 BMP 图像中被转换为 24 位 bmp 图像。在代码中 SIDE=512。 imgpath 类似于“users/admin”,destimgpath 类似于“users/admin/bmpfolder”。文件名将像 1.jpg

-(void) convertJpegToBmp: (NSString *)imgDirPath :(NSString *)destImgPath :(NSString *)fileName
{
NSString *jpegimagepath = [imgDirPath  stringByAppendingPathComponent:fileName]; 
NSString *fileNameWithoutExtension=[[fileName lastPathComponent] stringByDeletingPathExtension];
NSString *bmpFileName=[fileNameWithoutExtension  stringByAppendingString:@".bmp"];
NSString *bmpfilepath=[destImgPath stringByAppendingPathComponent:bmpFileName];
int totalbytesinbitmap=(int) (SIDE * SIDE * 3)+54;
Byte bytes[totalbytesinbitmap];
[self writeBitmapHeader:bytes];
UIImage *sourceImage1=[UIImage imageWithContentsOfFile:jpegimagepath];
if(sourceImage1==nil)
    return;
CGImageRef sourceImage = sourceImage1.CGImage;
CFDataRef theData;
theData = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage));
UInt8 *pixelDataS = (UInt8 *) CFDataGetBytePtr(theData);
int dataLength = (int)CFDataGetLength(theData);
int k=54;
int row=(int) SIDE -1 ;
int col=0;
int totalbytesinrow=(int) SIDE * 3;
for(int i=0;i<dataLength;){
    int red=pixelDataS[i++];
    int green=pixelDataS[i++];
    int blue=pixelDataS[i++];
        i++;
    if(col==totalbytesinrow){
        col=0;
        row--;
    }
    int location=(row * totalbytesinrow)+col+54;
    bytes[location]=blue;
    col++;
    k++;
    bytes[location+1]=green;
    col++;
    k++;
    bytes[location+2]=red;
    col++;
    k++;
}
NSData *TxData = [NSData dataWithBytesNoCopy:bytes length:totalbytesinbitmap freeWhenDone:NO];
[TxData writeToFile:bmpfilepath atomically:YES];
}

-(void) writeBitmapHeader:(Byte *) pixelData{
int BITMAPHEADER_SIZE= 14;
int BITMAPINFOHEADER_SIZE=40;
Byte bfType[]={ (Byte)'B', (Byte)'M'};
int bfSize=0;
int bfReserved1=0;
int bfReserved2=0;
int bfOffBits=BITMAPHEADER_SIZE+BITMAPINFOHEADER_SIZE;
int biSize = BITMAPINFOHEADER_SIZE;
int biWidth = 0;
int biHeight = 0;
int biPlanes = 1;
int biBitCount = 24;
int biCompression = 0;
int biSizeImage = 0x030000;
int biXPelsPerMeter = 0x0;
int biYPelsPerMeter = 0x0;
int biClrUsed = 0;
int biClrImportant = 0;
int parWidth=(int)SIDE;
int parHeight =(int)SIDE;
int pad= (4 -((parWidth * 3) %4)) * parHeight;
if((4 -((parWidth * 3) %4))==4)
{
    pad=0;
}
biSizeImage= ((parWidth * parHeight) *3)+pad;
bfSize= biSizeImage + BITMAPHEADER_SIZE+BITMAPINFOHEADER_SIZE;
biWidth=parWidth;
biHeight=parHeight;
int count=0;
pixelData[count++]=bfType[0];
pixelData[count++]=bfType[1];
count=[self intToDWord:bfSize :pixelData :count];
count=[self intToWord:bfReserved1 :pixelData :count];
count=[self intToWord:bfReserved2 :pixelData :count];
count=[self intToDWord:bfOffBits :pixelData :count];
count=[self intToDWord:biSize :pixelData :count];
count=[self intToDWord:biWidth :pixelData :count];
count=[self intToDWord:biHeight :pixelData :count];
count=[self intToWord:biPlanes :pixelData :count];
count=[self intToWord:biBitCount :pixelData :count];
count=[self intToDWord:biCompression :pixelData :count];
count=[self intToDWord:biSizeImage :pixelData :count];
count=[self intToDWord:biXPelsPerMeter :pixelData :count];
count=[self intToDWord:biYPelsPerMeter :pixelData :count];
count=[self intToDWord:biClrUsed :pixelData :count];
count=[self intToDWord:biClrImportant :pixelData :count];

}

-(int) intToWord :(int) parValue :(Byte *) pixelData :(int) count{
pixelData[count++]= (Byte) (parValue & 0x00ff);
pixelData[count++]= (Byte) ((parValue >> 8) & 0x00ff);
return count;
}

-(int) intToDWord :(int) parValue :(Byte *) pixelData :(int) count{
    pixelData[count++]= (Byte) (parValue & 0x000000FF);
    pixelData[count++]= (Byte) ((parValue >> 8) & 0x000000FF);
    pixelData[count++]= (Byte) ((parValue >> 16) & 0x000000FF);
    pixelData[count++]= (Byte) ((parValue >> 24) & 0x000000FF);
    return count;
}

关于ios - 如何使用 objective c 将 JPEG 文件转换为 BITMAP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44648897/

相关文章:

ios - 在 Objective-C 中定义常量的最佳方式

php - 有没有办法将VML文档转换为位图图像?

c - 在c中使用位图文件结构操作位图图像文件

ios - native 与跨平台 - 消息应用程序

iphone - 导航栏背景隐藏了右键,无法在下次查看时将其删除

ios - 桥接 header 导入找不到目标。如何正确导入?

objective-c - 如何在没有 UIScrollView 的情况下放大和缩小 UIImageView?

android - 检测颜色的圆圈会在OpenCV中产生 “image must be 8-bit single-channel in HoughCircle”错误

javascript - iOS WKWebView JS 在后台应用时不更新属性修改

iphone - dateWithTimeIntervalSince1970 在创建事件时不起作用