如何使用Objective C将.jpg图像转换为.bmp格式?

时间:2021-08-21 21:20:21

Anyone knows how to convert .jpg image to .bmp format in iphone using objective-C? And how i process(or RGB color) the each pixel of IPhone Device caputured image? Is there need to conversion of image type?

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

2 个解决方案

#1


0  

You won't be able to easily get to a bmp representation on the iPhone. In Cocoa on the Mac, it is managed by the NSBitmapImageRep class and is pretty straight forward as outlined below.

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

At a high level, you need to get the .jpg into an NSBitmapImageRep object and then let the frameworks handle the conversion for you:

在较高的层次上,您需要将.jpg转换为NSBitmapImageRep对象,然后让框架为您处理转换:

a. Convert the JPG image to an NSBitmapImageRep

一个。将JPG图像转换为NSBitmapImageRep

b. Use built in NSBitmapImageRep methods to save in desired formats.

湾使用内置的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];

}

On the iPhone, BMP is not directly supported by UIKit, so you would have to drop down into Quartz/Core Graphics and manage the transformation yourself.

在iPhone上,UIKit不直接支持BMP,因此您必须自己进入Quartz / Core Graphics并管理转换。

Pixel by pixel processing is much more involved. Again, you should get intimately familiar with the core graphics capabilities on the device if this is a hard requirement for you.

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

#2


0  

  1. Load the JPG image into a UIImage, which it can handle natively.
  2. 将JPG图像加载到UIImage中,它可以本机处理。
  3. Then you can grab the CGImageRef from the UIImage object.
  4. 然后你可以从UIImage对象中获取CGImageRef。
  5. Create a new bitmap CG image context with the same properties of the image you already have, and provide your own data buffer to hold the bytes of the bitmap context.
  6. 创建一个新的位图CG图像上下文,其具有与您已有图像相同的属性,并提供您自己的数据缓冲区来保存位图上下文的字节。
  7. Draw the original image into the new bitmap context: the bytes in your provided buffer are now the image's pixels.
  8. 将原始图像绘制到新的位图上下文中:提供的缓冲区中的字节现在是图像的像素。
  9. Now you need to encode the actual BMP file, which isn't a functionality that exists in the UIKit or CoreGraphics (as far as I know) frameworks. Fortunately, it's an intentionally trivial format-- I've written quick and dirty encoders for BMP in an hour or less. Here's the spec: http://www.fileformat.info/format/bmp/egff.htm (Version 3 should be fine, unless you need to support alpha, but coming from JPEG you probably don't.)
  10. 现在你需要编码实际的BMP文件,这不是UIKit或CoreGraphics(据我所知)框架中存在的功能。幸运的是,这是一种有意无关紧要的格式 - 我在一小时或更短的时间内为BMP编写了快速而肮脏的编码器。这是规范:http://www.fileformat.info/format/bmp/egff.htm(版本3应该没问题,除非你需要支持alpha,但是你可能不支持。)

Good luck.

祝你好运。

#1


0  

You won't be able to easily get to a bmp representation on the iPhone. In Cocoa on the Mac, it is managed by the NSBitmapImageRep class and is pretty straight forward as outlined below.

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

At a high level, you need to get the .jpg into an NSBitmapImageRep object and then let the frameworks handle the conversion for you:

在较高的层次上,您需要将.jpg转换为NSBitmapImageRep对象,然后让框架为您处理转换:

a. Convert the JPG image to an NSBitmapImageRep

一个。将JPG图像转换为NSBitmapImageRep

b. Use built in NSBitmapImageRep methods to save in desired formats.

湾使用内置的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];

}

On the iPhone, BMP is not directly supported by UIKit, so you would have to drop down into Quartz/Core Graphics and manage the transformation yourself.

在iPhone上,UIKit不直接支持BMP,因此您必须自己进入Quartz / Core Graphics并管理转换。

Pixel by pixel processing is much more involved. Again, you should get intimately familiar with the core graphics capabilities on the device if this is a hard requirement for you.

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

#2


0  

  1. Load the JPG image into a UIImage, which it can handle natively.
  2. 将JPG图像加载到UIImage中,它可以本机处理。
  3. Then you can grab the CGImageRef from the UIImage object.
  4. 然后你可以从UIImage对象中获取CGImageRef。
  5. Create a new bitmap CG image context with the same properties of the image you already have, and provide your own data buffer to hold the bytes of the bitmap context.
  6. 创建一个新的位图CG图像上下文,其具有与您已有图像相同的属性,并提供您自己的数据缓冲区来保存位图上下文的字节。
  7. Draw the original image into the new bitmap context: the bytes in your provided buffer are now the image's pixels.
  8. 将原始图像绘制到新的位图上下文中:提供的缓冲区中的字节现在是图像的像素。
  9. Now you need to encode the actual BMP file, which isn't a functionality that exists in the UIKit or CoreGraphics (as far as I know) frameworks. Fortunately, it's an intentionally trivial format-- I've written quick and dirty encoders for BMP in an hour or less. Here's the spec: http://www.fileformat.info/format/bmp/egff.htm (Version 3 should be fine, unless you need to support alpha, but coming from JPEG you probably don't.)
  10. 现在你需要编码实际的BMP文件,这不是UIKit或CoreGraphics(据我所知)框架中存在的功能。幸运的是,这是一种有意无关紧要的格式 - 我在一小时或更短的时间内为BMP编写了快速而肮脏的编码器。这是规范:http://www.fileformat.info/format/bmp/egff.htm(版本3应该没问题,除非你需要支持alpha,但是你可能不支持。)

Good luck.

祝你好运。