2010-03-03 118 views
1

任何人都知道如何将.jpg图像转换为.bmp格式在iphone中使用Objective-C? 以及我如何处理(或RGB颜色)iPhone设备caputured图像的每个像素? 是否需要转换图像类型?如何使用Objective C将.jpg图像转换为.bmp格式?

+2

你坚持BMP吗?或者你在原始RGB数据之后? – zoul 2010-03-03 10:34:17

回答

0

您将无法轻松在iPhone上获得bmp表示。在Mac上的Cocoa中,它由NSBitmapImageRep类管理,并且非常简单,如下所述。

在高层次上,你需要让.JPG成NSBitmapImageRep对象,然后让框架处理转换为您提供:

一个。将JPG图像转换为NSBitmapImageRep

b。使用内置的NSBitmapImageRep方法来保存所需的格式。

NSBitmapImageRep *origImage = [self documentAsBitmapImageRep:[NSURL fileURLWithPath:pathToJpgImage]]; 
NSBitmapImageRep *bmpImage = [origImage representationUsingType:NSBMPFileType properties:nil]; 

- (NSBitmapImageRep*)documentAsBitmapImageRep:(NSURL*)urlOfJpg; 
{ 

    CIImage *anImage = [CIImage imageWithContentsOfURL:urlOfJpg]; 
    CGRect outputExtent = [anImage extent]; 

    // Create a new NSBitmapImageRep. 
    NSBitmapImageRep *theBitMapToBeSaved = [[NSBitmapImageRep alloc] 
              initWithBitmapDataPlanes:NULL pixelsWide:outputExtent.size.width 
              pixelsHigh:outputExtent.size.height bitsPerSample:8 samplesPerPixel:4 
              hasAlpha:YES isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace 
              bytesPerRow:0 bitsPerPixel:0]; 

    // Create an NSGraphicsContext that draws into the NSBitmapImageRep. 
    NSGraphicsContext *nsContext = [NSGraphicsContext graphicsContextWithBitmapImageRep:theBitMapToBeSaved]; 

    // Save the previous graphics context and state, and make our bitmap context current. 
    [NSGraphicsContext saveGraphicsState]; 
    [NSGraphicsContext setCurrentContext: nsContext]; 
    CGPoint p = CGPointMake(0.0, 0.0); 

    // Get a CIContext from the NSGraphicsContext, and use it to draw the CIImage into the NSBitmapImageRep. 
    [[nsContext CIContext] drawImage:anImage atPoint:p fromRect:outputExtent]; 

    // Restore the previous graphics context and state. 
    [NSGraphicsContext restoreGraphicsState]; 

    return [[theBitMapToBeSaved retain] autorelease]; 

} 

在iPhone上,BMP不直接支持的UIKit,所以你必须向下拖放到Quartz/Core Graphics和管理改造自己。

像素逐像素处理涉及更多。同样,如果这对您来说很难满足,您应该非常熟悉设备上的核心图形功能。

0
  1. 将JPG图像载入UIImage,它可以在本机处理。
  2. 然后,您可以从UIImage对象中获取CGImageRef
  3. 创建一个新的位图CG图像上下文,它具有您已拥有的图像的相同属性,并提供您自己的数据缓冲区以保存位图上下文的字节。
  4. 将原始图像绘制到新的位图上下文中:您提供的缓冲区中的字节现在是图像的像素。
  5. 现在您需要对实际的BMP文件进行编码,这不是UIKit或CoreGraphics(据我所知)框架中存在的功能。幸运的是,这是一个有意无意的格式 - 我在一小时或更短的时间内为BMP编写了快速和不干净的编码器。这里的规范:http://www.fileformat.info/format/bmp/egff.htm(版本3应该没问题,除非你需要支持alpha,但来自JPEG你可能不需要)。

祝你好运。

+0

嗨,你的回答对我真的很有帮助,谢谢。 我可以阅读或比较单词(这是谎言的图像)与其他单词(这也是谎言的形象)?如果可能我该怎么做? – Tirth 2010-07-22 13:51:13

+0

你是指图像中的实际呈现文字?如果是这样,并不平凡 - 图像中的文本识别是整个研究领域。 :)(如果你指的是字节块,那就不一样了) – 2010-07-22 14:37:35

相关问题