使用 Objective C 在 ios 中将 jpeg 转换为位图? [关闭]

2024-04-25

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


首先从 jpeg 图像创建获取 BGR 数组。创建位图图像标题(所有 bmp 图像都有相同的标题)。将标头附加到字节数组,然后附加 BGR 数组,如 B 字节、G 字节、r 字节。然后最后将字节数组转换为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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Objective C 在 ios 中将 jpeg 转换为位图? [关闭] 的相关文章

  • 仅为 UITableView 中的某个部分启用编辑模式

    我有一个tableView其中有一个可编辑的部分 如果我启用整个编辑tableView 其他单元格在编辑模式下不是selectable 所以我需要仅在特定部分启用编辑模式 以便其他单元格selectable 该部分是可编辑的 我需要设置编辑
  • 尝试在写入事务之外修改对象

    所以我不知道为什么会出现这个错误 错误信息如下 由于未捕获的异常 RLMException 而终止应用程序 原因 尝试在写入事务之外修改对象 首先在 RLMRealm 实例上调用 beginWriteTransaction 首先抛出调用堆栈
  • iPhone 拒绝了发布请求 未说明

    iPhone 拒绝了发布请求 内部启动错误 进程启动失败 未指定 这个错误让我抓狂 我似乎无法解决它 我从发现的所有地方都做了以下操作 刷新证书 注销并进入开发者苹果帐户 下载手动证书 删除 Apple 全球证书 重新启动 Mac 和 iP
  • 为 UILabel 设置不同字体的问题

    我想将字体大小和姓氏设置为 titleLabel Helvetica Neue UltraLight titleLabel setFont UIFont fontWithName Helvetica Neue UltraLight size
  • 如何使用UIPageViewController跳转到特定页面?

    我正在使用 Xcode 8 的默认基于页面的应用程序 并且我一直在尝试跳转到特定页面 而不是左右滑动来转动 我在 StackOverflow 上发现了类似的问题 但答案大多建议使用这种方法 setViewControllers direct
  • Swift 2 中的 segue 没有后退按钮

    我刚刚将我的项目移植到 Swift 2 一切都运行良好 除了即使是最简单的 segues 也没有后退按钮 这是我正在使用的 segue 函数的准备 override func prepareForSegue segue UIStoryboa
  • 将大块位图转换为 3 维位图

    Problem 我需要这个大量的数据作为输入 对于基于C的arduino 这是上面示例中所需格式的大量数据 const byte bitmap 8 8 0xFF 0x81 0x81 0x81 0x81 0x81 0x81 0xFF 0x81
  • iOS 应用程序上的 Youtube API v3。我的 API 密钥不起作用,但其他人的密钥可以在同一应用程序上起作用。错误403

    这可能是 Google 的 Youtube 团队直接提出的问题 但我想先在这里问 以防将来也能帮助其他人 我在 iOS 应用程序上使用简单的 API 密钥 没有 OAuth 2 0 该应用程序只是从特定的播放列表 ID 返回视频 ID 列表
  • 如何从 os_log() 查找源文件和行号

    The 记录 Apple 参考 https developer apple com reference os 1891852 logging对于 iOS 10 和 macOS Sierra 中的新日志记录系统 明确表示不要包含行号和源文件信
  • “推”匹配 UIInterpolatingMotionEffect? (即访问 UIInterpolatingMotionEffect 上的“物理”)

    借助 UIInterpolatingMotionEffect 扭转 iPhone 即可让图像移动 现在 想象一个红色块 您将使用 UICollisionBehavior 和 UIDynamicItemBehavior 在屏幕上 弹跳 当用户
  • Alamofire 使用公共键和多个值传递参数?

    我需要在我的项目中执行此操作 如果我手动将字符串附加到 Alamofire 中的 URL 我可以轻松完成此操作 但我不希望这样做 我想要的参数为范围 object 参数的一个公共键中有多个值 我一直在做什么 public func find
  • xcodebuild 失败,返回代码:65 - 使用 CLI/Appcenter 但没有错误消息

    我已将 React Native 应用程序升级到 0 59 现在当我尝试使用 AppCenter 或 CLI 存档我的应 用程序时 我收到错误代码 65 但没有任何消息解释问题 当我直接从 Xcode 构建时 Xcode版本 10 2 1
  • 将图像从资产库复制到应用程序文件夹

    我有一个 UIImagePickerController 我想将选定的图像保存到我的应用程序中 我可以通过获取当前的 UIImage 并保存它 通过创建文件 来轻松做到这一点 但我更愿意直接将文件从库复制到我的文件夹中 现在我有了所选图像的
  • 如何在 Xcode 7 beta 4 中调用 SecItemCopyMatching?

    在使用 Swift 的 Xcode 6 和 7 的早期版本中 以下语法可以使用 var secureItemValue Unmanaged
  • 长按 UIButton

    我想知道如果有人按住 UIButton 按键的时间过长 我是否可以捕获 UIButton 的事件 通过通知或其他机制 比按一次按钮的时间更长 假设有人按住按钮几秒钟 谢谢 你可以加UILongPressGestureRecognizer h
  • iOS 15 中 UITableView 部分之间的额外空间

    UITableView显示没有节页脚的多个节 节之间有额外的空间 Xcode 的视图调试器显示它不是视图 而只是一个空白区域 就我而言 这种行为是不受欢迎的 添加 1 0 0 0 高度页脚并没有帮助 更改表视图也不行style 这是示例代码
  • Objective-C 中将 重新定义为另一种符号

    我们有一堂课WayPoint 但在某个时候 我们决定将类重命名为Placemark 然而 我们并不是真的想改变类的名称 因为这会导致现有代码的大量修改 因此 我添加了一行typedef在头文件的底部并开始使用Placemark从那以后 在任
  • 将相机移动到点击的 SCNNode

    我在用着SceneKit and Swift尝试移动相机 使其 聚焦 在所选节点上 我知道我启用了 defaultCameraController 但我试图通过调整相机的位置dolly rotate and translateInCamer
  • Xcode -- 让force_load 使用相对路径

    某些库在链接到 Xcode 项目时需要 all load 链接器标志 但是 如果库之间存在符号冲突 这会导致链接器错误 解决方案是使用 force load 它可以有效地让您在某些库上使用 all load 但不能在其他库上使用 然而 这反
  • 为什么自动布局约束在运行时不会出现?

    I am trying to create a custom subclass of UITableViewCell that displays an image and a few labels to the right of the i

随机推荐