Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用objective c将JPEG文件转换为位图?_Ios_Objective C_Bitmap_Jpeg - Fatal编程技术网

Ios 如何使用objective c将JPEG文件转换为位图?

Ios 如何使用objective c将JPEG文件转换为位图?,ios,objective-c,bitmap,jpeg,Ios,Objective C,Bitmap,Jpeg,我想使用Objective C将jpeg文件转换为位图。我们必须将其转换为png,但Objective C中没有将jpeg转换为位图的特定方法。首先从jpeg图像创建get BGR数组。创建位图图像标题(所有bmp图像都有相同的标题)。将头附加到字节数组,然后像B字节、G字节、r字节那样附加BGR数组。最后将字节数组转换为NSDATA。您将获得BMP图像的数据。此代码适用于具有512 X 512像素的图像,并且在BMP图像中转换为24位BMP图像。在代码端=512。imgpath将类似于“use

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

首先从jpeg图像创建get BGR数组。创建位图图像标题(所有bmp图像都有相同的标题)。将头附加到字节数组,然后像B字节、G字节、r字节那样附加BGR数组。最后将字节数组转换为NSDATA。您将获得BMP图像的数据。此代码适用于具有512 X 512像素的图像,并且在BMP图像中转换为24位BMP图像。在代码端=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;
}
-(void)convertJpegToBmp:(NSString*)imgDirPath:(NSString*)destingPath:(NSString*)文件名
{
NSString*jpegimagepath=[imgDirPath stringByAppendingPathComponent:fileName];
NSString*fileName WithOutExtension=[[fileName lastPathComponent]stringByDeletingPathExtension];
NSString*bmpFileName=[FileName WithoutExtension stringByAppendingString:@.bmp”];
NSString*bmpfilepath=[DestingPath stringByAppendingPathComponent:bmpFileName];
int totalbytesinbitmap=(int)(边*边*3)+54;
字节字节[totalbytesinbitmap];
[self-WriteBitmapheder:字节];
UIImage*sourceImage1=[UIImage imageWithContentsOfFile:jpegimagepath];
如果(sourceImage1==nil)
返回;
CGImageRef sourceImage=sourceImage1.CGImage;
CFDataRef数据;
数据=CGDataProviderCopyData(CGImageGetDataProvider(sourceImage));
UInt8*像素数据=(UInt8*)CFDataGetBytePtr(数据);
int dataLength=(int)CFDataGetLength(theData);
int k=54;
int行=(int)边-1;
int col=0;
int totalbytesinrow=(int)边*3;
对于(int i=0;i>8)和0x00ff);
返回计数;
}
-(int)intToWord:(int)parValue:(字节*)pixelData:(int)计数{
pixelData[count++]=(字节)(parValue&0x000000FF);
pixelData[count++]=(字节)((parValue>>8)&0x000000FF);
pixelData[count++]=(字节)((parValue>>16)和0x000000FF);
pixelData[count++]=(字节)((parValue>>24)&0x000000FF);
返回计数;
}
检查