使用目标c将jpeg转换为ios中的位图? [英] Converting jpeg to bitmap in ios using objective c?

查看:171
本文介绍了使用目标c将jpeg转换为ios中的位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有找到任何将Jpeg图像转换为24位BITMAP图像的示例或库。我已经在android和java中创建了示例,但没有找到任何线索将jpeg转换为ios中的位图。

I did not find any example or library to convert Jpeg image to 24 bit BITMAP image. I have created example in android and java but did not find any clue to convert jpeg to bitmap in ios.

推荐答案

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

First create get BGR array from jpeg image. create Bitmap image header(all bmp image have same header). Append Header to byte array and then append BGR array like B byte then G byte then r byte like wise. Then finally convert byte array to NSDATA. You will get data for BMP image. This code works for Image having 512 X 512 pixel and in BMP image is converted into 24bit bmp image. In code SIDE=512. imgpath will be like "users/admin" and destimgpath will be like "users/admin/bmpfolder". Filename will be like 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;
}

这篇关于使用目标c将jpeg转换为ios中的位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆